Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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 2 commits into from
Jan 11, 2017
Merged
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
143 changes: 44 additions & 99 deletions lib/mpl_toolkits/basemap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.


def _restore_hold(self, ax):
if hasattr(ax, '_hold'):
Copy link
Member

Choose a reason for hiding this comment

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

I would check for hasattr(self, '_tmp_hold') instead, and then del the attribute when done

ax._hold = self._tmp_hold

@_transform1d
def scatter(self, *args, **kwargs):
"""
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down