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

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Jan 26, 2021

This corresponds to _setattr_cm, but for Artist properties.
(Calling it _set_cm sounded too much like setting a "cm" property.
Perhaps _setattr_cm should have been _cm_setattr...)

See #19342 (comment) and discussion above.

PR Summary

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

This corresponds to _setattr_cm, but for Artist properties.
(Calling it `_set_cm` sounded too much like setting a "cm" property.
Perhaps `_setattr_cm` should have been `_cm_setattr`...)
Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

Naming: Alternatives could be _temporary_set or _temp_properties or similar.

Comment on lines 2258 to +2261
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:
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

@QuLogic QuLogic added this to the v3.5.0 milestone May 6, 2021
@QuLogic QuLogic merged commit cb756d3 into matplotlib:master May 6, 2021
@anntzer anntzer deleted the cmset branch May 6, 2021 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants