Skip to content

Add Artist._cm_set for temporarily setting an Artist property. #19369

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

Merged
merged 1 commit into from
May 6, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import namedtuple
import contextlib
from functools import wraps
import inspect
import logging
Expand Down Expand Up @@ -1154,6 +1155,18 @@ def set(self, **kwargs):
kwargs = cbook.normalize_kwargs(kwargs, self)
return self.update(kwargs)

@contextlib.contextmanager
def _cm_set(self, **kwargs):
"""
`.Artist.set` context-manager that restores original values at exit.
"""
orig_vals = {k: getattr(self, f"get_{k}")() for k in kwargs}
try:
self.set(**kwargs)
yield
finally:
self.set(**orig_vals)

def findobj(self, match=None, include_self=True):
"""
Find artist objects.
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3122,8 +3122,7 @@ def redraw_in_frame(self):
with ExitStack() as stack:
for artist in [*self._get_axis_list(),
self.title, self._left_title, self._right_title]:
stack.callback(artist.set_visible, artist.get_visible())
artist.set_visible(False)
stack.enter_context(artist._cm_set(visible=False))
self.draw(self.figure._cachedRenderer)

def get_renderer_cache(self):
Expand Down
35 changes: 12 additions & 23 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"""

from collections import namedtuple
from contextlib import contextmanager, nullcontext
from contextlib import ExitStack, contextmanager, nullcontext
from enum import Enum, IntEnum
import functools
import importlib
Expand Down Expand Up @@ -2256,22 +2256,16 @@ def print_figure(

# Remove the figure manager, if any, to avoid resizing the GUI widget.
with cbook._setattr_cm(self, manager=None), \
cbook._setattr_cm(self.figure, dpi=dpi), \
cbook._setattr_cm(canvas, _is_saving=True):
origfacecolor = self.figure.get_facecolor()
origedgecolor = self.figure.get_edgecolor()

if facecolor is None:
facecolor = rcParams['savefig.facecolor']
if cbook._str_equal(facecolor, 'auto'):
facecolor = origfacecolor
if edgecolor is None:
edgecolor = rcParams['savefig.edgecolor']
if cbook._str_equal(edgecolor, 'auto'):
edgecolor = origedgecolor

self.figure.set_facecolor(facecolor)
self.figure.set_edgecolor(edgecolor)
cbook._setattr_cm(self.figure, dpi=dpi), \
cbook._setattr_cm(canvas, _is_saving=True), \
ExitStack() as stack:
Comment on lines 2258 to +2261
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could re-arrange this to avoid the escaped newlines:

Suggested change
with cbook._setattr_cm(self, manager=None), \
cbook._setattr_cm(self.figure, dpi=dpi), \
cbook._setattr_cm(canvas, _is_saving=True):
origfacecolor = self.figure.get_facecolor()
origedgecolor = self.figure.get_edgecolor()
if facecolor is None:
facecolor = rcParams['savefig.facecolor']
if cbook._str_equal(facecolor, 'auto'):
facecolor = origfacecolor
if edgecolor is None:
edgecolor = rcParams['savefig.edgecolor']
if cbook._str_equal(edgecolor, 'auto'):
edgecolor = origedgecolor
self.figure.set_facecolor(facecolor)
self.figure.set_edgecolor(edgecolor)
cbook._setattr_cm(self.figure, dpi=dpi), \
cbook._setattr_cm(canvas, _is_saving=True), \
ExitStack() as stack:
with ExitStack() as stack:
stack.enter_context(cbook._setattr_cm(self, manager=None))
stack.enter_context(cbook._setattr_cm(self.figure, dpi=dpi))
stack.enter_context(cbook._setattr_cm(canvas, _is_saving=True))

and maybe move the comment in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having syntactic context managers remains more readable; also note that in Py3.10+ we'll be able to parenthesize the multiple contextmanagers to avoid the backslashes anyways.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK


for prop in ["facecolor", "edgecolor"]:
color = locals()[prop]
if color is None:
color = rcParams[f"savefig.{prop}"]
if not cbook._str_equal(color, "auto"):
stack.enter_context(self.figure._cm_set(**{prop: color}))

if bbox_inches is None:
bbox_inches = rcParams['savefig.bbox']
Expand Down Expand Up @@ -2306,8 +2300,7 @@ def print_figure(
_bbox_inches_restore = None

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

self.figure.set_facecolor(origfacecolor)
self.figure.set_edgecolor(origedgecolor)
self.figure.set_canvas(self)
# reset to cached state
self.figure.set_constrained_layout(cl_state)
return result

@classmethod
Expand Down
27 changes: 10 additions & 17 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Control the default spacing between subplots.
"""

from contextlib import ExitStack
import inspect
import logging
from numbers import Integral
Expand Down Expand Up @@ -2922,23 +2923,15 @@ def savefig(self, fname, *, transparent=None, **kwargs):
if transparent is None:
transparent = mpl.rcParams['savefig.transparent']

if transparent:
kwargs.setdefault('facecolor', 'none')
kwargs.setdefault('edgecolor', 'none')
original_axes_colors = []
for ax in self.axes:
patch = ax.patch
original_axes_colors.append((patch.get_facecolor(),
patch.get_edgecolor()))
patch.set_facecolor('none')
patch.set_edgecolor('none')

self.canvas.print_figure(fname, **kwargs)

if transparent:
for ax, cc in zip(self.axes, original_axes_colors):
ax.patch.set_facecolor(cc[0])
ax.patch.set_edgecolor(cc[1])
with ExitStack() as stack:
if transparent:
kwargs.setdefault('facecolor', 'none')
kwargs.setdefault('edgecolor', 'none')
for ax in self.axes:
stack.enter_context(
ax.patch._cm_set(facecolor='none', edgecolor='none'))

self.canvas.print_figure(fname, **kwargs)

def ginput(self, n=1, timeout=30, show_clicks=True,
mouse_add=MouseButton.LEFT,
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1735,8 +1735,7 @@ def update_background(self, event):
with ExitStack() as stack:
if needs_redraw:
for artist in self.artists:
stack.callback(artist.set_visible, artist.get_visible())
artist.set_visible(False)
stack.enter_context(artist._cm_set(visible=False))
self.canvas.draw()
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
if needs_redraw:
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy