@@ -451,39 +451,63 @@ def colorbar(self):
451
451
def colorbar (self , colorbar ):
452
452
self ._colorizer .colorbar = colorbar
453
453
454
- def _format_cursor_data_override (self , data ):
455
- # This function overwrites Artist.format_cursor_data(). We cannot
456
- # implement cm.ScalarMappable.format_cursor_data() directly, because
457
- # most cm.ScalarMappable subclasses inherit from Artist first and from
458
- # cm.ScalarMappable second, so Artist.format_cursor_data would always
459
- # have precedence over cm.ScalarMappable.format_cursor_data.
460
-
461
- # Note if cm.ScalarMappable is depreciated, this functionality should be
462
- # implemented as format_cursor_data() on ColorizingArtist.
463
- n = self .cmap .N
464
- if np .ma .getmask (data ):
465
- return "[]"
466
- normed = self .norm (data )
454
+ @staticmethod
455
+ def _sig_digits_from_norm (norm , data , n ):
456
+ # Determines the number of significant digits
457
+ # to use for a number given a norm, and n, where n is the
458
+ # number of colors in the colormap.
459
+ normed = norm (data )
467
460
if np .isfinite (normed ):
468
- if isinstance (self . norm , colors .BoundaryNorm ):
461
+ if isinstance (norm , colors .BoundaryNorm ):
469
462
# not an invertible normalization mapping
470
- cur_idx = np .argmin (np .abs (self . norm .boundaries - data ))
463
+ cur_idx = np .argmin (np .abs (norm .boundaries - data ))
471
464
neigh_idx = max (0 , cur_idx - 1 )
472
465
# use max diff to prevent delta == 0
473
466
delta = np .diff (
474
- self . norm .boundaries [neigh_idx :cur_idx + 2 ]
467
+ norm .boundaries [neigh_idx :cur_idx + 2 ]
475
468
).max ()
476
- elif self . norm .vmin == self . norm .vmax :
469
+ elif norm .vmin == norm .vmax :
477
470
# singular norms, use delta of 10% of only value
478
- delta = np .abs (self . norm .vmin * .1 )
471
+ delta = np .abs (norm .vmin * .1 )
479
472
else :
480
473
# Midpoints of neighboring color intervals.
481
- neighbors = self . norm .inverse (
474
+ neighbors = norm .inverse (
482
475
(int (normed * n ) + np .array ([0 , 1 ])) / n )
483
476
delta = abs (neighbors - data ).max ()
484
477
g_sig_digits = cbook ._g_sig_digits (data , delta )
485
478
else :
486
479
g_sig_digits = 3 # Consistent with default below.
480
+ return g_sig_digits
481
+
482
+ def _format_cursor_data_override (self , data ):
483
+ # This function overwrites Artist.format_cursor_data(). We cannot
484
+ # implement cm.ScalarMappable.format_cursor_data() directly, because
485
+ # most cm.ScalarMappable subclasses inherit from Artist first and from
486
+ # cm.ScalarMappable second, so Artist.format_cursor_data would always
487
+ # have precedence over cm.ScalarMappable.format_cursor_data.
488
+
489
+ # Note if cm.ScalarMappable is depreciated, this functionality should be
490
+ # implemented as format_cursor_data() on ColorizingArtist.
491
+ if np .ma .getmask (data ) or data is None :
492
+ return "[]"
493
+ if len (data .dtype .descr ) > 1 :
494
+ # We have multivariate data encoded as a data type with multiple fields
495
+ # NOTE: If any of the fields are masked, "[]" would be returned via
496
+ # the if statement above.
497
+ s_sig_digits_list = []
498
+ if isinstance (self .cmap , colors .BivarColormap ):
499
+ n_s = (self .cmap .N , self .cmap .M )
500
+ else :
501
+ n_s = [part .N for part in self .cmap ]
502
+ os = [f"{ d :-#.{self ._sig_digits_from_norm (no , d , n )}g} "
503
+ for no , d , n in zip (self .norm .norms , data , n_s )]
504
+ return f"[{ ', ' .join (os )} ]"
505
+
506
+ # scalar data
507
+ n = self .cmap .N
508
+ g_sig_digits = self ._sig_digits_from_norm (self .norm ,
509
+ data ,
510
+ n )
487
511
return f"[{ data :-#.{g_sig_digits }g} ]"
488
512
489
513
0 commit comments