@@ -304,7 +304,47 @@ def tick_values(self, vmin, vmax):
304
304
return ticks
305
305
306
306
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 ):
308
348
'''
309
349
Draw a colorbar in an existing axes.
310
350
@@ -371,7 +411,8 @@ def __init__(self, ax, cmap=None,
371
411
if norm is None :
372
412
norm = colors .Normalize ()
373
413
self .alpha = alpha
374
- cm .ScalarMappable .__init__ (self , cmap = cmap , norm = norm )
414
+ self .cmap = cmap
415
+ self .norm = norm
375
416
self .values = values
376
417
self .boundaries = boundaries
377
418
self .extend = extend
@@ -387,30 +428,26 @@ def __init__(self, ax, cmap=None,
387
428
self .outline = None
388
429
self .patch = None
389
430
self .dividers = None
431
+ self .locator = None
432
+ self .formatter = None
390
433
self ._manual_tick_data_values = None
391
434
392
435
if ticklocation == 'auto' :
393
436
ticklocation = 'bottom' if orientation == 'horizontal' else 'right'
394
437
self .ticklocation = ticklocation
395
438
396
439
self .set_label (label )
440
+ self ._reset_locator_formatter_scale ()
441
+
397
442
if np .iterable (ticks ):
398
443
self .locator = ticker .FixedLocator (ticks , nbins = len (ticks ))
399
444
else :
400
445
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 ):
410
448
self .formatter = ticker .FormatStrFormatter (format )
411
449
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
414
451
self .draw_all ()
415
452
416
453
def _extend_lower (self ):
@@ -432,7 +469,6 @@ def draw_all(self):
432
469
Calculate any free parameters based on the current cmap and norm,
433
470
and do all the drawing.
434
471
'''
435
-
436
472
# sets self._boundaries and self._values in real data units.
437
473
# takes into account extend values:
438
474
self ._process_values ()
@@ -451,12 +487,6 @@ def draw_all(self):
451
487
452
488
def config_axis (self ):
453
489
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' )
460
490
461
491
if self .orientation == 'vertical' :
462
492
long_axis , short_axis = ax .yaxis , ax .xaxis
@@ -504,6 +534,20 @@ def _get_ticker_locator_formatter(self):
504
534
else :
505
535
b = self ._boundaries [self ._inside ]
506
536
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
507
551
_log .debug ('locator: %r' , locator )
508
552
return locator , formatter
509
553
@@ -517,6 +561,24 @@ def _use_auto_colorbar_locator(self):
517
561
and ((type (self .norm ) == colors .Normalize )
518
562
or (type (self .norm ) == colors .LogNorm )))
519
563
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
+
520
582
def update_ticks (self ):
521
583
"""
522
584
Force the update of the ticks and ticklabels. This must be
@@ -526,7 +588,6 @@ def update_ticks(self):
526
588
# get the locator and formatter. Defaults to
527
589
# self.locator if not None..
528
590
locator , formatter = self ._get_ticker_locator_formatter ()
529
-
530
591
if self .orientation == 'vertical' :
531
592
long_axis , short_axis = ax .yaxis , ax .xaxis
532
593
else :
@@ -1102,7 +1163,6 @@ def __init__(self, ax, mappable, **kw):
1102
1163
kw ['boundaries' ] = CS ._levels
1103
1164
kw ['values' ] = CS .cvalues
1104
1165
kw ['extend' ] = CS .extend
1105
- #kw['ticks'] = CS._levels
1106
1166
kw .setdefault ('ticks' , ticker .FixedLocator (CS .levels , nbins = 10 ))
1107
1167
kw ['filled' ] = CS .filled
1108
1168
ColorbarBase .__init__ (self , ax , ** kw )
@@ -1125,8 +1185,7 @@ def on_mappable_changed(self, mappable):
1125
1185
by :func:`colorbar_factory` and should not be called manually.
1126
1186
1127
1187
"""
1128
- self .set_cmap (mappable .get_cmap ())
1129
- self .set_clim (mappable .get_clim ())
1188
+ _log .debug ('colorbar mappable changed' )
1130
1189
self .update_normal (mappable )
1131
1190
1132
1191
def add_lines (self , CS , erase = True ):
@@ -1156,9 +1215,24 @@ def update_normal(self, mappable):
1156
1215
Update solid patches, lines, etc.
1157
1216
1158
1217
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.
1161
1226
"""
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
+
1162
1236
self .draw_all ()
1163
1237
if isinstance (self .mappable , contour .ContourSet ):
1164
1238
CS = self .mappable
@@ -1180,15 +1254,16 @@ def update_bruteforce(self, mappable):
1180
1254
# properties have been changed by methods other than the
1181
1255
# colorbar methods, those changes will be lost.
1182
1256
self .ax .cla ()
1257
+ self .locator = None
1258
+ self .formatter = None
1259
+
1183
1260
# clearing the axes will delete outline, patch, solids, and lines:
1184
1261
self .outline = None
1185
1262
self .patch = None
1186
1263
self .solids = None
1187
1264
self .lines = list ()
1188
1265
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 )
1192
1267
self .draw_all ()
1193
1268
if isinstance (self .mappable , contour .ContourSet ):
1194
1269
CS = self .mappable
0 commit comments