@@ -304,7 +304,47 @@ def tick_values(self, vmin, vmax):
304304 return ticks
305305
306306
307- class ColorbarBase (cm .ScalarMappable ):
307+ class _ColorbarMappableDummy (object ):
308+ """
309+ Private class to hold deprecated ColorbarBase methods that used to be
310+ inhereted from ScalarMappable.
311+ """
312+ @cbook .deprecated ("3.1" , alternative = "ScalarMappable.set_norm" )
313+ def set_norm (self , norm ):
314+ """
315+ `.colorbar.Colorbar.set_norm` does nothing; set the norm on
316+ the mappable associated with this colorbar.
317+ """
318+ pass
319+
320+ @cbook .deprecated ("3.1" , alternative = "ScalarMappable.set_cmap" )
321+ def set_cmap (self , cmap ):
322+ """
323+ `.colorbar.Colorbar.set_cmap` does nothing; set the norm on
324+ the mappable associated with this colorbar.
325+ """
326+ pass
327+
328+ @cbook .deprecated ("3.1" , alternative = "ScalarMappable.set_clim" )
329+ def set_clim (self , cmap ):
330+ """
331+ `.colorbar.Colorbar.set_clim` does nothing; set the limits on
332+ the mappable associated with this colorbar.
333+ """
334+ pass
335+
336+ @cbook .deprecated ("3.1" , alternative = "ScalarMappable.get_cmap" )
337+ def get_cmap (self ):
338+ 'return the colormap'
339+ return self .cmap
340+
341+ @cbook .deprecated ("3.1" , alternative = "ScalarMappable.get_clim" )
342+ def get_clim (self ):
343+ 'return the min, max of the color limits for image scaling'
344+ return self .norm .vmin , self .norm .vmax
345+
346+
347+ class ColorbarBase (_ColorbarMappableDummy ):
308348 '''
309349 Draw a colorbar in an existing axes.
310350
@@ -371,7 +411,8 @@ def __init__(self, ax, cmap=None,
371411 if norm is None :
372412 norm = colors .Normalize ()
373413 self .alpha = alpha
374- cm .ScalarMappable .__init__ (self , cmap = cmap , norm = norm )
414+ self .cmap = cmap
415+ self .norm = norm
375416 self .values = values
376417 self .boundaries = boundaries
377418 self .extend = extend
@@ -387,30 +428,26 @@ def __init__(self, ax, cmap=None,
387428 self .outline = None
388429 self .patch = None
389430 self .dividers = None
431+ self .locator = None
432+ self .formatter = None
390433 self ._manual_tick_data_values = None
391434
392435 if ticklocation == 'auto' :
393436 ticklocation = 'bottom' if orientation == 'horizontal' else 'right'
394437 self .ticklocation = ticklocation
395438
396439 self .set_label (label )
440+ self ._reset_locator_formatter_scale ()
441+
397442 if np .iterable (ticks ):
398443 self .locator = ticker .FixedLocator (ticks , nbins = len (ticks ))
399444 else :
400445 self .locator = ticks # Handle default in _ticker()
401- if format is None :
402- if isinstance (self .norm , colors .LogNorm ):
403- self .formatter = ticker .LogFormatterSciNotation ()
404- elif isinstance (self .norm , colors .SymLogNorm ):
405- self .formatter = ticker .LogFormatterSciNotation (
406- linthresh = self .norm .linthresh )
407- else :
408- self .formatter = ticker .ScalarFormatter ()
409- elif isinstance (format , str ):
446+
447+ if isinstance (format , str ):
410448 self .formatter = ticker .FormatStrFormatter (format )
411449 else :
412- self .formatter = format # Assume it is a Formatter
413- # The rest is in a method so we can recalculate when clim changes.
450+ self .formatter = format # Assume it is a Formatter or None
414451 self .draw_all ()
415452
416453 def _extend_lower (self ):
@@ -432,7 +469,6 @@ def draw_all(self):
432469 Calculate any free parameters based on the current cmap and norm,
433470 and do all the drawing.
434471 '''
435-
436472 # sets self._boundaries and self._values in real data units.
437473 # takes into account extend values:
438474 self ._process_values ()
@@ -451,12 +487,6 @@ def draw_all(self):
451487
452488 def config_axis (self ):
453489 ax = self .ax
454- if (isinstance (self .norm , colors .LogNorm )
455- and self ._use_auto_colorbar_locator ()):
456- # *both* axes are made log so that determining the
457- # mid point is easier.
458- ax .set_xscale ('log' )
459- ax .set_yscale ('log' )
460490
461491 if self .orientation == 'vertical' :
462492 long_axis , short_axis = ax .yaxis , ax .xaxis
@@ -504,6 +534,20 @@ def _get_ticker_locator_formatter(self):
504534 else :
505535 b = self ._boundaries [self ._inside ]
506536 locator = ticker .FixedLocator (b , nbins = 10 )
537+
538+ if formatter is None :
539+ if isinstance (self .norm , colors .LogNorm ):
540+ formatter = ticker .LogFormatterSciNotation ()
541+ elif isinstance (self .norm , colors .SymLogNorm ):
542+ formatter = ticker .LogFormatterSciNotation (
543+ linthresh = self .norm .linthresh )
544+ else :
545+ formatter = ticker .ScalarFormatter ()
546+ else :
547+ formatter = self .formatter
548+
549+ self .locator = locator
550+ self .formatter = formatter
507551 _log .debug ('locator: %r' , locator )
508552 return locator , formatter
509553
@@ -517,6 +561,24 @@ def _use_auto_colorbar_locator(self):
517561 and ((type (self .norm ) == colors .Normalize )
518562 or (type (self .norm ) == colors .LogNorm )))
519563
564+ def _reset_locator_formatter_scale (self ):
565+ """
566+ Reset the locator et al to defaults. Any user-hardcoded changes
567+ need to be re-entered if this gets called (either at init, or when
568+ the mappable normal gets changed: Colorbar.update_normal)
569+ """
570+ self .locator = None
571+ self .formatter = None
572+ if (isinstance (self .norm , colors .LogNorm )
573+ and self ._use_auto_colorbar_locator ()):
574+ # *both* axes are made log so that determining the
575+ # mid point is easier.
576+ self .ax .set_xscale ('log' )
577+ self .ax .set_yscale ('log' )
578+ else :
579+ self .ax .set_xscale ('linear' )
580+ self .ax .set_yscale ('linear' )
581+
520582 def update_ticks (self ):
521583 """
522584 Force the update of the ticks and ticklabels. This must be
@@ -526,7 +588,6 @@ def update_ticks(self):
526588 # get the locator and formatter. Defaults to
527589 # self.locator if not None..
528590 locator , formatter = self ._get_ticker_locator_formatter ()
529-
530591 if self .orientation == 'vertical' :
531592 long_axis , short_axis = ax .yaxis , ax .xaxis
532593 else :
@@ -1102,7 +1163,6 @@ def __init__(self, ax, mappable, **kw):
11021163 kw ['boundaries' ] = CS ._levels
11031164 kw ['values' ] = CS .cvalues
11041165 kw ['extend' ] = CS .extend
1105- #kw['ticks'] = CS._levels
11061166 kw .setdefault ('ticks' , ticker .FixedLocator (CS .levels , nbins = 10 ))
11071167 kw ['filled' ] = CS .filled
11081168 ColorbarBase .__init__ (self , ax , ** kw )
@@ -1125,8 +1185,7 @@ def on_mappable_changed(self, mappable):
11251185 by :func:`colorbar_factory` and should not be called manually.
11261186
11271187 """
1128- self .set_cmap (mappable .get_cmap ())
1129- self .set_clim (mappable .get_clim ())
1188+ _log .debug ('colorbar mappable changed' )
11301189 self .update_normal (mappable )
11311190
11321191 def add_lines (self , CS , erase = True ):
@@ -1156,9 +1215,24 @@ def update_normal(self, mappable):
11561215 Update solid patches, lines, etc.
11571216
11581217 Unlike `.update_bruteforce`, this does not clear the axes. This is
1159- meant to be called when the image or contour plot to which this
1160- colorbar belongs changes.
1218+ meant to be called when the norm of the image or contour plot to which
1219+ this colorbar belongs changes.
1220+
1221+ If the norm on the mappable is different than before, this resets the
1222+ locator and formatter for the axis, so if these have been customized,
1223+ they will need to be customized again. However, if the norm only
1224+ changes values of *vmin*, *vmax* or *cmap* then the old formatter
1225+ and locator will be preserved.
11611226 """
1227+
1228+ _log .debug ('colorbar update normal %r %r' , mappable .norm , self .norm )
1229+ self .mappable = mappable
1230+ self .set_alpha (mappable .get_alpha ())
1231+ self .cmap = mappable .cmap
1232+ if mappable .norm != self .norm :
1233+ self .norm = mappable .norm
1234+ self ._reset_locator_formatter_scale ()
1235+
11621236 self .draw_all ()
11631237 if isinstance (self .mappable , contour .ContourSet ):
11641238 CS = self .mappable
@@ -1180,15 +1254,16 @@ def update_bruteforce(self, mappable):
11801254 # properties have been changed by methods other than the
11811255 # colorbar methods, those changes will be lost.
11821256 self .ax .cla ()
1257+ self .locator = None
1258+ self .formatter = None
1259+
11831260 # clearing the axes will delete outline, patch, solids, and lines:
11841261 self .outline = None
11851262 self .patch = None
11861263 self .solids = None
11871264 self .lines = list ()
11881265 self .dividers = None
1189- self .set_alpha (mappable .get_alpha ())
1190- self .cmap = mappable .cmap
1191- self .norm = mappable .norm
1266+ self .update_normal (mappable )
11921267 self .draw_all ()
11931268 if isinstance (self .mappable , contour .ContourSet ):
11941269 CS = self .mappable
0 commit comments