diff --git a/lib/mpl_toolkits/basemap/__init__.py b/lib/mpl_toolkits/basemap/__init__.py index 89a39b398..52bab38c6 100644 --- a/lib/mpl_toolkits/basemap/__init__.py +++ b/lib/mpl_toolkits/basemap/__init__.py @@ -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 + + def _restore_hold(self, ax): + if hasattr(ax, '_hold'): + ax._hold = self._tmp_hold + @_transform1d def scatter(self, *args, **kwargs): """ @@ -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) @@ -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 @@ -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) @@ -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: @@ -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) @@ -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) @@ -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) @@ -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: @@ -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) @@ -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: @@ -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) @@ -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. @@ -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. @@ -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: