@@ -3206,6 +3206,17 @@ def set_axes_limits(self,ax=None):
3206
3206
import matplotlib .pyplot as plt
3207
3207
plt .draw_if_interactive ()
3208
3208
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
+
3209
3220
@_transform1d
3210
3221
def scatter (self , * args , ** kwargs ):
3211
3222
"""
@@ -3224,17 +3235,11 @@ def scatter(self, *args, **kwargs):
3224
3235
Other \**kwargs passed on to matplotlib.pyplot.scatter.
3225
3236
"""
3226
3237
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 )
3232
3239
try :
3233
3240
ret = ax .scatter (* args , ** kwargs )
3234
- except :
3235
- ax ._hold = b
3236
- raise
3237
- ax ._hold = b
3241
+ finally :
3242
+ self ._restore_hold (ax )
3238
3243
# reset current active image (only if pyplot is imported).
3239
3244
if plt :
3240
3245
plt .sci (ret )
@@ -3262,17 +3267,11 @@ def plot(self, *args, **kwargs):
3262
3267
Other \**kwargs passed on to matplotlib.pyplot.plot.
3263
3268
"""
3264
3269
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 )
3270
3271
try :
3271
3272
ret = ax .plot (* args , ** kwargs )
3272
- except :
3273
- ax ._hold = b
3274
- raise
3275
- ax ._hold = b
3273
+ finally :
3274
+ self ._restore_hold (ax )
3276
3275
# set axes limits to fit map region.
3277
3276
self .set_axes_limits (ax = ax )
3278
3277
# clip to map limbs
@@ -3298,17 +3297,11 @@ def imshow(self, *args, **kwargs):
3298
3297
# use origin='lower', unless overridden.
3299
3298
if 'origin' not in kwargs :
3300
3299
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 )
3306
3301
try :
3307
3302
ret = ax .imshow (* args , ** kwargs )
3308
- except :
3309
- ax ._hold = b
3310
- raise
3311
- ax ._hold = b
3303
+ finally :
3304
+ self ._restore_hold (ax )
3312
3305
# reset current active image (only if pyplot is imported).
3313
3306
if plt :
3314
3307
plt .sci (ret )
@@ -3348,11 +3341,7 @@ def pcolor(self,x,y,data,**kwargs):
3348
3341
if the dimensions are the same, then the last row and column of data will be ignored.
3349
3342
"""
3350
3343
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 )
3356
3345
try :
3357
3346
if kwargs .pop ('tri' , False ):
3358
3347
try :
@@ -3385,10 +3374,8 @@ def pcolor(self,x,y,data,**kwargs):
3385
3374
x = ma .masked_values (np .where (x > 1.e20 ,1.e20 ,x ), 1.e20 )
3386
3375
y = ma .masked_values (np .where (y > 1.e20 ,1.e20 ,y ), 1.e20 )
3387
3376
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 )
3392
3379
# reset current active image (only if pyplot is imported).
3393
3380
if plt :
3394
3381
plt .sci (ret )
@@ -3423,17 +3410,11 @@ def pcolormesh(self,x,y,data,**kwargs):
3423
3410
if the dimensions are the same, then the last row and column of data will be ignored.
3424
3411
"""
3425
3412
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 )
3431
3414
try :
3432
3415
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 )
3437
3418
# reset current active image (only if pyplot is imported).
3438
3419
if plt :
3439
3420
plt .sci (ret )
@@ -3469,21 +3450,15 @@ def hexbin(self,x,y,**kwargs):
3469
3450
Other \**kwargs passed on to matplotlib.pyplot.hexbin
3470
3451
"""
3471
3452
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 )
3477
3454
try :
3478
3455
# make x,y masked arrays
3479
3456
# (masked where data is outside of projection limb)
3480
3457
x = ma .masked_values (np .where (x > 1.e20 ,1.e20 ,x ), 1.e20 )
3481
3458
y = ma .masked_values (np .where (y > 1.e20 ,1.e20 ,y ), 1.e20 )
3482
3459
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 )
3487
3462
# reset current active image (only if pyplot is imported).
3488
3463
if plt :
3489
3464
plt .sci (ret )
@@ -3515,11 +3490,7 @@ def contour(self,x,y,data,*args,**kwargs):
3515
3490
(or tricontour if ``tri=True``).
3516
3491
"""
3517
3492
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 )
3523
3494
try :
3524
3495
if kwargs .pop ('tri' , False ):
3525
3496
try :
@@ -3580,10 +3551,8 @@ def contour(self,x,y,data,*args,**kwargs):
3580
3551
mask = np .logical_or (ma .getmaskarray (data ),xymask )
3581
3552
data = ma .masked_array (data ,mask = mask )
3582
3553
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 )
3587
3556
# reset current active image (only if pyplot is imported).
3588
3557
if plt and CS .get_array () is not None :
3589
3558
plt .sci (CS )
@@ -3618,11 +3587,7 @@ def contourf(self,x,y,data,*args,**kwargs):
3618
3587
(or tricontourf if ``tri=True``).
3619
3588
"""
3620
3589
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 )
3626
3591
try :
3627
3592
if kwargs .get ('tri' , False ):
3628
3593
try :
@@ -3685,10 +3650,8 @@ def contourf(self,x,y,data,*args,**kwargs):
3685
3650
mask = np .logical_or (ma .getmaskarray (data ),xymask )
3686
3651
data = ma .masked_array (data ,mask = mask )
3687
3652
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 )
3692
3655
# reset current active image (only if pyplot is imported).
3693
3656
if plt and CS .get_array () is not None :
3694
3657
plt .sci (CS )
@@ -3717,17 +3680,11 @@ def quiver(self, x, y, u, v, *args, **kwargs):
3717
3680
Other \*args and \**kwargs passed on to matplotlib.pyplot.quiver.
3718
3681
"""
3719
3682
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 )
3725
3684
try :
3726
3685
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 )
3731
3688
if plt is not None and ret .get_array () is not None :
3732
3689
plt .sci (ret )
3733
3690
# set axes limits to fit map region.
@@ -3759,17 +3716,11 @@ def streamplot(self, x, y, u, v, *args, **kwargs):
3759
3716
you have %s""" % _matplotlib_version )
3760
3717
raise NotImplementedError (msg )
3761
3718
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 )
3767
3720
try :
3768
3721
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 )
3773
3724
if plt is not None and ret .lines .get_array () is not None :
3774
3725
plt .sci (ret .lines )
3775
3726
# set axes limits to fit map region.
@@ -3809,24 +3760,18 @@ def barbs(self, x, y, u, v, *args, **kwargs):
3809
3760
you have %s""" % _matplotlib_version )
3810
3761
raise NotImplementedError (msg )
3811
3762
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
3817
3763
lons , lats = self (x , y , inverse = True )
3818
3764
unh = ma .masked_where (lats <= 0 , u )
3819
3765
vnh = ma .masked_where (lats <= 0 , v )
3820
3766
ush = ma .masked_where (lats > 0 , u )
3821
3767
vsh = ma .masked_where (lats > 0 , v )
3768
+ self ._save_use_hold (ax , kwargs )
3822
3769
try :
3823
3770
retnh = ax .barbs (x ,y ,unh ,vnh ,* args ,** kwargs )
3824
3771
kwargs ['flip_barb' ]= True
3825
3772
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 )
3830
3775
# Because there are two collections returned in general,
3831
3776
# we can't set the current image...
3832
3777
#if plt is not None and ret.get_array() is not None:
0 commit comments