@@ -219,7 +219,6 @@ def _set_cmap(self, cmap):
219
219
----------
220
220
cmap : `.Colormap` or str or None
221
221
"""
222
- # bury import to avoid circular imports
223
222
in_init = self ._cmap is None
224
223
cmap_obj = _ensure_cmap (cmap , accept_multivariate = True )
225
224
if not in_init and self .norm .n_components != cmap_obj .n_variates :
@@ -258,9 +257,6 @@ def set_clim(self, vmin=None, vmax=None):
258
257
vmin , vmax = vmin
259
258
except (TypeError , ValueError ):
260
259
pass
261
- # If the norm's limits are updated self.changed() will be called
262
- # through the callbacks attached to the norm, this causes an inconsistent
263
- # state, to prevent this blocked context manager is used
264
260
265
261
orig_vmin_vmax = self .norm .vmin , self .norm .vmax
266
262
@@ -472,24 +468,24 @@ def _format_cursor_data_override(self, data):
472
468
# Note if cm.ScalarMappable is depreciated, this functionality should be
473
469
# implemented as format_cursor_data() on ColorizingArtist.
474
470
if np .ma .getmask (data ) or data is None :
471
+ # NOTE: for multivariate data, if *any* of the fields are masked,
472
+ # "[]" is returned here
475
473
return "[]"
476
- if len (data .dtype .descr ) > 1 :
477
- # We have multivariate data encoded as a data type with multiple fields
478
- # NOTE: If any of the fields are masked, "[]" would be returned via
479
- # the if statement above.
480
- s_sig_digits_list = []
474
+
475
+ if isinstance (self .norm , colors .MultiNorm ):
476
+ norms = self .norm .norms
481
477
if isinstance (self .cmap , colors .BivarColormap ):
482
478
n_s = (self .cmap .N , self .cmap .M )
483
- else :
479
+ else : # colors.MultivarColormap
484
480
n_s = [part .N for part in self .cmap ]
485
- os = [f"{ d :-#.{self ._sig_digits_from_norm (no , d , n )}g} "
486
- for no , d , n in zip (self .norm .norms , data , n_s )]
487
- return f"[{ ', ' .join (os )} ]"
481
+ else : # colors.Colormap
482
+ norms = [self .norm ]
483
+ data = [data ]
484
+ n_s = [self .cmap .N ]
488
485
489
- # scalar data
490
- n = self .cmap .N
491
- g_sig_digits = self ._sig_digits_from_norm (self .norm , data , n )
492
- return f"[{ data :-#.{g_sig_digits }g} ]"
486
+ os = [f"{ d :-#.{self ._sig_digits_from_norm (no , d , n )}g} "
487
+ for no , d , n in zip (norms , data , n_s )]
488
+ return f"[{ ', ' .join (os )} ]"
493
489
494
490
@staticmethod
495
491
def _sig_digits_from_norm (norm , data , n ):
@@ -503,16 +499,13 @@ def _sig_digits_from_norm(norm, data, n):
503
499
cur_idx = np .argmin (np .abs (norm .boundaries - data ))
504
500
neigh_idx = max (0 , cur_idx - 1 )
505
501
# use max diff to prevent delta == 0
506
- delta = np .diff (
507
- norm .boundaries [neigh_idx :cur_idx + 2 ]
508
- ).max ()
502
+ delta = np .diff (norm .boundaries [neigh_idx :cur_idx + 2 ]).max ()
509
503
elif norm .vmin == norm .vmax :
510
504
# singular norms, use delta of 10% of only value
511
505
delta = np .abs (norm .vmin * .1 )
512
506
else :
513
507
# Midpoints of neighboring color intervals.
514
- neighbors = norm .inverse (
515
- (int (normed * n ) + np .array ([0 , 1 ])) / n )
508
+ neighbors = norm .inverse ((int (normed * n ) + np .array ([0 , 1 ])) / n )
516
509
delta = abs (neighbors - data ).max ()
517
510
518
511
g_sig_digits = cbook ._g_sig_digits (data , delta )
@@ -803,35 +796,28 @@ def _ensure_cmap(cmap, accept_multivariate=False):
803
796
- if a string, look it up in three corresponding databases
804
797
when not found: raise an error based on the expected shape
805
798
- if None, look up the default color map in mpl.colormaps
806
- accept_multivariate : bool, default True
799
+ accept_multivariate : bool, default False
807
800
- if False, accept only Colormap, string in mpl.colormaps or None
808
801
809
802
Returns
810
803
-------
811
804
Colormap
812
805
813
806
"""
814
- if not accept_multivariate :
815
- if isinstance (cmap , colors .Colormap ):
816
- return cmap
817
- cmap_name = cmap if cmap is not None else mpl .rcParams ["image.cmap" ]
818
- # use check_in_list to ensure type stability of the exception raised by
819
- # the internal usage of this (ValueError vs KeyError)
820
- if cmap_name not in mpl .colormaps :
821
- _api .check_in_list (sorted (mpl .colormaps ), cmap = cmap_name )
822
-
823
- if isinstance (cmap , (colors .Colormap ,
824
- colors .BivarColormap ,
825
- colors .MultivarColormap )):
807
+ if accept_multivariate :
808
+ types = (colors .Colormap , colors .BivarColormap , colors .MultivarColormap )
809
+ mappings = (mpl .colormaps , mpl .multivar_colormaps , mpl .bivar_colormaps )
810
+ else :
811
+ types = (colors .Colormap , )
812
+ mappings = (mpl .colormaps , )
813
+
814
+ if isinstance (cmap , types ):
826
815
return cmap
827
816
828
817
cmap_name = cmap if cmap is not None else mpl .rcParams ["image.cmap" ]
829
- if cmap_name in mpl .colormaps :
830
- return mpl .colormaps [cmap_name ]
831
- if cmap_name in mpl .multivar_colormaps :
832
- return mpl .multivar_colormaps [cmap_name ]
833
- if cmap_name in mpl .bivar_colormaps :
834
- return mpl .bivar_colormaps [cmap_name ]
818
+ for mapping in mappings :
819
+ if cmap_name in mapping :
820
+ return mapping [cmap_name ]
835
821
836
822
# this error message is a variant of _api.check_in_list but gives
837
823
# additional hints as to how to access multivariate colormaps
@@ -843,13 +829,6 @@ def _ensure_cmap(cmap, accept_multivariate=False):
843
829
" `matplotlib.multivar_colormaps()` for"
844
830
" bivariate and multivariate colormaps" )
845
831
846
- if isinstance (cmap , colors .Colormap ):
847
- return cmap
848
- cmap_name = cmap if cmap is not None else mpl .rcParams ["image.cmap" ]
849
- # use check_in_list to ensure type stability of the exception raised by
850
- # the internal usage of this (ValueError vs KeyError)
851
- if cmap_name not in cm .colormaps :
852
- _api .check_in_list (sorted (cm .colormaps ), cmap = cmap_name )
853
832
return cm .colormaps [cmap_name ]
854
833
855
834
@@ -881,6 +860,11 @@ def _ensure_multivariate_data(data, n_components):
881
860
# and already formatted data
882
861
return data
883
862
elif data .dtype in [np .complex64 , np .complex128 ]:
863
+ if n_components != 2 :
864
+ raise ValueError ("Invalid data entry for multivariate data. "
865
+ "Complex numbers are incompatible with "
866
+ f"{ n_components } variates." )
867
+
884
868
# pass complex data
885
869
if data .dtype == np .complex128 :
886
870
dt = np .dtype ('float64, float64' )
0 commit comments