@@ -3207,6 +3207,17 @@ def set_axes_limits(self,ax=None):
3207
3207
import matplotlib .pyplot as plt
3208
3208
plt .draw_if_interactive ()
3209
3209
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
+
3210
3221
@_transform1d
3211
3222
def scatter (self , * args , ** kwargs ):
3212
3223
"""
@@ -3225,17 +3236,11 @@ def scatter(self, *args, **kwargs):
3225
3236
Other \**kwargs passed on to matplotlib.pyplot.scatter.
3226
3237
"""
3227
3238
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 )
3233
3240
try :
3234
3241
ret = ax .scatter (* args , ** kwargs )
3235
- except :
3236
- ax .hold (b )
3237
- raise
3238
- ax .hold (b )
3242
+ finally :
3243
+ self ._restore_hold (ax )
3239
3244
# reset current active image (only if pyplot is imported).
3240
3245
if plt :
3241
3246
plt .sci (ret )
@@ -3263,17 +3268,11 @@ def plot(self, *args, **kwargs):
3263
3268
Other \**kwargs passed on to matplotlib.pyplot.plot.
3264
3269
"""
3265
3270
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 )
3271
3272
try :
3272
3273
ret = ax .plot (* args , ** kwargs )
3273
- except :
3274
- ax .hold (b )
3275
- raise
3276
- ax .hold (b )
3274
+ finally :
3275
+ self ._restore_hold (ax )
3277
3276
# set axes limits to fit map region.
3278
3277
self .set_axes_limits (ax = ax )
3279
3278
# clip to map limbs
@@ -3299,17 +3298,11 @@ def imshow(self, *args, **kwargs):
3299
3298
# use origin='lower', unless overridden.
3300
3299
if 'origin' not in kwargs :
3301
3300
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 )
3307
3302
try :
3308
3303
ret = ax .imshow (* args , ** kwargs )
3309
- except :
3310
- ax .hold (b )
3311
- raise
3312
- ax .hold (b )
3304
+ finally :
3305
+ self ._restore_hold (ax )
3313
3306
# reset current active image (only if pyplot is imported).
3314
3307
if plt :
3315
3308
plt .sci (ret )
@@ -3349,11 +3342,7 @@ def pcolor(self,x,y,data,**kwargs):
3349
3342
if the dimensions are the same, then the last row and column of data will be ignored.
3350
3343
"""
3351
3344
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 )
3357
3346
try :
3358
3347
if kwargs .pop ('tri' , False ):
3359
3348
try :
@@ -3386,10 +3375,8 @@ def pcolor(self,x,y,data,**kwargs):
3386
3375
x = ma .masked_values (np .where (x > 1.e20 ,1.e20 ,x ), 1.e20 )
3387
3376
y = ma .masked_values (np .where (y > 1.e20 ,1.e20 ,y ), 1.e20 )
3388
3377
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 )
3393
3380
# reset current active image (only if pyplot is imported).
3394
3381
if plt :
3395
3382
plt .sci (ret )
@@ -3424,17 +3411,11 @@ def pcolormesh(self,x,y,data,**kwargs):
3424
3411
if the dimensions are the same, then the last row and column of data will be ignored.
3425
3412
"""
3426
3413
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 )
3432
3415
try :
3433
3416
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 )
3438
3419
# reset current active image (only if pyplot is imported).
3439
3420
if plt :
3440
3421
plt .sci (ret )
@@ -3470,21 +3451,15 @@ def hexbin(self,x,y,**kwargs):
3470
3451
Other \**kwargs passed on to matplotlib.pyplot.hexbin
3471
3452
"""
3472
3453
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 )
3478
3455
try :
3479
3456
# make x,y masked arrays
3480
3457
# (masked where data is outside of projection limb)
3481
3458
x = ma .masked_values (np .where (x > 1.e20 ,1.e20 ,x ), 1.e20 )
3482
3459
y = ma .masked_values (np .where (y > 1.e20 ,1.e20 ,y ), 1.e20 )
3483
3460
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 )
3488
3463
# reset current active image (only if pyplot is imported).
3489
3464
if plt :
3490
3465
plt .sci (ret )
@@ -3516,11 +3491,7 @@ def contour(self,x,y,data,*args,**kwargs):
3516
3491
(or tricontour if ``tri=True``).
3517
3492
"""
3518
3493
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 )
3524
3495
try :
3525
3496
if kwargs .pop ('tri' , False ):
3526
3497
try :
@@ -3581,10 +3552,8 @@ def contour(self,x,y,data,*args,**kwargs):
3581
3552
mask = np .logical_or (ma .getmaskarray (data ),xymask )
3582
3553
data = ma .masked_array (data ,mask = mask )
3583
3554
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 )
3588
3557
# reset current active image (only if pyplot is imported).
3589
3558
if plt and CS .get_array () is not None :
3590
3559
plt .sci (CS )
@@ -3619,11 +3588,7 @@ def contourf(self,x,y,data,*args,**kwargs):
3619
3588
(or tricontourf if ``tri=True``).
3620
3589
"""
3621
3590
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 )
3627
3592
try :
3628
3593
if kwargs .get ('tri' , False ):
3629
3594
try :
@@ -3686,10 +3651,8 @@ def contourf(self,x,y,data,*args,**kwargs):
3686
3651
mask = np .logical_or (ma .getmaskarray (data ),xymask )
3687
3652
data = ma .masked_array (data ,mask = mask )
3688
3653
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 )
3693
3656
# reset current active image (only if pyplot is imported).
3694
3657
if plt and CS .get_array () is not None :
3695
3658
plt .sci (CS )
@@ -3719,17 +3682,11 @@ def quiver(self, x, y, u, v, *args, **kwargs):
3719
3682
Other \*args and \**kwargs passed on to matplotlib.pyplot.quiver.
3720
3683
"""
3721
3684
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 )
3727
3686
try :
3728
3687
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 )
3733
3690
if plt is not None and ret .get_array () is not None :
3734
3691
plt .sci (ret )
3735
3692
# set axes limits to fit map region.
@@ -3761,17 +3718,11 @@ def streamplot(self, x, y, u, v, *args, **kwargs):
3761
3718
you have %s""" % _matplotlib_version )
3762
3719
raise NotImplementedError (msg )
3763
3720
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 )
3769
3722
try :
3770
3723
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 )
3775
3726
if plt is not None and ret .lines .get_array () is not None :
3776
3727
plt .sci (ret .lines )
3777
3728
# set axes limits to fit map region.
@@ -3811,24 +3762,18 @@ def barbs(self, x, y, u, v, *args, **kwargs):
3811
3762
you have %s""" % _matplotlib_version )
3812
3763
raise NotImplementedError (msg )
3813
3764
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 )
3819
3765
lons , lats = self (x , y , inverse = True )
3820
3766
unh = ma .masked_where (lats <= 0 , u )
3821
3767
vnh = ma .masked_where (lats <= 0 , v )
3822
3768
ush = ma .masked_where (lats > 0 , u )
3823
3769
vsh = ma .masked_where (lats > 0 , v )
3770
+ self ._save_use_hold (ax , kwargs )
3824
3771
try :
3825
3772
retnh = ax .barbs (x ,y ,unh ,vnh ,* args ,** kwargs )
3826
3773
kwargs ['flip_barb' ]= True
3827
3774
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 )
3832
3777
# Because there are two collections returned in general,
3833
3778
# we can't set the current image...
3834
3779
#if plt is not None and ret.get_array() is not None:
0 commit comments