@@ -304,7 +304,51 @@ 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
+ """
339
+ return the colormap
340
+ """
341
+ return self .cmap
342
+
343
+ @cbook .deprecated ("3.1" , alternative = "ScalarMappable.get_clim" )
344
+ def get_clim (self ):
345
+ """
346
+ return the min, max of the color limits for image scaling
347
+ """
348
+ return self .norm .vmin , self .norm .vmax
349
+
350
+
351
+ class ColorbarBase (_ColorbarMappableDummy ):
308
352
'''
309
353
Draw a colorbar in an existing axes.
310
354
@@ -371,7 +415,8 @@ def __init__(self, ax, cmap=None,
371
415
if norm is None :
372
416
norm = colors .Normalize ()
373
417
self .alpha = alpha
374
- cm .ScalarMappable .__init__ (self , cmap = cmap , norm = norm )
418
+ self .cmap = cmap
419
+ self .norm = norm
375
420
self .values = values
376
421
self .boundaries = boundaries
377
422
self .extend = extend
@@ -387,30 +432,26 @@ def __init__(self, ax, cmap=None,
387
432
self .outline = None
388
433
self .patch = None
389
434
self .dividers = None
435
+ self .locator = None
436
+ self .formatter = None
390
437
self ._manual_tick_data_values = None
391
438
392
439
if ticklocation == 'auto' :
393
440
ticklocation = 'bottom' if orientation == 'horizontal' else 'right'
394
441
self .ticklocation = ticklocation
395
442
396
443
self .set_label (label )
444
+ self ._reset_locator_formatter_scale ()
445
+
397
446
if np .iterable (ticks ):
398
447
self .locator = ticker .FixedLocator (ticks , nbins = len (ticks ))
399
448
else :
400
449
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 ):
450
+
451
+ if isinstance (format , str ):
410
452
self .formatter = ticker .FormatStrFormatter (format )
411
453
else :
412
- self .formatter = format # Assume it is a Formatter
413
- # The rest is in a method so we can recalculate when clim changes.
454
+ self .formatter = format # Assume it is a Formatter or None
414
455
self .draw_all ()
415
456
416
457
def _extend_lower (self ):
@@ -432,7 +473,6 @@ def draw_all(self):
432
473
Calculate any free parameters based on the current cmap and norm,
433
474
and do all the drawing.
434
475
'''
435
-
436
476
# sets self._boundaries and self._values in real data units.
437
477
# takes into account extend values:
438
478
self ._process_values ()
@@ -451,12 +491,6 @@ def draw_all(self):
451
491
452
492
def config_axis (self ):
453
493
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
494
461
495
if self .orientation == 'vertical' :
462
496
long_axis , short_axis = ax .yaxis , ax .xaxis
@@ -504,6 +538,20 @@ def _get_ticker_locator_formatter(self):
504
538
else :
505
539
b = self ._boundaries [self ._inside ]
506
540
locator = ticker .FixedLocator (b , nbins = 10 )
541
+
542
+ if formatter is None :
543
+ if isinstance (self .norm , colors .LogNorm ):
544
+ formatter = ticker .LogFormatterSciNotation ()
545
+ elif isinstance (self .norm , colors .SymLogNorm ):
546
+ formatter = ticker .LogFormatterSciNotation (
547
+ linthresh = self .norm .linthresh )
548
+ else :
549
+ formatter = ticker .ScalarFormatter ()
550
+ else :
551
+ formatter = self .formatter
552
+
553
+ self .locator = locator
554
+ self .formatter = formatter
507
555
_log .debug ('locator: %r' , locator )
508
556
return locator , formatter
509
557
@@ -517,6 +565,24 @@ def _use_auto_colorbar_locator(self):
517
565
and ((type (self .norm ) == colors .Normalize )
518
566
or (type (self .norm ) == colors .LogNorm )))
519
567
568
+ def _reset_locator_formatter_scale (self ):
569
+ """
570
+ Reset the locator et al to defaults. Any user-hardcoded changes
571
+ need to be re-entered if this gets called (either at init, or when
572
+ the mappable normal gets changed: Colorbar.update_normal)
573
+ """
574
+ self .locator = None
575
+ self .formatter = None
576
+ if (isinstance (self .norm , colors .LogNorm )
577
+ and self ._use_auto_colorbar_locator ()):
578
+ # *both* axes are made log so that determining the
579
+ # mid point is easier.
580
+ self .ax .set_xscale ('log' )
581
+ self .ax .set_yscale ('log' )
582
+ else :
583
+ self .ax .set_xscale ('linear' )
584
+ self .ax .set_yscale ('linear' )
585
+
520
586
def update_ticks (self ):
521
587
"""
522
588
Force the update of the ticks and ticklabels. This must be
@@ -526,7 +592,6 @@ def update_ticks(self):
526
592
# get the locator and formatter. Defaults to
527
593
# self.locator if not None..
528
594
locator , formatter = self ._get_ticker_locator_formatter ()
529
-
530
595
if self .orientation == 'vertical' :
531
596
long_axis , short_axis = ax .yaxis , ax .xaxis
532
597
else :
@@ -1102,7 +1167,6 @@ def __init__(self, ax, mappable, **kw):
1102
1167
kw ['boundaries' ] = CS ._levels
1103
1168
kw ['values' ] = CS .cvalues
1104
1169
kw ['extend' ] = CS .extend
1105
- #kw['ticks'] = CS._levels
1106
1170
kw .setdefault ('ticks' , ticker .FixedLocator (CS .levels , nbins = 10 ))
1107
1171
kw ['filled' ] = CS .filled
1108
1172
ColorbarBase .__init__ (self , ax , ** kw )
@@ -1125,8 +1189,7 @@ def on_mappable_changed(self, mappable):
1125
1189
by :func:`colorbar_factory` and should not be called manually.
1126
1190
1127
1191
"""
1128
- self .set_cmap (mappable .get_cmap ())
1129
- self .set_clim (mappable .get_clim ())
1192
+ _log .debug ('colorbar mappable changed' )
1130
1193
self .update_normal (mappable )
1131
1194
1132
1195
def add_lines (self , CS , erase = True ):
@@ -1156,9 +1219,24 @@ def update_normal(self, mappable):
1156
1219
Update solid patches, lines, etc.
1157
1220
1158
1221
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.
1222
+ meant to be called when the norm of the image or contour plot to which
1223
+ this colorbar belongs changes.
1224
+
1225
+ If the norm on the mappable is different than before, this resets the
1226
+ locator and formatter for the axis, so if these have been customized,
1227
+ they will need to be customized again. However, if the norm only
1228
+ changes values of *vmin*, *vmax* or *cmap* then the old formatter
1229
+ and locator will be preserved.
1161
1230
"""
1231
+
1232
+ _log .debug ('colorbar update normal %r %r' , mappable .norm , self .norm )
1233
+ self .mappable = mappable
1234
+ self .set_alpha (mappable .get_alpha ())
1235
+ self .cmap = mappable .cmap
1236
+ if mappable .norm != self .norm :
1237
+ self .norm = mappable .norm
1238
+ self ._reset_locator_formatter_scale ()
1239
+
1162
1240
self .draw_all ()
1163
1241
if isinstance (self .mappable , contour .ContourSet ):
1164
1242
CS = self .mappable
@@ -1180,15 +1258,16 @@ def update_bruteforce(self, mappable):
1180
1258
# properties have been changed by methods other than the
1181
1259
# colorbar methods, those changes will be lost.
1182
1260
self .ax .cla ()
1261
+ self .locator = None
1262
+ self .formatter = None
1263
+
1183
1264
# clearing the axes will delete outline, patch, solids, and lines:
1184
1265
self .outline = None
1185
1266
self .patch = None
1186
1267
self .solids = None
1187
1268
self .lines = list ()
1188
1269
self .dividers = None
1189
- self .set_alpha (mappable .get_alpha ())
1190
- self .cmap = mappable .cmap
1191
- self .norm = mappable .norm
1270
+ self .update_normal (mappable )
1192
1271
self .draw_all ()
1193
1272
if isinstance (self .mappable , contour .ContourSet ):
1194
1273
CS = self .mappable
0 commit comments