forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcm.py
More file actions
1320 lines (1099 loc) · 46.8 KB
/
cm.py
File metadata and controls
1320 lines (1099 loc) · 46.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Builtin colormaps, colormap handling utilities, and the `VectorMappable` and
`ScalarMappable` mixin.
.. seealso::
:doc:`/gallery/color/colormap_reference` for a list of builtin colormaps.
:ref:`colormap-manipulation` for examples of how to make
colormaps.
:ref:`colormaps` an in-depth discussion of choosing
colormaps.
:ref:`colormapnorms` for more details about data normalization.
"""
from collections.abc import Mapping
import functools
import numpy as np
from numpy import ma
import numbers
import matplotlib as mpl
from matplotlib import _api, colors, cbook, scale
from matplotlib._cm import datad
from matplotlib._cm_listed import cmaps as cmaps_listed
from matplotlib._cm_multivar import cmap_families as multivar_cmaps
from matplotlib._cm_bivar import cmaps as bivar_cmaps
from typing import Union
ColorMapType = Union[colors.Colormap,
colors.BivarColormap,
colors.MultivarColormap]
_LUTSIZE = mpl.rcParams['image.lut']
def _gen_cmap_registry():
"""
Generate a dict mapping standard colormap names to standard colormaps, as
well as the reversed colormaps.
"""
cmap_d = {**cmaps_listed}
for name, spec in datad.items():
cmap_d[name] = ( # Precache the cmaps at a fixed lutsize..
colors.LinearSegmentedColormap(name, spec, _LUTSIZE)
if 'red' in spec else
colors.ListedColormap(spec['listed'], name)
if 'listed' in spec else
colors.LinearSegmentedColormap.from_list(name, spec, _LUTSIZE))
# Register colormap aliases for gray and grey.
aliases = {
# alias -> original name
'grey': 'gray',
'gist_grey': 'gist_gray',
'gist_yerg': 'gist_yarg',
'Grays': 'Greys',
}
for alias, original_name in aliases.items():
cmap = cmap_d[original_name].copy()
cmap.name = alias
cmap_d[alias] = cmap
# Generate reversed cmaps.
for cmap in list(cmap_d.values()):
rmap = cmap.reversed()
cmap_d[rmap.name] = rmap
return cmap_d
class ColormapRegistry(Mapping):
r"""
Container for colormaps that are known to Matplotlib by name.
The universal registry instance is `matplotlib.colormaps`. There should be
no need for users to instantiate `.ColormapRegistry` themselves.
Read access uses a dict-like interface mapping names to `.Colormap`\s::
import matplotlib as mpl
cmap = mpl.colormaps['viridis']
Returned `.Colormap`\s are copies, so that their modification does not
change the global definition of the colormap.
Additional colormaps can be added via `.ColormapRegistry.register`::
mpl.colormaps.register(my_colormap)
To get a list of all registered colormaps, you can do::
from matplotlib import colormaps
list(colormaps)
"""
def __init__(self, cmaps):
self._cmaps = cmaps
self._builtin_cmaps = tuple(cmaps)
def __getitem__(self, item):
try:
return self._cmaps[item].copy()
except KeyError:
raise KeyError(f"{item!r} is not a known colormap name") from None
def __iter__(self):
return iter(self._cmaps)
def __len__(self):
return len(self._cmaps)
def __str__(self):
return ('ColormapRegistry; available colormaps:\n' +
', '.join(f"'{name}'" for name in self))
def __call__(self):
"""
Return a list of the registered colormap names.
This exists only for backward-compatibility in `.pyplot` which had a
``plt.colormaps()`` method. The recommended way to get this list is
now ``list(colormaps)``.
"""
return list(self)
def register(self, cmap, *, name=None, force=False):
"""
Register a new colormap.
The colormap name can then be used as a string argument to any ``cmap``
parameter in Matplotlib. It is also available in ``pyplot.get_cmap``.
The colormap registry stores a copy of the given colormap, so that
future changes to the original colormap instance do not affect the
registered colormap. Think of this as the registry taking a snapshot
of the colormap at registration.
Parameters
----------
cmap : matplotlib.colors.Colormap
The colormap to register.
name : str, optional
The name for the colormap. If not given, ``cmap.name`` is used.
force : bool, default: False
If False, a ValueError is raised if trying to overwrite an already
registered name. True supports overwriting registered colormaps
other than the builtin colormaps.
"""
_api.check_isinstance(colors.Colormap, cmap=cmap)
name = name or cmap.name
if name in self:
if not force:
# don't allow registering an already existing cmap
# unless explicitly asked to
raise ValueError(
f'A colormap named "{name}" is already registered.')
elif name in self._builtin_cmaps:
# We don't allow overriding a builtin.
raise ValueError("Re-registering the builtin cmap "
f"{name!r} is not allowed.")
# Warn that we are updating an already existing colormap
_api.warn_external(f"Overwriting the cmap {name!r} "
"that was already in the registry.")
self._cmaps[name] = cmap.copy()
# Someone may set the extremes of a builtin colormap and want to register it
# with a different name for future lookups. The object would still have the
# builtin name, so we should update it to the registered name
if self._cmaps[name].name != name:
self._cmaps[name].name = name
def unregister(self, name):
"""
Remove a colormap from the registry.
You cannot remove built-in colormaps.
If the named colormap is not registered, returns with no error, raises
if you try to de-register a default colormap.
.. warning::
Colormap names are currently a shared namespace that may be used
by multiple packages. Use `unregister` only if you know you
have registered that name before. In particular, do not
unregister just in case to clean the name before registering a
new colormap.
Parameters
----------
name : str
The name of the colormap to be removed.
Raises
------
ValueError
If you try to remove a default built-in colormap.
"""
if name in self._builtin_cmaps:
raise ValueError(f"cannot unregister {name!r} which is a builtin "
"colormap.")
self._cmaps.pop(name, None)
def get_cmap(self, cmap):
"""
Return a color map specified through *cmap*.
Parameters
----------
cmap : str or `~matplotlib.colors.Colormap` or None
- if a `.Colormap`, return it
- if a string, look it up in ``mpl.colormaps``
- if None, return the Colormap defined in :rc:`image.cmap`
Returns
-------
Colormap
"""
# get the default color map
if cmap is None:
return self[mpl.rcParams["image.cmap"]]
# if the user passed in a valid colormap type, simply return it
if isinstance(cmap, (colors.Colormap,
colors.BivarColormap,
colors.MultivarColormap)):
return cmap
if isinstance(cmap, str):
_api.check_in_list(sorted(_colormaps), cmap=cmap)
# otherwise, it must be a string so look it up
return self[cmap]
raise TypeError(
'get_cmap expects None or an instance of a str or Colormap . ' +
f'you passed {cmap!r} of type {type(cmap)}'
)
# public access to the colormaps should be via `matplotlib.colormaps`. For now,
# we still create the registry here, but that should stay an implementation
# detail.
_colormaps = ColormapRegistry(_gen_cmap_registry())
globals().update(_colormaps)
_multivar_colormaps = ColormapRegistry(multivar_cmaps)
globals().update(_multivar_colormaps)
_bivar_colormaps = ColormapRegistry(bivar_cmaps)
globals().update(_bivar_colormaps)
# This is an exact copy of pyplot.get_cmap(). It was removed in 3.9, but apparently
# caused more user trouble than expected. Re-added for 3.9.1 and extended the
# deprecation period for two additional minor releases.
@_api.deprecated(
'3.7',
removal='3.11',
alternative="``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap()``"
" or ``pyplot.get_cmap()``"
)
def get_cmap(name=None, lut=None):
"""
Get a colormap instance, defaulting to rc values if *name* is None.
Parameters
----------
name : `~matplotlib.colors.Colormap` or str or None, default: None
If a `.Colormap` instance, it will be returned. Otherwise, the name of
a colormap known to Matplotlib, which will be resampled by *lut*. The
default, None, means :rc:`image.cmap`.
lut : int or None, default: None
If *name* is not already a Colormap instance and *lut* is not None, the
colormap will be resampled to have *lut* entries in the lookup table.
Returns
-------
Colormap
"""
if name is None:
name = mpl.rcParams['image.cmap']
if isinstance(name, colors.Colormap):
return name
_api.check_in_list(sorted(_colormaps), name=name)
if lut is None:
return _colormaps[name]
else:
return _colormaps[name].resampled(lut)
def _auto_norm_from_scale(scale_cls):
"""
Automatically generate a norm class from *scale_cls*.
This differs from `.colors.make_norm_from_scale` in the following points:
- This function is not a class decorator, but directly returns a norm class
(as if decorating `.Normalize`).
- The scale is automatically constructed with ``nonpositive="mask"``, if it
supports such a parameter, to work around the difference in defaults
between standard scales (which use "clip") and norms (which use "mask").
Note that ``make_norm_from_scale`` caches the generated norm classes
(not the instances) and reuses them for later calls. For example,
``type(_auto_norm_from_scale("log")) == LogNorm``.
"""
# Actually try to construct an instance, to verify whether
# ``nonpositive="mask"`` is supported.
try:
norm = colors.make_norm_from_scale(
functools.partial(scale_cls, nonpositive="mask"))(
colors.Normalize)()
except TypeError:
norm = colors.make_norm_from_scale(scale_cls)(
colors.Normalize)()
return type(norm)
class Colorizer():
"""
Class that holds (multiple) norm and (one) colormap object.
"""
def __init__(self, cmap=None, norm=None):
self._cmap = None
self._set_cmap(cmap)
self._id_norm = [None] * self.cmap.n_variates
self._norm = [None] * self.cmap.n_variates
self.norm = norm # The Normalize instance of this Colorizer
self.callbacks = cbook.CallbackRegistry(signals=["changed"])
self.colorbar = None
# @property
# def n_variates(self):
# return self.cmap.n_variates
@property
def norm(self):
return self._norm
@norm.setter
def norm(self, norm):
norm = _ensure_multivariate_norm(self.cmap.n_variates, norm)
changed = False
for i, n in enumerate(norm):
_api.check_isinstance((colors.Normalize, str, None), norm=n)
if n is None:
n = colors.Normalize()
elif isinstance(n, str):
try:
scale_cls = scale._scale_mapping[n]
except KeyError:
raise ValueError(
"Invalid norm str name; the following values are "
f"supported: {', '.join(scale._scale_mapping)}"
) from None
n = _auto_norm_from_scale(scale_cls)()
if n is self._norm[i]:
continue
if self._norm[i] is not None:
# Remove the current callback and connect to the new one
self._norm[i].callbacks.disconnect(self._id_norm[i])
# emit changed if we are changing norm
# do not emit during initialization (self.norm[i] is None)
changed = True
self._norm[i] = n
self._id_norm[i] = self._norm[i].callbacks.connect('changed',
self.changed)
if changed:
self.changed()
def _scale_norm(self, norm, vmin, vmax, A):
"""
Helper for initial scaling.
Used by public functions that create a VectorMappable and support
parameters *vmin*, *vmax* and *norm*. This makes sure that a *norm*
will take precedence over *vmin*, *vmax*.
Note that this method does not set the norm.
"""
norm = _ensure_multivariate_norm(self.cmap.n_variates, norm)
vmin, vmax = _ensure_multivariate_clim(self.cmap.n_variates, vmin, vmax)
for i, _ in enumerate(self._norm):
if vmin[i] is not None or vmax[i] is not None:
if isinstance(norm[i], colors.Normalize):
raise ValueError(
"Passing a Normalize instance simultaneously with "
"vmin/vmax is not supported. Please pass vmin/vmax "
"directly to the norm when creating it.")
self._set_clim_i(i, vmin[i], vmax[i])
# always resolve the autoscaling so we have concrete limits
# rather than deferring to draw time.
self.autoscale_None(A)
def to_rgba(self, x, alpha=None, bytes=False, norm=True):
"""
Return a normalized RGBA array corresponding to *x*.
In the normal case, *x* is a 1D or 2D sequence of scalars, and
the corresponding `~numpy.ndarray` of RGBA values will be returned,
based on the norm and colormap set for this VectorMappable.
There is one special case, for handling images that are already
RGB or RGBA, such as might have been read from an image file.
If *x* is an `~numpy.ndarray` with 3 dimensions,
and the last dimension is either 3 or 4, then it will be
treated as an RGB or RGBA array, and no mapping will be done.
The array can be `~numpy.uint8`, or it can be floats with
values in the 0-1 range; otherwise a ValueError will be raised.
Any NaNs or masked elements will be set to 0 alpha.
If the last dimension is 3, the *alpha* kwarg (defaulting to 1)
will be used to fill in the transparency. If the last dimension
is 4, the *alpha* kwarg is ignored; it does not
replace the preexisting alpha. A ValueError will be raised
if the third dimension is other than 3 or 4.
In either case, if *bytes* is *False* (default), the RGBA
array will be floats in the 0-1 range; if it is *True*,
the returned RGBA array will be `~numpy.uint8` in the 0 to 255 range.
If norm is False, no normalization of the input data is
performed, and it is assumed to be in the range (0-1).
"""
# First check for special case, image input:
try:
if self.cmap.n_variates == 1 and x.ndim == 3:
# looks like imega data, try to process it without cmap
return self._pass_image_data(x, alpha, bytes, norm)
except AttributeError:
# e.g., x is not an ndarray; so try mapping it
pass
# This is the normal case, mapping a scalar/vector array:
if self.cmap.n_variates == 1:
x = ma.asarray(x)
if norm:
x = self.normalize(x)
rgba = self.cmap(x, alpha=alpha, bytes=bytes)
else:
if norm:
x = self.normalize(x)
rgba = self.cmap(x, alpha=alpha, bytes=bytes)
return rgba
@staticmethod
def _pass_image_data(x, alpha=None, bytes=False, norm=True):
"""
Helper function to pass ndarray of shape (...,3) or (..., 4)
through `to_rgba()`, see `to_rgba()` for docstring.
"""
if x.shape[2] == 3:
if alpha is None:
alpha = 1
if x.dtype == np.uint8:
alpha = np.uint8(alpha * 255)
m, n = x.shape[:2]
xx = np.empty(shape=(m, n, 4), dtype=x.dtype)
xx[:, :, :3] = x
xx[:, :, 3] = alpha
elif x.shape[2] == 4:
xx = x
else:
raise ValueError("Third dimension must be 3 or 4")
if xx.dtype.kind == 'f':
# If any of R, G, B, or A is nan, set to 0
if np.any(nans := np.isnan(x)):
if x.shape[2] == 4:
xx = xx.copy()
xx[np.any(nans, axis=2), :] = 0
if norm and (xx.max() > 1 or xx.min() < 0):
raise ValueError("Floating point image RGB values "
"must be in the 0..1 range.")
if bytes:
xx = (xx * 255).astype(np.uint8)
elif xx.dtype == np.uint8:
if not bytes:
xx = xx.astype(np.float32) / 255
else:
raise ValueError("Image RGB array must be uint8 or "
"floating point; found %s" % xx.dtype)
# Account for any masked entries in the original array
# If any of R, G, B, or A are masked for an entry, we set alpha to 0
if np.ma.is_masked(x):
xx[np.any(np.ma.getmaskarray(x), axis=2), 3] = 0
return xx
def normalize(self, x):
"""
Normalize the data in x.
Parameters
----------
x : np.array or sequence of arrays. Must be compatible with the number
of variates (`Colorizer.n_variates`).
- If there is a single norm, x may be of any shape.
- If there are two norms x may be a sequce of length 2, an array with
complex numbers, or an array with a dtype containing two fields
- If there more than two norms, x may be a sequce of length n, or an array
with a dtype containing n fields.
Returns
-------
np.array, or if more than one variate, a list of np.arrays.
"""
if self.cmap.n_variates == 1:
return self._norm[0](x)
elif hasattr(x, 'dtype') and len(x.dtype.descr) > 1:
x = _iterable_variates_in_data(x)
elif np.iscomplexobj(x):
# NOTE: when data is passed to plotting methods, i.e.
# imshow(data), and the data is complex, it is converted
# to a dtype with two fields.
# Therefore, complex data should only arrive here if
# the user invokes VectorMappable.to_rgba(data) or
# Colorizer.to_rgba(data) etc. with complex data directly.
x = [x.real, x.imag]
return [norm(xx) for norm, xx in zip(self._norm, x)]
def autoscale(self, A):
"""
Autoscale the scalar limits on the norm instance using the
current array
"""
if A is None:
raise TypeError('You must first set_array for mappable')
# If the norm's limits are updated self.changed() will be called
# through the callbacks attached to the norm
for n, a in zip(self._norm, _iterable_variates_in_data(A)):
n.autoscale(a)
def autoscale_None(self, A):
"""
Autoscale the scalar limits on the norm instance using the
current array, changing only limits that are None
"""
if A is None:
raise TypeError('You must first set_array for mappable')
# If the norm's limits are updated self.changed() will be called
# through the callbacks attached to the norm
for n, a in zip(self._norm, _iterable_variates_in_data(A)):
n.autoscale_None(a)
def _set_cmap(self, cmap):
"""
Set the colormap for luminance data.
Parameters
----------
cmap : `.Colormap` or str or None
"""
in_init = self._cmap is None
cmap = _ensure_cmap(cmap)
if not in_init:
if not cmap.n_variates == self.cmap.n_variates:
raise ValueError('The selected colormap does not have'
' the correct number of variates')
self._cmap = cmap
if not in_init:
self.changed() # Things are not set up properly yet.
@property
def cmap(self):
return self._cmap
@cmap.setter
def cmap(self, cmap):
self._set_cmap(cmap)
def _set_clim_i(self, i, vmin, vmax):
"""
Set the norm limits for the norm at index i
"""
if vmin is not None and vmax is not None:
# this block exists to avoid calling _changed twice.
vmin = colors._sanitize_extrema(vmin)
vmax = colors._sanitize_extrema(vmax)
if vmin != self._norm[i]._vmin or vmax != self._norm[i]._vmax:
self._norm[i]._vmin = vmin
self._norm[i]._vmax = vmax
self._norm[i]._changed()
else:
if vmin is not None:
self._norm[i].vmin = vmin
if vmax is not None:
self._norm[i].vmax = vmax
def set_clim(self, vmin=None, vmax=None):
"""
Set the norm limits for image scaling.
Parameters
----------
vmin, vmax : float
The limits.
.. ACCEPTS: (vmin: float, vmax: float)
"""
# If the norm's limits are updated self.changed() will be called
# through the callbacks attached to the norm
# we can add logic here to avoid multiple calls to self.changed() if the
# data is multivariate. This would be a minor efficiency improvement, but
# the use case of repeated calls to multivariate set_clim is limited,
# so the performance improvement is not prioritized at this moment.
vmin, vmax = _ensure_multivariate_clim(self.cmap.n_variates, vmin, vmax)
for i, _ in enumerate(self._norm):
self._set_clim_i(i, vmin[i], vmax[i])
def get_clim(self):
"""
Return the values (min, max) that are mapped to the colormap limits.
"""
return [n.vmin for n in self._norm], [n.vmax for n in self._norm]
def changed(self):
"""
Call this whenever the mappable is changed to notify all the
callbackSM listeners to the 'changed' signal.
"""
self.callbacks.process('changed')
self.stale = True
@property
def vmin(self):
return self.get_clim[0]
@vmin.setter
def vmin(self, vmin):
if not np.iterable(vmin):
vmin = [vmin]
self.set_clim(vmin=vmin)
@property
def vmax(self):
return self.get_clim[1]
@vmax.setter
def vmax(self, vmax):
if not np.iterable(vmax):
vmax = [vmax]
self.set_clim(vmax=vmax)
@property
def clip(self):
return [n.clip for n in self._norm]
@clip.setter
def clip(self, clip):
if not np.iterable(clip):
clip = [clip]*len(self._norm)
for n, c in zip(self._norm, clip):
n.clip = c
def __getitem__(self, index):
"""
Returns a Colorizer object containing the norm and colormap for one axis
"""
if self.cmap.n_variates > 1:
if index >= 0 and index < self.cmap.n_variates:
part = Colorizer(cmap=self._cmap[index], norm=self._norm[index])
part._super_colorizer = self
part._super_colorizer_index = index
part._id_parent_cmap = id(self.cmap)
part._id_parent_norm = id(self._norm[index])
self.callbacks.connect('changed', part._check_update_super_colorizer)
return part
elif self.cmap.n_variates == 1 and index == 0:
return self
raise ValueError(f'Only 0..{self.cmap.n_variates-1} are valid indexes'
' for this Colorizer object.')
def _check_update_super_colorizer(self):
"""
If this `Colorizer` object was created by __getitem__ it is a
one-dimensional component of another `Colorizer`.
In this case, `self._super_colorizer` is the Colorizer this was generated from.
This function propagetes changes from the `self._super_colorizer` to `self`.
"""
if hasattr(self, '_super_colorizer'):
# _super_colorizer, the colorizer this is a component of
if id(self._super_colorizer.cmap) != self._id_parent_cmap:
self.cmap = self._super_colorizer.cmap[self._super_colorizer_index]
super_colorizer_norm =\
self._super_colorizer._norm[self._super_colorizer_index]
if id(super_colorizer_norm) != self._id_parent_norm:
self.norm = [super_colorizer_norm]
def _format_cursor_data(self, data):
"""
Return a string representation of *data*.
Uses the colorbar's formatter to format the data.
"""
if (np.ndim(data) == 0 or len(data) == self.cmap.n_variates):
if self.cmap.n_variates == 1:
# This if test is equivalent to `isinstance(self.cmap, Colormap)`
data = [data]
num_colors = [self.cmap.N]
else:
if isinstance(self.cmap, colors.BivarColormap):
num_colors = [self.cmap.N, self.cmap.M]
else: # i.e. a MultivarColormap object
num_colors = [component.N for component in self.cmap]
out_str = '['
for nn, dd, nc in zip(self._norm, data, num_colors):
if np.ma.getmask(dd):
out_str += ", "
else:
# Figure out a reasonable amount of significant digits
normed = nn(dd)
if np.isfinite(normed):
if isinstance(nn, colors.BoundaryNorm):
# not an invertible normalization mapping
cur_idx = np.argmin(np.abs(nn.boundaries - dd))
neigh_idx = max(0, cur_idx - 1)
# use max diff to prevent delta == 0
delta = np.diff(
nn.boundaries[neigh_idx:cur_idx + 2]
).max()
else:
# Midpoints of neighboring color intervals.
neighbors = nn.inverse(
(int(normed * nc) + np.array([0, 1])) / nc)
delta = abs(neighbors - dd).max()
g_sig_digits = cbook._g_sig_digits(dd, delta)
else:
g_sig_digits = 3 # Consistent with default below.
out_str += f"{dd:-#.{g_sig_digits}g}, "
return out_str[:-2] + ']'
else:
# This point is reacehd if the number of colormaps does not match the length
# of data. This happens in the as-of-yet hypothetical case that the norm
# converts from one data field to two.
raise ValueError
try:
data[0]
except (TypeError, IndexError):
data = [data]
data_str = ', '.join(f'{item:0.3g}' for item in data
if isinstance(item, numbers.Number))
return "[" + data_str + "]"
class ColorizerShim:
def _scale_norm(self, norm, vmin, vmax):
self.colorizer._scale_norm(norm, vmin, vmax, self._A)
def to_rgba(self, x, alpha=None, bytes=False, norm=True):
"""
Return a normalized RGBA array corresponding to *x*.
In the normal case, *x* is a 1D or 2D sequence of scalars, and
the corresponding `~numpy.ndarray` of RGBA values will be returned,
based on the norm and colormap set for this VectorMappable.
There is one special case, for handling images that are already
RGB or RGBA, such as might have been read from an image file.
If *x* is an `~numpy.ndarray` with 3 dimensions,
and the last dimension is either 3 or 4, then it will be
treated as an RGB or RGBA array, and no mapping will be done.
The array can be `~numpy.uint8`, or it can be floats with
values in the 0-1 range; otherwise a ValueError will be raised.
Any NaNs or masked elements will be set to 0 alpha.
If the last dimension is 3, the *alpha* kwarg (defaulting to 1)
will be used to fill in the transparency. If the last dimension
is 4, the *alpha* kwarg is ignored; it does not
replace the preexisting alpha. A ValueError will be raised
if the third dimension is other than 3 or 4.
In either case, if *bytes* is *False* (default), the RGBA
array will be floats in the 0-1 range; if it is *True*,
the returned RGBA array will be `~numpy.uint8` in the 0 to 255 range.
If norm is False, no normalization of the input data is
performed, and it is assumed to be in the range (0-1).
"""
return self.colorizer.to_rgba(x, alpha=alpha, bytes=bytes, norm=norm)
def get_cmap(self):
"""Return the `.Colormap` instance."""
return self.colorizer.cmap
def get_clim(self):
"""
Return the values (min, max) that are mapped to the colormap limits.
"""
if self.colorizer.cmap.n_variates == 1:
return self.colorizer._norm[0].vmin, self.colorizer._norm[0].vmax
return self.colorizer.get_clim()
def set_clim(self, vmin=None, vmax=None):
"""
Set the norm limits for image scaling.
Parameters
----------
vmin, vmax : float
The limits.
For scalar data, the limits may also be passed as a
tuple (*vmin*, *vmax*) as a single positional argument.
.. ACCEPTS: (vmin: float, vmax: float)
"""
# If the norm's limits are updated self.changed() will be called
# through the callbacks attached to the norm
if self.cmap.n_variates == 1:
try:
vmin, vmax = vmin
except (TypeError, ValueError):
pass
self.colorizer.set_clim(vmin, vmax)
def get_alpha(self):
"""
Returns
-------
float
Always returns 1.
"""
# This method is intended to be overridden by Artist sub-classes
return 1.
@property
def cmap(self):
return self.colorizer.cmap
@cmap.setter
def cmap(self, cmap):
self.colorizer.cmap = cmap
def set_cmap(self, cmap):
"""
Set the colormap for luminance data.
Parameters
----------
cmap : `.Colormap` or str or None
"""
self.colorizer.cmap = cmap
@property
def norm(self):
if self.cmap.n_variates == 1:
return self.colorizer.norm[0]
return self.colorizer.norm
@norm.setter
def norm(self, norm):
self.colorizer.norm = norm
def set_norm(self, norm):
"""
Set the normalization instance.
Parameters
----------
norm : `.Normalize` or str or None
Notes
-----
If there are any colorbars using the mappable for this norm, setting
the norm of the mappable will reset the norm, locator, and formatters
on the colorbar to default.
"""
self.norm = norm
def autoscale(self):
"""
Autoscale the scalar limits on the norm instance using the
current array
"""
self.colorizer.autoscale(self._A)
def autoscale_None(self):
"""
Autoscale the scalar limits on the norm instance using the
current array, changing only limits that are None
"""
self.colorizer.autoscale_None(self._A)
def _parse_multivariate_data(self, data):
"""
Parse data to a dtype with self.cmap.n_variates.
Input data of shape (n_variates, n, m) is converted to an array of shape
(n, m) with data type np.dtype(f'{data.dtype}, ' * n_variates)
Complex data is returned as a view with dtype np.dtype('float64, float64')
or np.dtype('float32, float32')
If n_variates is 1 and data is not of type np.ndarray (i.e. PIL.Image),
the data is returned unchanged.
If data is None, the function returns None
Parameters
----------
data : np.ndarray, PIL.Image or None
Returns
-------
np.ndarray, PIL.Image or None
"""
return _ensure_multivariate_data(self.cmap.n_variates, data)
@property
def colorbar(self):
return self.colorizer.colorbar
@colorbar.setter
def colorbar(self, colorbar):
self.colorizer.colorbar = colorbar
class ScalarMappable(ColorizerShim):
"""
A mixin class to map one or multiple sets of scalar data to RGBA.
The VectorMappable applies data normalization before returning RGBA colors
from the given `~matplotlib.colors.Colormap`, `~matplotlib.colors.BivarColormap`,
or `~matplotlib.colors.MultivarColormap`.
"""
def __init__(self, norm=None, cmap=None):
"""
Parameters
----------
norm : `.Normalize` (or subclass thereof) or str or None
The normalizing object which scales data, typically into the
interval ``[0, 1]``.
If a `str`, a `.Normalize` subclass is dynamically generated based
on the scale with the corresponding name.
If *None*, *norm* defaults to a *colors.Normalize* object which
initializes its scaling based on the first data processed.
cmap : str or `~matplotlib.colors.Colormap`
The colormap used to map normalized data values to RGBA colors.
"""
self._A = None
if isinstance(norm, Colorizer):
self.colorizer = norm
else:
self.colorizer = Colorizer(cmap, norm)
self._id_colorizer = self.colorizer.callbacks.connect('changed', self.changed)
self.callbacks = cbook.CallbackRegistry(signals=["changed"])
self.get_alpha = lambda: 1
def set_array(self, A):
"""
Set the value array from array-like *A*.
Parameters
----------
A : array-like or None
The values that are mapped to colors.
The base class `.VectorMappable` does not make any assumptions on
the dimensionality and shape of the value array *A*.
"""
if A is None:
self._A = None
return
A = _ensure_multivariate_data(self.cmap.n_variates, A)
A = cbook.safe_masked_invalid(A, copy=True)
if not np.can_cast(A.dtype, float, "same_kind"):
if A.dtype.fields is None:
raise TypeError(f"Image data of dtype {A.dtype} cannot be "
f"converted to float")
else:
for key in A.dtype.fields:
if not np.can_cast(A[key].dtype, float, "same_kind"):
raise TypeError(f"Image data of dtype {A.dtype} cannot be "
f"converted to a sequence of floats")
self._A = A
self.colorizer.autoscale_None(A)