-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Turn autoscale into a contextmanager. #5538
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
from matplotlib.externals import six | ||
from matplotlib.externals.six.moves import xrange | ||
|
||
from contextlib import contextmanager | ||
import itertools | ||
import warnings | ||
import math | ||
|
@@ -2103,22 +2104,31 @@ def autoscale(self, enable=True, axis='both', tight=None): | |
The *tight* setting is retained for future autoscaling | ||
until it is explicitly changed. | ||
|
||
This returns a context-manager/decorator that allows temporarily | ||
setting the autoscale status, i.e.:: | ||
|
||
Returns None. | ||
with axes.autoscale(enable): ... | ||
|
||
will keep the autoscale status to `enable` for the duration of the | ||
block only. | ||
""" | ||
if enable is None: | ||
scalex = True | ||
scaley = True | ||
else: | ||
scalex = False | ||
scaley = False | ||
orig_autoscale = self._autoscaleXon, self._autoscaleYon | ||
if enable is not None: | ||
if axis in ['x', 'both']: | ||
self._autoscaleXon = bool(enable) | ||
scalex = self._autoscaleXon | ||
if axis in ['y', 'both']: | ||
self._autoscaleYon = bool(enable) | ||
scaley = self._autoscaleYon | ||
self.autoscale_view(tight=tight, scalex=scalex, scaley=scaley) | ||
self.autoscale_view( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not behave the same if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, so I misunderstood the code (I never used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was just reading this code locally and sorting through what inputs are going to the |
||
tight=tight, scalex=self._autoscaleXon, scaley=self._autoscaleYon) | ||
|
||
@contextmanager | ||
def restore_autoscaling(): | ||
try: | ||
yield | ||
finally: | ||
self._autoscaleXon, self._autoscaleYon = orig_autoscale | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we mark the axes object as stale? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I have no idea of what you're talking about so I'll leave the discussion to the specialists.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this needs to set stale because changing this internal state does not invalidate the drawn figure (that is the next call to |
||
|
||
return restore_autoscaling() | ||
|
||
def autoscale_view(self, tight=None, scalex=True, scaley=True): | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put these is a file in
api_changes/*.rst
. This helps to prevent the sort of conflicts this branch has.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather close this PR and make
margin
a tristate as discussed above. In fact, makingautoscale
a contextmanager is a bit misleading given that at the exit of the contextmanager (assuming it temporarily disabled autoscaling), all the artists, including those created while the cm was active, will participate in the next autoscaling... which defeats the purpose of the cm IMO. Thoughts?The fix for z autoscaling should go in its own PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anntzer That sounds like a good plan to me.
Wish we were 3 only and could use enums 😞
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case I think the best is to use strings everywhere (IMO the problem of using, say, True, False and a third value is that it is too easy to accidentally test for the boolean value and forget about the third possibility). Say "default" (current True), "remove" (current False) and "ignore"?
attn @mdboom as the author of #5583.