Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport PR #25085 on branch v3.7.x (FIX: only try to update blit caches if the canvas we expect) #25188

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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