Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 54db976

Browse files
committed
FIX: be very paranoid about checking what the current canvas is
closes #25572
1 parent 370547e commit 54db976

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,3 +1740,14 @@ def test_MultiCursor(horizOn, vertOn):
17401740
assert l.get_xdata() == (.5, .5)
17411741
for l in multi.hlines:
17421742
assert l.get_ydata() == (.25, .25)
1743+
1744+
1745+
def test_parent_axes_removal():
1746+
1747+
fig, ax = plt.subplots(1, 1)
1748+
1749+
radio = widgets.RadioButtons(ax, ['1', '2'], 0)
1750+
1751+
ax.remove()
1752+
with io.BytesIO() as out:
1753+
fig.savefig(out, format='raw')

lib/matplotlib/widgets.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,15 @@ def _changed_canvas(self):
104104
True if the canvas has been changed.
105105
106106
"""
107-
return self.canvas is not self.ax.figure.canvas
107+
# no canvas, something is very fishy
108+
if self.canvas is None:
109+
return True
110+
# self.ax.figure.canvas but very defensively, can got to odd states if the
111+
# the widget or the parent Axes is removed from the Figure
112+
parent_canvas = getattr(
113+
getattr(getattr(self, 'ax', None), 'figure', None), 'canvas', None
114+
)
115+
return self.canvas is not parent_canvas
108116

109117

110118
class AxesWidget(Widget):

0 commit comments

Comments
 (0)