Skip to content

Commit

Permalink
Merge pull request #25188 from meeseeksmachine/auto-backport-of-pr-25…
Browse files Browse the repository at this point in the history
…085-on-v3.7.x

Backport PR #25085 on branch v3.7.x (FIX: only try to update blit caches if the canvas we expect)
  • Loading branch information
ksunden committed Feb 10, 2023
2 parents 12b1dc2 + f8ecdaf commit fc775b1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
40 changes: 40 additions & 0 deletions lib/matplotlib/tests/test_widgets.py
@@ -1,4 +1,5 @@
import functools
import io
from unittest import mock

from matplotlib._api.deprecation import MatplotlibDeprecationWarning
Expand All @@ -23,6 +24,45 @@ def ax():
return get_ax()


def test_save_blitted_widget_as_pdf():
from matplotlib.widgets import CheckButtons, RadioButtons
from matplotlib.cbook import _get_running_interactive_framework
if _get_running_interactive_framework() not in ['headless', None]:
pytest.xfail("Callback exceptions are not raised otherwise.")

fig, ax = plt.subplots(
nrows=2, ncols=2, figsize=(5, 2), width_ratios=[1, 2]
)
default_rb = RadioButtons(ax[0, 0], ['Apples', 'Oranges'])
styled_rb = RadioButtons(
ax[0, 1], ['Apples', 'Oranges'],
label_props={'color': ['red', 'orange'],
'fontsize': [16, 20]},
radio_props={'edgecolor': ['red', 'orange'],
'facecolor': ['mistyrose', 'peachpuff']}
)

default_cb = CheckButtons(ax[1, 0], ['Apples', 'Oranges'],
actives=[True, True])
styled_cb = CheckButtons(
ax[1, 1], ['Apples', 'Oranges'],
actives=[True, True],
label_props={'color': ['red', 'orange'],
'fontsize': [16, 20]},
frame_props={'edgecolor': ['red', 'orange'],
'facecolor': ['mistyrose', 'peachpuff']},
check_props={'color': ['darkred', 'darkorange']}
)

ax[0, 0].set_title('Default')
ax[0, 1].set_title('Stylized')
# force an Agg render
fig.canvas.draw()
# force a pdf save
with io.BytesIO() as result_after:
fig.savefig(result_after, format='pdf')


@pytest.mark.parametrize('kwargs', [
dict(),
dict(useblit=True, button=1),
Expand Down
28 changes: 25 additions & 3 deletions lib/matplotlib/widgets.py
Expand Up @@ -90,6 +90,22 @@ def ignore(self, event):
"""
return not self.active

def _changed_canvas(self):
"""
Someone has switched the canvas on us!
This happens if `savefig` needs to save to a format the previous
backend did not support (e.g. saving a figure using an Agg based
backend saved to a vector format).
Returns
-------
bool
True if the canvas has been changed.
"""
return self.canvas is not self.ax.figure.canvas


class AxesWidget(Widget):
"""
Expand Down Expand Up @@ -1088,7 +1104,7 @@ def __init__(self, ax, labels, actives=None, *, useblit=True,

def _clear(self, event):
"""Internal event handler to clear the buttons."""
if self.ignore(event):
if self.ignore(event) or self._changed_canvas():
return
self._background = self.canvas.copy_from_bbox(self.ax.bbox)
self.ax.draw_artist(self._checks)
Expand Down Expand Up @@ -1700,7 +1716,7 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,

def _clear(self, event):
"""Internal event handler to clear the buttons."""
if self.ignore(event):
if self.ignore(event) or self._changed_canvas():
return
self._background = self.canvas.copy_from_bbox(self.ax.bbox)
self.ax.draw_artist(self._buttons)
Expand Down Expand Up @@ -1971,7 +1987,7 @@ def __init__(self, ax, horizOn=True, vertOn=True, useblit=False,

def clear(self, event):
"""Internal event handler to clear the cursor."""
if self.ignore(event):
if self.ignore(event) or self._changed_canvas():
return
if self.useblit:
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
Expand Down Expand Up @@ -2110,6 +2126,12 @@ def clear(self, event):
return
if self.useblit:
for canvas, info in self._canvas_infos.items():
# someone has switched the canvas on us! This happens if
# `savefig` needs to save to a format the previous backend did
# not support (e.g. saving a figure using an Agg based backend
# saved to a vector format).
if canvas is not canvas.figure.canvas:
continue
info["background"] = canvas.copy_from_bbox(canvas.figure.bbox)

def onmove(self, event):
Expand Down

0 comments on commit fc775b1

Please sign in to comment.