Skip to content

Commit 6cd39d5

Browse files
authored
Merge pull request #328 from efiring/kill_hold
Convert hold-related functions to attribute assignments.
2 parents 845fb6d + 91dcace commit 6cd39d5

File tree

1 file changed

+44
-99
lines changed

1 file changed

+44
-99
lines changed

lib/mpl_toolkits/basemap/__init__.py

Lines changed: 44 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -3207,6 +3207,17 @@ def set_axes_limits(self,ax=None):
32073207
import matplotlib.pyplot as plt
32083208
plt.draw_if_interactive()
32093209

3210+
3211+
def _save_use_hold(self, ax, kwargs):
3212+
h = kwargs.pop('hold', None)
3213+
if hasattr(ax, '_hold'):
3214+
self._tmp_hold = ax._hold
3215+
ax._hold = h
3216+
3217+
def _restore_hold(self, ax):
3218+
if hasattr(ax, '_hold'):
3219+
ax._hold = self._tmp_hold
3220+
32103221
@_transform1d
32113222
def scatter(self, *args, **kwargs):
32123223
"""
@@ -3225,17 +3236,11 @@ def scatter(self, *args, **kwargs):
32253236
Other \**kwargs passed on to matplotlib.pyplot.scatter.
32263237
"""
32273238
ax, plt = self._ax_plt_from_kw(kwargs)
3228-
# allow callers to override the hold state by passing hold=True|False
3229-
b = ax.ishold()
3230-
h = kwargs.pop('hold',None)
3231-
if h is not None:
3232-
ax.hold(h)
3239+
self._save_use_hold(ax, kwargs)
32333240
try:
32343241
ret = ax.scatter(*args, **kwargs)
3235-
except:
3236-
ax.hold(b)
3237-
raise
3238-
ax.hold(b)
3242+
finally:
3243+
self._restore_hold(ax)
32393244
# reset current active image (only if pyplot is imported).
32403245
if plt:
32413246
plt.sci(ret)
@@ -3263,17 +3268,11 @@ def plot(self, *args, **kwargs):
32633268
Other \**kwargs passed on to matplotlib.pyplot.plot.
32643269
"""
32653270
ax = kwargs.pop('ax', None) or self._check_ax()
3266-
# allow callers to override the hold state by passing hold=True|False
3267-
b = ax.ishold()
3268-
h = kwargs.pop('hold',None)
3269-
if h is not None:
3270-
ax.hold(h)
3271+
self._save_use_hold(ax, kwargs)
32713272
try:
32723273
ret = ax.plot(*args, **kwargs)
3273-
except:
3274-
ax.hold(b)
3275-
raise
3276-
ax.hold(b)
3274+
finally:
3275+
self._restore_hold(ax)
32773276
# set axes limits to fit map region.
32783277
self.set_axes_limits(ax=ax)
32793278
# clip to map limbs
@@ -3299,17 +3298,11 @@ def imshow(self, *args, **kwargs):
32993298
# use origin='lower', unless overridden.
33003299
if 'origin' not in kwargs:
33013300
kwargs['origin']='lower'
3302-
# allow callers to override the hold state by passing hold=True|False
3303-
b = ax.ishold()
3304-
h = kwargs.pop('hold',None)
3305-
if h is not None:
3306-
ax.hold(h)
3301+
self._save_use_hold(ax, kwargs)
33073302
try:
33083303
ret = ax.imshow(*args, **kwargs)
3309-
except:
3310-
ax.hold(b)
3311-
raise
3312-
ax.hold(b)
3304+
finally:
3305+
self._restore_hold(ax)
33133306
# reset current active image (only if pyplot is imported).
33143307
if plt:
33153308
plt.sci(ret)
@@ -3349,11 +3342,7 @@ def pcolor(self,x,y,data,**kwargs):
33493342
if the dimensions are the same, then the last row and column of data will be ignored.
33503343
"""
33513344
ax, plt = self._ax_plt_from_kw(kwargs)
3352-
# allow callers to override the hold state by passing hold=True|False
3353-
b = ax.ishold()
3354-
h = kwargs.pop('hold',None)
3355-
if h is not None:
3356-
ax.hold(h)
3345+
self._save_use_hold(ax, kwargs)
33573346
try:
33583347
if kwargs.pop('tri', False):
33593348
try:
@@ -3386,10 +3375,8 @@ def pcolor(self,x,y,data,**kwargs):
33863375
x = ma.masked_values(np.where(x > 1.e20,1.e20,x), 1.e20)
33873376
y = ma.masked_values(np.where(y > 1.e20,1.e20,y), 1.e20)
33883377
ret = ax.pcolor(x,y,data,**kwargs)
3389-
except:
3390-
ax.hold(b)
3391-
raise
3392-
ax.hold(b)
3378+
finally:
3379+
self._restore_hold(ax)
33933380
# reset current active image (only if pyplot is imported).
33943381
if plt:
33953382
plt.sci(ret)
@@ -3424,17 +3411,11 @@ def pcolormesh(self,x,y,data,**kwargs):
34243411
if the dimensions are the same, then the last row and column of data will be ignored.
34253412
"""
34263413
ax, plt = self._ax_plt_from_kw(kwargs)
3427-
# allow callers to override the hold state by passing hold=True|False
3428-
b = ax.ishold()
3429-
h = kwargs.pop('hold',None)
3430-
if h is not None:
3431-
ax.hold(h)
3414+
self._save_use_hold(ax, kwargs)
34323415
try:
34333416
ret = ax.pcolormesh(x,y,data,**kwargs)
3434-
except:
3435-
ax.hold(b)
3436-
raise
3437-
ax.hold(b)
3417+
finally:
3418+
self._restore_hold(ax)
34383419
# reset current active image (only if pyplot is imported).
34393420
if plt:
34403421
plt.sci(ret)
@@ -3470,21 +3451,15 @@ def hexbin(self,x,y,**kwargs):
34703451
Other \**kwargs passed on to matplotlib.pyplot.hexbin
34713452
"""
34723453
ax, plt = self._ax_plt_from_kw(kwargs)
3473-
# allow callers to override the hold state by passing hold=True|False
3474-
b = ax.ishold()
3475-
h = kwargs.pop('hold',None)
3476-
if h is not None:
3477-
ax.hold(h)
3454+
self._save_use_hold(ax, kwargs)
34783455
try:
34793456
# make x,y masked arrays
34803457
# (masked where data is outside of projection limb)
34813458
x = ma.masked_values(np.where(x > 1.e20,1.e20,x), 1.e20)
34823459
y = ma.masked_values(np.where(y > 1.e20,1.e20,y), 1.e20)
34833460
ret = ax.hexbin(x,y,**kwargs)
3484-
except:
3485-
ax.hold(b)
3486-
raise
3487-
ax.hold(b)
3461+
finally:
3462+
self._restore_hold(ax)
34883463
# reset current active image (only if pyplot is imported).
34893464
if plt:
34903465
plt.sci(ret)
@@ -3516,11 +3491,7 @@ def contour(self,x,y,data,*args,**kwargs):
35163491
(or tricontour if ``tri=True``).
35173492
"""
35183493
ax, plt = self._ax_plt_from_kw(kwargs)
3519-
# allow callers to override the hold state by passing hold=True|False
3520-
b = ax.ishold()
3521-
h = kwargs.pop('hold',None)
3522-
if h is not None:
3523-
ax.hold(h)
3494+
self._save_use_hold(ax, kwargs)
35243495
try:
35253496
if kwargs.pop('tri', False):
35263497
try:
@@ -3581,10 +3552,8 @@ def contour(self,x,y,data,*args,**kwargs):
35813552
mask = np.logical_or(ma.getmaskarray(data),xymask)
35823553
data = ma.masked_array(data,mask=mask)
35833554
CS = ax.contour(x,y,data,*args,**kwargs)
3584-
except:
3585-
ax.hold(b)
3586-
raise
3587-
ax.hold(b)
3555+
finally:
3556+
self._restore_hold(ax)
35883557
# reset current active image (only if pyplot is imported).
35893558
if plt and CS.get_array() is not None:
35903559
plt.sci(CS)
@@ -3619,11 +3588,7 @@ def contourf(self,x,y,data,*args,**kwargs):
36193588
(or tricontourf if ``tri=True``).
36203589
"""
36213590
ax, plt = self._ax_plt_from_kw(kwargs)
3622-
# allow callers to override the hold state by passing hold=True|False
3623-
b = ax.ishold()
3624-
h = kwargs.pop('hold',None)
3625-
if h is not None:
3626-
ax.hold(h)
3591+
self._save_use_hold(ax, kwargs)
36273592
try:
36283593
if kwargs.get('tri', False):
36293594
try:
@@ -3686,10 +3651,8 @@ def contourf(self,x,y,data,*args,**kwargs):
36863651
mask = np.logical_or(ma.getmaskarray(data),xymask)
36873652
data = ma.masked_array(data,mask=mask)
36883653
CS = ax.contourf(x,y,data,*args,**kwargs)
3689-
except:
3690-
ax.hold(b)
3691-
raise
3692-
ax.hold(b)
3654+
finally:
3655+
self._restore_hold(ax)
36933656
# reset current active image (only if pyplot is imported).
36943657
if plt and CS.get_array() is not None:
36953658
plt.sci(CS)
@@ -3719,17 +3682,11 @@ def quiver(self, x, y, u, v, *args, **kwargs):
37193682
Other \*args and \**kwargs passed on to matplotlib.pyplot.quiver.
37203683
"""
37213684
ax, plt = self._ax_plt_from_kw(kwargs)
3722-
# allow callers to override the hold state by passing hold=True|False
3723-
b = ax.ishold()
3724-
h = kwargs.pop('hold',None)
3725-
if h is not None:
3726-
ax.hold(h)
3685+
self._save_use_hold(ax, kwargs)
37273686
try:
37283687
ret = ax.quiver(x,y,u,v,*args,**kwargs)
3729-
except:
3730-
ax.hold(b)
3731-
raise
3732-
ax.hold(b)
3688+
finally:
3689+
self._restore_hold(ax)
37333690
if plt is not None and ret.get_array() is not None:
37343691
plt.sci(ret)
37353692
# set axes limits to fit map region.
@@ -3761,17 +3718,11 @@ def streamplot(self, x, y, u, v, *args, **kwargs):
37613718
you have %s""" % _matplotlib_version)
37623719
raise NotImplementedError(msg)
37633720
ax, plt = self._ax_plt_from_kw(kwargs)
3764-
# allow callers to override the hold state by passing hold=True|False
3765-
b = ax.ishold()
3766-
h = kwargs.pop('hold',None)
3767-
if h is not None:
3768-
ax.hold(h)
3721+
self._save_use_hold(ax, kwargs)
37693722
try:
37703723
ret = ax.streamplot(x,y,u,v,*args,**kwargs)
3771-
except:
3772-
ax.hold(b)
3773-
raise
3774-
ax.hold(b)
3724+
finally:
3725+
self._restore_hold(ax)
37753726
if plt is not None and ret.lines.get_array() is not None:
37763727
plt.sci(ret.lines)
37773728
# set axes limits to fit map region.
@@ -3811,24 +3762,18 @@ def barbs(self, x, y, u, v, *args, **kwargs):
38113762
you have %s""" % _matplotlib_version)
38123763
raise NotImplementedError(msg)
38133764
ax, plt = self._ax_plt_from_kw(kwargs)
3814-
# allow callers to override the hold state by passing hold=True|False
3815-
b = ax.ishold()
3816-
h = kwargs.pop('hold',None)
3817-
if h is not None:
3818-
ax.hold(h)
38193765
lons, lats = self(x, y, inverse=True)
38203766
unh = ma.masked_where(lats <= 0, u)
38213767
vnh = ma.masked_where(lats <= 0, v)
38223768
ush = ma.masked_where(lats > 0, u)
38233769
vsh = ma.masked_where(lats > 0, v)
3770+
self._save_use_hold(ax, kwargs)
38243771
try:
38253772
retnh = ax.barbs(x,y,unh,vnh,*args,**kwargs)
38263773
kwargs['flip_barb']=True
38273774
retsh = ax.barbs(x,y,ush,vsh,*args,**kwargs)
3828-
except:
3829-
ax.hold(b)
3830-
raise
3831-
ax.hold(b)
3775+
finally:
3776+
self._restore_hold(ax)
38323777
# Because there are two collections returned in general,
38333778
# we can't set the current image...
38343779
#if plt is not None and ret.get_array() is not None:

0 commit comments

Comments
 (0)