FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

FIX: only try to update blit caches if the canvas we expect by tacaswell · Pull Request #25085 · matplotlib/matplotlib · GitHub

Repository navigation

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

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
40 changes: 40 additions & 0 deletions lib/matplotlib/tests/test_widgets.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
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

Back | FazBrowse Home | New Git URL