-
Notifications
You must be signed in to change notification settings - Fork 397
Convert hold-related functions to attribute assignments. #328
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -3206,6 +3206,17 @@ def set_axes_limits(self,ax=None): | |
import matplotlib.pyplot as plt | ||
plt.draw_if_interactive() | ||
|
||
|
||
def _save_use_hold(self, ax, kwargs): | ||
h = kwargs.pop('hold', None) | ||
if hasattr(ax, '_hold'): | ||
self._tmp_hold = ax._hold | ||
ax._hold = h | ||
|
||
def _restore_hold(self, ax): | ||
if hasattr(ax, '_hold'): | ||
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 would check for |
||
ax._hold = self._tmp_hold | ||
|
||
@_transform1d | ||
def scatter(self, *args, **kwargs): | ||
""" | ||
|
@@ -3224,17 +3235,11 @@ def scatter(self, *args, **kwargs): | |
Other \**kwargs passed on to matplotlib.pyplot.scatter. | ||
""" | ||
ax, plt = self._ax_plt_from_kw(kwargs) | ||
# allow callers to override the hold state by passing hold=True|False | ||
b = ax.ishold() | ||
h = kwargs.pop('hold',None) | ||
if h is not None: | ||
ax.hold(h) | ||
self._save_use_hold(ax, kwargs) | ||
try: | ||
ret = ax.scatter(*args, **kwargs) | ||
except: | ||
ax.hold(b) | ||
raise | ||
ax.hold(b) | ||
finally: | ||
self._restore_hold(ax) | ||
# reset current active image (only if pyplot is imported). | ||
if plt: | ||
plt.sci(ret) | ||
|
@@ -3262,17 +3267,11 @@ def plot(self, *args, **kwargs): | |
Other \**kwargs passed on to matplotlib.pyplot.plot. | ||
""" | ||
ax = kwargs.pop('ax', None) or self._check_ax() | ||
# allow callers to override the hold state by passing hold=True|False | ||
b = ax.ishold() | ||
h = kwargs.pop('hold',None) | ||
if h is not None: | ||
ax.hold(h) | ||
self._save_use_hold(ax, kwargs) | ||
try: | ||
ret = ax.plot(*args, **kwargs) | ||
except: | ||
ax.hold(b) | ||
raise | ||
ax.hold(b) | ||
finally: | ||
self._restore_hold(ax) | ||
# set axes limits to fit map region. | ||
self.set_axes_limits(ax=ax) | ||
# clip to map limbs | ||
|
@@ -3298,17 +3297,11 @@ def imshow(self, *args, **kwargs): | |
# use origin='lower', unless overridden. | ||
if 'origin' not in kwargs: | ||
kwargs['origin']='lower' | ||
# allow callers to override the hold state by passing hold=True|False | ||
b = ax.ishold() | ||
h = kwargs.pop('hold',None) | ||
if h is not None: | ||
ax.hold(h) | ||
self._save_use_hold(ax, kwargs) | ||
try: | ||
ret = ax.imshow(*args, **kwargs) | ||
except: | ||
ax.hold(b) | ||
raise | ||
ax.hold(b) | ||
finally: | ||
self._restore_hold(ax) | ||
# reset current active image (only if pyplot is imported). | ||
if plt: | ||
plt.sci(ret) | ||
|
@@ -3348,11 +3341,7 @@ def pcolor(self,x,y,data,**kwargs): | |
if the dimensions are the same, then the last row and column of data will be ignored. | ||
""" | ||
ax, plt = self._ax_plt_from_kw(kwargs) | ||
# allow callers to override the hold state by passing hold=True|False | ||
b = ax.ishold() | ||
h = kwargs.pop('hold',None) | ||
if h is not None: | ||
ax.hold(h) | ||
self._save_use_hold(ax, kwargs) | ||
try: | ||
if kwargs.pop('tri', False): | ||
try: | ||
|
@@ -3385,10 +3374,8 @@ def pcolor(self,x,y,data,**kwargs): | |
x = ma.masked_values(np.where(x > 1.e20,1.e20,x), 1.e20) | ||
y = ma.masked_values(np.where(y > 1.e20,1.e20,y), 1.e20) | ||
ret = ax.pcolor(x,y,data,**kwargs) | ||
except: | ||
ax.hold(b) | ||
raise | ||
ax.hold(b) | ||
finally: | ||
self._restore_hold(ax) | ||
# reset current active image (only if pyplot is imported). | ||
if plt: | ||
plt.sci(ret) | ||
|
@@ -3423,17 +3410,11 @@ def pcolormesh(self,x,y,data,**kwargs): | |
if the dimensions are the same, then the last row and column of data will be ignored. | ||
""" | ||
ax, plt = self._ax_plt_from_kw(kwargs) | ||
# allow callers to override the hold state by passing hold=True|False | ||
b = ax.ishold() | ||
h = kwargs.pop('hold',None) | ||
if h is not None: | ||
ax.hold(h) | ||
self._save_use_hold(ax, kwargs) | ||
try: | ||
ret = ax.pcolormesh(x,y,data,**kwargs) | ||
except: | ||
ax.hold(b) | ||
raise | ||
ax.hold(b) | ||
finally: | ||
self._restore_hold(ax) | ||
# reset current active image (only if pyplot is imported). | ||
if plt: | ||
plt.sci(ret) | ||
|
@@ -3469,21 +3450,15 @@ def hexbin(self,x,y,**kwargs): | |
Other \**kwargs passed on to matplotlib.pyplot.hexbin | ||
""" | ||
ax, plt = self._ax_plt_from_kw(kwargs) | ||
# allow callers to override the hold state by passing hold=True|False | ||
b = ax.ishold() | ||
h = kwargs.pop('hold',None) | ||
if h is not None: | ||
ax.hold(h) | ||
self._save_use_hold(ax, kwargs) | ||
try: | ||
# make x,y masked arrays | ||
# (masked where data is outside of projection limb) | ||
x = ma.masked_values(np.where(x > 1.e20,1.e20,x), 1.e20) | ||
y = ma.masked_values(np.where(y > 1.e20,1.e20,y), 1.e20) | ||
ret = ax.hexbin(x,y,**kwargs) | ||
except: | ||
ax.hold(b) | ||
raise | ||
ax.hold(b) | ||
finally: | ||
self._restore_hold(ax) | ||
# reset current active image (only if pyplot is imported). | ||
if plt: | ||
plt.sci(ret) | ||
|
@@ -3515,11 +3490,7 @@ def contour(self,x,y,data,*args,**kwargs): | |
(or tricontour if ``tri=True``). | ||
""" | ||
ax, plt = self._ax_plt_from_kw(kwargs) | ||
# allow callers to override the hold state by passing hold=True|False | ||
b = ax.ishold() | ||
h = kwargs.pop('hold',None) | ||
if h is not None: | ||
ax.hold(h) | ||
self._save_use_hold(ax, kwargs) | ||
try: | ||
if kwargs.pop('tri', False): | ||
try: | ||
|
@@ -3580,10 +3551,8 @@ def contour(self,x,y,data,*args,**kwargs): | |
mask = np.logical_or(ma.getmaskarray(data),xymask) | ||
data = ma.masked_array(data,mask=mask) | ||
CS = ax.contour(x,y,data,*args,**kwargs) | ||
except: | ||
ax.hold(b) | ||
raise | ||
ax.hold(b) | ||
finally: | ||
self._restore_hold(ax) | ||
# reset current active image (only if pyplot is imported). | ||
if plt and CS.get_array() is not None: | ||
plt.sci(CS) | ||
|
@@ -3618,11 +3587,7 @@ def contourf(self,x,y,data,*args,**kwargs): | |
(or tricontourf if ``tri=True``). | ||
""" | ||
ax, plt = self._ax_plt_from_kw(kwargs) | ||
# allow callers to override the hold state by passing hold=True|False | ||
b = ax.ishold() | ||
h = kwargs.pop('hold',None) | ||
if h is not None: | ||
ax.hold(h) | ||
self._save_use_hold(ax, kwargs) | ||
try: | ||
if kwargs.get('tri', False): | ||
try: | ||
|
@@ -3685,10 +3650,8 @@ def contourf(self,x,y,data,*args,**kwargs): | |
mask = np.logical_or(ma.getmaskarray(data),xymask) | ||
data = ma.masked_array(data,mask=mask) | ||
CS = ax.contourf(x,y,data,*args,**kwargs) | ||
except: | ||
ax.hold(b) | ||
raise | ||
ax.hold(b) | ||
finally: | ||
self._restore_hold(ax) | ||
# reset current active image (only if pyplot is imported). | ||
if plt and CS.get_array() is not None: | ||
plt.sci(CS) | ||
|
@@ -3717,17 +3680,11 @@ def quiver(self, x, y, u, v, *args, **kwargs): | |
Other \*args and \**kwargs passed on to matplotlib.pyplot.quiver. | ||
""" | ||
ax, plt = self._ax_plt_from_kw(kwargs) | ||
# allow callers to override the hold state by passing hold=True|False | ||
b = ax.ishold() | ||
h = kwargs.pop('hold',None) | ||
if h is not None: | ||
ax.hold(h) | ||
self._save_use_hold(ax, kwargs) | ||
try: | ||
ret = ax.quiver(x,y,u,v,*args,**kwargs) | ||
except: | ||
ax.hold(b) | ||
raise | ||
ax.hold(b) | ||
finally: | ||
self._restore_hold(ax) | ||
if plt is not None and ret.get_array() is not None: | ||
plt.sci(ret) | ||
# set axes limits to fit map region. | ||
|
@@ -3759,17 +3716,11 @@ def streamplot(self, x, y, u, v, *args, **kwargs): | |
you have %s""" % _matplotlib_version) | ||
raise NotImplementedError(msg) | ||
ax, plt = self._ax_plt_from_kw(kwargs) | ||
# allow callers to override the hold state by passing hold=True|False | ||
b = ax.ishold() | ||
h = kwargs.pop('hold',None) | ||
if h is not None: | ||
ax.hold(h) | ||
self._save_use_hold(ax, kwargs) | ||
try: | ||
ret = ax.streamplot(x,y,u,v,*args,**kwargs) | ||
except: | ||
ax.hold(b) | ||
raise | ||
ax.hold(b) | ||
finally: | ||
self._restore_hold(ax) | ||
if plt is not None and ret.lines.get_array() is not None: | ||
plt.sci(ret.lines) | ||
# set axes limits to fit map region. | ||
|
@@ -3809,24 +3760,18 @@ def barbs(self, x, y, u, v, *args, **kwargs): | |
you have %s""" % _matplotlib_version) | ||
raise NotImplementedError(msg) | ||
ax, plt = self._ax_plt_from_kw(kwargs) | ||
# allow callers to override the hold state by passing hold=True|False | ||
b = ax.ishold() | ||
h = kwargs.pop('hold',None) | ||
if h is not None: | ||
ax.hold(h) | ||
lons, lats = self(x, y, inverse=True) | ||
unh = ma.masked_where(lats <= 0, u) | ||
vnh = ma.masked_where(lats <= 0, v) | ||
ush = ma.masked_where(lats > 0, u) | ||
vsh = ma.masked_where(lats > 0, v) | ||
self._save_use_hold(ax, kwargs) | ||
try: | ||
retnh = ax.barbs(x,y,unh,vnh,*args,**kwargs) | ||
kwargs['flip_barb']=True | ||
retsh = ax.barbs(x,y,ush,vsh,*args,**kwargs) | ||
except: | ||
ax.hold(b) | ||
raise | ||
ax.hold(b) | ||
finally: | ||
self._restore_hold(ax) | ||
# Because there are two collections returned in general, | ||
# we can't set the current image... | ||
#if plt is not None and ret.get_array() is not None: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This assignment only happens if the axes already has a
_hold
attribute. So, this wouldn't work if someone was setting the hold initially.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.
My thinking here is that presently (with 2.x), the _hold attribute is always present. It is set in the AxesBase initializer. Some time in the future it won't be, and at that point Basemap doesn't need to obey it, and shouldn't set it at all.
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.
ah, yeah, that does make sense then. And it prevents basemap from adding the attribute itself if it isn't there already.