Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cb756d3

Browse filesBrowse files
authored
Merge pull request #19369 from anntzer/cmset
Add Artist._cm_set for temporarily setting an Artist property.
2 parents 688badb + 9bf86b2 commit cb756d3
Copy full SHA for cb756d3

File tree

Expand file treeCollapse file tree

5 files changed

+37
-44
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+37
-44
lines changed

‎lib/matplotlib/artist.py

Copy file name to clipboardExpand all lines: lib/matplotlib/artist.py
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from collections import namedtuple
2+
import contextlib
23
from functools import wraps
34
import inspect
45
import logging
@@ -1154,6 +1155,18 @@ def set(self, **kwargs):
11541155
kwargs = cbook.normalize_kwargs(kwargs, self)
11551156
return self.update(kwargs)
11561157

1158+
@contextlib.contextmanager
1159+
def _cm_set(self, **kwargs):
1160+
"""
1161+
`.Artist.set` context-manager that restores original values at exit.
1162+
"""
1163+
orig_vals = {k: getattr(self, f"get_{k}")() for k in kwargs}
1164+
try:
1165+
self.set(**kwargs)
1166+
yield
1167+
finally:
1168+
self.set(**orig_vals)
1169+
11571170
def findobj(self, match=None, include_self=True):
11581171
"""
11591172
Find artist objects.

‎lib/matplotlib/axes/_base.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3122,8 +3122,7 @@ def redraw_in_frame(self):
31223122
with ExitStack() as stack:
31233123
for artist in [*self._get_axis_list(),
31243124
self.title, self._left_title, self._right_title]:
3125-
stack.callback(artist.set_visible, artist.get_visible())
3126-
artist.set_visible(False)
3125+
stack.enter_context(artist._cm_set(visible=False))
31273126
self.draw(self.figure._cachedRenderer)
31283127

31293128
def get_renderer_cache(self):

‎lib/matplotlib/backend_bases.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backend_bases.py
+12-23Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"""
2727

2828
from collections import namedtuple
29-
from contextlib import contextmanager, nullcontext
29+
from contextlib import ExitStack, contextmanager, nullcontext
3030
from enum import Enum, IntEnum
3131
import functools
3232
import importlib
@@ -2256,22 +2256,16 @@ def print_figure(
22562256

22572257
# Remove the figure manager, if any, to avoid resizing the GUI widget.
22582258
with cbook._setattr_cm(self, manager=None), \
2259-
cbook._setattr_cm(self.figure, dpi=dpi), \
2260-
cbook._setattr_cm(canvas, _is_saving=True):
2261-
origfacecolor = self.figure.get_facecolor()
2262-
origedgecolor = self.figure.get_edgecolor()
2263-
2264-
if facecolor is None:
2265-
facecolor = rcParams['savefig.facecolor']
2266-
if cbook._str_equal(facecolor, 'auto'):
2267-
facecolor = origfacecolor
2268-
if edgecolor is None:
2269-
edgecolor = rcParams['savefig.edgecolor']
2270-
if cbook._str_equal(edgecolor, 'auto'):
2271-
edgecolor = origedgecolor
2272-
2273-
self.figure.set_facecolor(facecolor)
2274-
self.figure.set_edgecolor(edgecolor)
2259+
cbook._setattr_cm(self.figure, dpi=dpi), \
2260+
cbook._setattr_cm(canvas, _is_saving=True), \
2261+
ExitStack() as stack:
2262+
2263+
for prop in ["facecolor", "edgecolor"]:
2264+
color = locals()[prop]
2265+
if color is None:
2266+
color = rcParams[f"savefig.{prop}"]
2267+
if not cbook._str_equal(color, "auto"):
2268+
stack.enter_context(self.figure._cm_set(**{prop: color}))
22752269

22762270
if bbox_inches is None:
22772271
bbox_inches = rcParams['savefig.bbox']
@@ -2306,8 +2300,7 @@ def print_figure(
23062300
_bbox_inches_restore = None
23072301

23082302
# we have already done CL above, so turn it off:
2309-
cl_state = self.figure.get_constrained_layout()
2310-
self.figure.set_constrained_layout(False)
2303+
stack.enter_context(self.figure._cm_set(constrained_layout=False))
23112304
try:
23122305
# _get_renderer may change the figure dpi (as vector formats
23132306
# force the figure dpi to 72), so we need to set it again here.
@@ -2323,11 +2316,7 @@ def print_figure(
23232316
if bbox_inches and restore_bbox:
23242317
restore_bbox()
23252318

2326-
self.figure.set_facecolor(origfacecolor)
2327-
self.figure.set_edgecolor(origedgecolor)
23282319
self.figure.set_canvas(self)
2329-
# reset to cached state
2330-
self.figure.set_constrained_layout(cl_state)
23312320
return result
23322321

23332322
@classmethod

‎lib/matplotlib/figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/figure.py
+10-17Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
Control the default spacing between subplots.
1515
"""
1616

17+
from contextlib import ExitStack
1718
import inspect
1819
import logging
1920
from numbers import Integral
@@ -2936,23 +2937,15 @@ def savefig(self, fname, *, transparent=None, **kwargs):
29362937
if transparent is None:
29372938
transparent = mpl.rcParams['savefig.transparent']
29382939

2939-
if transparent:
2940-
kwargs.setdefault('facecolor', 'none')
2941-
kwargs.setdefault('edgecolor', 'none')
2942-
original_axes_colors = []
2943-
for ax in self.axes:
2944-
patch = ax.patch
2945-
original_axes_colors.append((patch.get_facecolor(),
2946-
patch.get_edgecolor()))
2947-
patch.set_facecolor('none')
2948-
patch.set_edgecolor('none')
2949-
2950-
self.canvas.print_figure(fname, **kwargs)
2951-
2952-
if transparent:
2953-
for ax, cc in zip(self.axes, original_axes_colors):
2954-
ax.patch.set_facecolor(cc[0])
2955-
ax.patch.set_edgecolor(cc[1])
2940+
with ExitStack() as stack:
2941+
if transparent:
2942+
kwargs.setdefault('facecolor', 'none')
2943+
kwargs.setdefault('edgecolor', 'none')
2944+
for ax in self.axes:
2945+
stack.enter_context(
2946+
ax.patch._cm_set(facecolor='none', edgecolor='none'))
2947+
2948+
self.canvas.print_figure(fname, **kwargs)
29562949

29572950
def ginput(self, n=1, timeout=30, show_clicks=True,
29582951
mouse_add=MouseButton.LEFT,

‎lib/matplotlib/widgets.py

Copy file name to clipboardExpand all lines: lib/matplotlib/widgets.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,8 +1735,7 @@ def update_background(self, event):
17351735
with ExitStack() as stack:
17361736
if needs_redraw:
17371737
for artist in self.artists:
1738-
stack.callback(artist.set_visible, artist.get_visible())
1739-
artist.set_visible(False)
1738+
stack.enter_context(artist._cm_set(visible=False))
17401739
self.canvas.draw()
17411740
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
17421741
if needs_redraw:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.