Skip to content

Commit 91dcace

Browse files
committed
Simplify and future-proof the hold-handling
1 parent 800e9aa commit 91dcace

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
@@ -3206,6 +3206,17 @@ def set_axes_limits(self,ax=None):
32063206
import matplotlib.pyplot as plt
32073207
plt.draw_if_interactive()
32083208

3209+
3210+
def _save_use_hold(self, ax, kwargs):
3211+
h = kwargs.pop('hold', None)
3212+
if hasattr(ax, '_hold'):
3213+
self._tmp_hold = ax._hold
3214+
ax._hold = h
3215+
3216+
def _restore_hold(self, ax):
3217+
if hasattr(ax, '_hold'):
3218+
ax._hold = self._tmp_hold
3219+
32093220
@_transform1d
32103221
def scatter(self, *args, **kwargs):
32113222
"""
@@ -3224,17 +3235,11 @@ def scatter(self, *args, **kwargs):
32243235
Other \**kwargs passed on to matplotlib.pyplot.scatter.
32253236
"""
32263237
ax, plt = self._ax_plt_from_kw(kwargs)
3227-
# allow callers to override the hold state by passing hold=True|False
3228-
b = ax._hold
3229-
h = kwargs.pop('hold',None)
3230-
if h is not None:
3231-
ax._hold = h
3238+
self._save_use_hold(ax, kwargs)
32323239
try:
32333240
ret = ax.scatter(*args, **kwargs)
3234-
except:
3235-
ax._hold = b
3236-
raise
3237-
ax._hold = b
3241+
finally:
3242+
self._restore_hold(ax)
32383243
# reset current active image (only if pyplot is imported).
32393244
if plt:
32403245
plt.sci(ret)
@@ -3262,17 +3267,11 @@ def plot(self, *args, **kwargs):
32623267
Other \**kwargs passed on to matplotlib.pyplot.plot.
32633268
"""
32643269
ax = kwargs.pop('ax', None) or self._check_ax()
3265-
# allow callers to override the hold state by passing hold=True|False
3266-
b = ax._hold
3267-
h = kwargs.pop('hold',None)
3268-
if h is not None:
3269-
ax._hold = h
3270+
self._save_use_hold(ax, kwargs)
32703271
try:
32713272
ret = ax.plot(*args, **kwargs)
3272-
except:
3273-
ax._hold = b
3274-
raise
3275-
ax._hold = b
3273+
finally:
3274+
self._restore_hold(ax)
32763275
# set axes limits to fit map region.
32773276
self.set_axes_limits(ax=ax)
32783277
# clip to map limbs
@@ -3298,17 +3297,11 @@ def imshow(self, *args, **kwargs):
32983297
# use origin='lower', unless overridden.
32993298
if 'origin' not in kwargs:
33003299
kwargs['origin']='lower'
3301-
# allow callers to override the hold state by passing hold=True|False
3302-
b = ax._hold
3303-
h = kwargs.pop('hold',None)
3304-
if h is not None:
3305-
ax._hold = h
3300+
self._save_use_hold(ax, kwargs)
33063301
try:
33073302
ret = ax.imshow(*args, **kwargs)
3308-
except:
3309-
ax._hold = b
3310-
raise
3311-
ax._hold = b
3303+
finally:
3304+
self._restore_hold(ax)
33123305
# reset current active image (only if pyplot is imported).
33133306
if plt:
33143307
plt.sci(ret)
@@ -3348,11 +3341,7 @@ def pcolor(self,x,y,data,**kwargs):
33483341
if the dimensions are the same, then the last row and column of data will be ignored.
33493342
"""
33503343
ax, plt = self._ax_plt_from_kw(kwargs)
3351-
# allow callers to override the hold state by passing hold=True|False
3352-
b = ax._hold
3353-
h = kwargs.pop('hold',None)
3354-
if h is not None:
3355-
ax._hold = h
3344+
self._save_use_hold(ax, kwargs)
33563345
try:
33573346
if kwargs.pop('tri', False):
33583347
try:
@@ -3385,10 +3374,8 @@ def pcolor(self,x,y,data,**kwargs):
33853374
x = ma.masked_values(np.where(x > 1.e20,1.e20,x), 1.e20)
33863375
y = ma.masked_values(np.where(y > 1.e20,1.e20,y), 1.e20)
33873376
ret = ax.pcolor(x,y,data,**kwargs)
3388-
except:
3389-
ax._hold = b
3390-
raise
3391-
ax._hold = b
3377+
finally:
3378+
self._restore_hold(ax)
33923379
# reset current active image (only if pyplot is imported).
33933380
if plt:
33943381
plt.sci(ret)
@@ -3423,17 +3410,11 @@ def pcolormesh(self,x,y,data,**kwargs):
34233410
if the dimensions are the same, then the last row and column of data will be ignored.
34243411
"""
34253412
ax, plt = self._ax_plt_from_kw(kwargs)
3426-
# allow callers to override the hold state by passing hold=True|False
3427-
b = ax._hold
3428-
h = kwargs.pop('hold',None)
3429-
if h is not None:
3430-
ax._hold = h
3413+
self._save_use_hold(ax, kwargs)
34313414
try:
34323415
ret = ax.pcolormesh(x,y,data,**kwargs)
3433-
except:
3434-
ax._hold = b
3435-
raise
3436-
ax._hold = b
3416+
finally:
3417+
self._restore_hold(ax)
34373418
# reset current active image (only if pyplot is imported).
34383419
if plt:
34393420
plt.sci(ret)
@@ -3469,21 +3450,15 @@ def hexbin(self,x,y,**kwargs):
34693450
Other \**kwargs passed on to matplotlib.pyplot.hexbin
34703451
"""
34713452
ax, plt = self._ax_plt_from_kw(kwargs)
3472-
# allow callers to override the hold state by passing hold=True|False
3473-
b = ax._hold
3474-
h = kwargs.pop('hold',None)
3475-
if h is not None:
3476-
ax._hold = h
3453+
self._save_use_hold(ax, kwargs)
34773454
try:
34783455
# make x,y masked arrays
34793456
# (masked where data is outside of projection limb)
34803457
x = ma.masked_values(np.where(x > 1.e20,1.e20,x), 1.e20)
34813458
y = ma.masked_values(np.where(y > 1.e20,1.e20,y), 1.e20)
34823459
ret = ax.hexbin(x,y,**kwargs)
3483-
except:
3484-
ax._hold = b
3485-
raise
3486-
ax._hold = b
3460+
finally:
3461+
self._restore_hold(ax)
34873462
# reset current active image (only if pyplot is imported).
34883463
if plt:
34893464
plt.sci(ret)
@@ -3515,11 +3490,7 @@ def contour(self,x,y,data,*args,**kwargs):
35153490
(or tricontour if ``tri=True``).
35163491
"""
35173492
ax, plt = self._ax_plt_from_kw(kwargs)
3518-
# allow callers to override the hold state by passing hold=True|False
3519-
b = ax._hold
3520-
h = kwargs.pop('hold',None)
3521-
if h is not None:
3522-
ax._hold = h
3493+
self._save_use_hold(ax, kwargs)
35233494
try:
35243495
if kwargs.pop('tri', False):
35253496
try:
@@ -3580,10 +3551,8 @@ def contour(self,x,y,data,*args,**kwargs):
35803551
mask = np.logical_or(ma.getmaskarray(data),xymask)
35813552
data = ma.masked_array(data,mask=mask)
35823553
CS = ax.contour(x,y,data,*args,**kwargs)
3583-
except:
3584-
ax._hold = b
3585-
raise
3586-
ax._hold = b
3554+
finally:
3555+
self._restore_hold(ax)
35873556
# reset current active image (only if pyplot is imported).
35883557
if plt and CS.get_array() is not None:
35893558
plt.sci(CS)
@@ -3618,11 +3587,7 @@ def contourf(self,x,y,data,*args,**kwargs):
36183587
(or tricontourf if ``tri=True``).
36193588
"""
36203589
ax, plt = self._ax_plt_from_kw(kwargs)
3621-
# allow callers to override the hold state by passing hold=True|False
3622-
b = ax._hold
3623-
h = kwargs.pop('hold',None)
3624-
if h is not None:
3625-
ax._hold = h
3590+
self._save_use_hold(ax, kwargs)
36263591
try:
36273592
if kwargs.get('tri', False):
36283593
try:
@@ -3685,10 +3650,8 @@ def contourf(self,x,y,data,*args,**kwargs):
36853650
mask = np.logical_or(ma.getmaskarray(data),xymask)
36863651
data = ma.masked_array(data,mask=mask)
36873652
CS = ax.contourf(x,y,data,*args,**kwargs)
3688-
except:
3689-
ax._hold = b
3690-
raise
3691-
ax._hold = b
3653+
finally:
3654+
self._restore_hold(ax)
36923655
# reset current active image (only if pyplot is imported).
36933656
if plt and CS.get_array() is not None:
36943657
plt.sci(CS)
@@ -3717,17 +3680,11 @@ def quiver(self, x, y, u, v, *args, **kwargs):
37173680
Other \*args and \**kwargs passed on to matplotlib.pyplot.quiver.
37183681
"""
37193682
ax, plt = self._ax_plt_from_kw(kwargs)
3720-
# allow callers to override the hold state by passing hold=True|False
3721-
b = ax._hold
3722-
h = kwargs.pop('hold',None)
3723-
if h is not None:
3724-
ax._hold = h
3683+
self._save_use_hold(ax, kwargs)
37253684
try:
37263685
ret = ax.quiver(x,y,u,v,*args,**kwargs)
3727-
except:
3728-
ax._hold = b
3729-
raise
3730-
ax._hold = b
3686+
finally:
3687+
self._restore_hold(ax)
37313688
if plt is not None and ret.get_array() is not None:
37323689
plt.sci(ret)
37333690
# set axes limits to fit map region.
@@ -3759,17 +3716,11 @@ def streamplot(self, x, y, u, v, *args, **kwargs):
37593716
you have %s""" % _matplotlib_version)
37603717
raise NotImplementedError(msg)
37613718
ax, plt = self._ax_plt_from_kw(kwargs)
3762-
# allow callers to override the hold state by passing hold=True|False
3763-
b = ax._hold
3764-
h = kwargs.pop('hold',None)
3765-
if h is not None:
3766-
ax._hold = h
3719+
self._save_use_hold(ax, kwargs)
37673720
try:
37683721
ret = ax.streamplot(x,y,u,v,*args,**kwargs)
3769-
except:
3770-
ax._hold = b
3771-
raise
3772-
ax._hold = b
3722+
finally:
3723+
self._restore_hold(ax)
37733724
if plt is not None and ret.lines.get_array() is not None:
37743725
plt.sci(ret.lines)
37753726
# set axes limits to fit map region.
@@ -3809,24 +3760,18 @@ def barbs(self, x, y, u, v, *args, **kwargs):
38093760
you have %s""" % _matplotlib_version)
38103761
raise NotImplementedError(msg)
38113762
ax, plt = self._ax_plt_from_kw(kwargs)
3812-
# allow callers to override the hold state by passing hold=True|False
3813-
b = ax._hold
3814-
h = kwargs.pop('hold',None)
3815-
if h is not None:
3816-
ax._hold = h
38173763
lons, lats = self(x, y, inverse=True)
38183764
unh = ma.masked_where(lats <= 0, u)
38193765
vnh = ma.masked_where(lats <= 0, v)
38203766
ush = ma.masked_where(lats > 0, u)
38213767
vsh = ma.masked_where(lats > 0, v)
3768+
self._save_use_hold(ax, kwargs)
38223769
try:
38233770
retnh = ax.barbs(x,y,unh,vnh,*args,**kwargs)
38243771
kwargs['flip_barb']=True
38253772
retsh = ax.barbs(x,y,ush,vsh,*args,**kwargs)
3826-
except:
3827-
ax._hold = b
3828-
raise
3829-
ax._hold = b
3773+
finally:
3774+
self._restore_hold(ax)
38303775
# Because there are two collections returned in general,
38313776
# we can't set the current image...
38323777
#if plt is not None and ret.get_array() is not None:

0 commit comments

Comments
 (0)