8000 Only keep weakrefs to the axes. · matplotlib/matplotlib@cffeef0 · GitHub
[go: up one dir, main page]

Skip to content

Commit cffeef0

Browse files
committed
Only keep weakrefs to the axes.
1 parent 2b0b376 commit cffeef0

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import sys
4747
import time
4848
import warnings
49+
from weakref import WeakKeyDictionary
4950

5051
import numpy as np
5152
import matplotlib.cbook as cbook
@@ -2799,15 +2800,11 @@ def __init__(self, canvas):
27992800
@partial(canvas.mpl_connect, 'draw_event')
28002801
def update_stack(event):
28012802
nav_info = self._nav_stack()
2802-
if nav_info is None:
2803-
# Define the true initial navigation info.
2804-
self.push_current()
2805-
else:
2806-
axes, views, positions = nav_info
2807-
if axes != self.canvas.figure.axes:
2803+
if (nav_info is None # True initial navigation info.
28082804
# An axes has been added or removed, so update the
28092805
# navigation info too.
2810-
self.push_current()
2806+
or set(nav_info) != set(self.canvas.figure.axes)):
2807+
self.push_current()
28112808

28122809
def set_message(self, s):
28132810
"""Display a message on toolbar or in status bar."""
@@ -3016,13 +3013,13 @@ def _switch_off_zoom_mode(self, event):
30163013

30173014
def push_current(self):
30183015
"""Push the current view limits and position onto the stack."""
3019-
axs = self.canvas.figure.axes
3020-
views = [ax._get_view() for ax in axs]
3021-
# Store both the original and modified positions.
3022-
positions = [
3023-
(ax.get_position(True).frozen(), ax.get_position().frozen())
3024-
for ax in axs]
3025-
self._nav_stack.push((axs, views, positions))
3016+
self._nav_stack.push(
3017+
WeakKeyDictionary(
3018+
{ax: (ax._get_view(),
3019+
# Store both the original and modified positions.
3020+
(ax.get_position(True).frozen(),
3021+
ax.get_position().frozen()))
3022+
for ax in self.canvas.figure.axes}))
30263023
self.set_history_buttons()
30273024

30283025
def release(self, event):
@@ -3143,16 +3140,17 @@ def _update_view(self):
31433140
"""Update the viewlim and position from the view and
31443141
position stack for each axes.
31453142
"""
3146-
31473143
nav_info = self._nav_stack()
31483144
if nav_info is None:
31493145
return
3150-
axs, views, pos = nav_info
3151-
for i, a in enumerate(self.canvas.figure.get_axes()):
3152-
a._set_view(views[i])
3146+
# Retrieve all items at once to avoid any risk of GC deleting an Axes
3147+
# while in the middle of the loop below.
3148+
items = list(nav_info.items())
3149+
for ax, (view, (pos_orig, pos_active)) in items:
3150+
ax._set_view(view)
31533151
# Restore both the original and modified positions
3154-
a.set_position(pos[i][0], 'original')
3155-
a.set_position(pos[i][1], 'active')
3152+
ax.set_position(pos_orig, 'original')
3153+
ax.set_position(pos_active, 'active')
31563154
self.canvas.draw_idle()
31573155

31583156
def save_figure(self, *args):

0 commit comments

Comments
 (0)
0