@@ -110,6 +110,8 @@ def __delitem__(self, key):
110
110
_colors_full_map .update (BASE_COLORS )
111
111
_colors_full_map = _ColorMapping (_colors_full_map )
112
112
113
+ _REPR_PNG_SIZE = (512 , 64 )
114
+
113
115
114
116
def get_named_colors_mapping ():
115
117
"""Return the global mapping of names to named colors."""
@@ -607,26 +609,40 @@ def __copy__(self):
607
609
cmapobject ._global = False
608
610
return cmapobject
609
611
612
+ def get_bad (self ):
613
+ """Get the color for masked values."""
614
+ if not self ._isinit :
615
+ self ._init ()
616
+ return self ._lut [self ._i_bad ]
617
+
610
618
def set_bad (self , color = 'k' , alpha = None ):
611
619
"""Set the color for masked values."""
612
620
_warn_if_global_cmap_modified (self )
613
621
self ._rgba_bad = to_rgba (color , alpha )
614
622
if self ._isinit :
615
623
self ._set_extremes ()
616
624
625
+ def get_under (self ):
626
+ """Get the color for low out-of-range values."""
627
+ if not self ._isinit :
628
+ self ._init ()
629
+ return self ._lut [self ._i_under ]
630
+
617
631
def set_under (self , color = 'k' , alpha = None ):
618
- """
619
- Set the color for low out-of-range values when ``norm.clip = False``.
620
- """
632
+ """Set the color for low out-of-range values."""
621
633
_warn_if_global_cmap_modified (self )
622
634
self ._rgba_under = to_rgba (color , alpha )
623
635
if self ._isinit :
624
636
self ._set_extremes ()
625
637
638
+ def get_over (self ):
639
+ """Get the color for high out-of-range values."""
640
+ if not self ._isinit :
641
+ self ._init ()
642
+ return self ._lut [self ._i_over ]
643
+
626
644
def set_over (self , color = 'k' , alpha = None ):
627
- """
628
- Set the color for high out-of-range values when ``norm.clip = False``.
629
- """
645
+ """Set the color for high out-of-range values."""
630
646
_warn_if_global_cmap_modified (self )
631
647
self ._rgba_over = to_rgba (color , alpha )
632
648
if self ._isinit :
@@ -648,6 +664,7 @@ def _init(self):
648
664
raise NotImplementedError ("Abstract class only" )
649
665
650
666
def is_gray (self ):
667
+ """Return whether the color map is grayscale."""
651
668
if not self ._isinit :
652
669
self ._init ()
653
670
return (np .all (self ._lut [:, 0 ] == self ._lut [:, 1 ]) and
@@ -678,8 +695,8 @@ def reversed(self, name=None):
678
695
679
696
def _repr_png_ (self ):
680
697
"""Generate a PNG representation of the Colormap."""
681
- IMAGE_SIZE = ( 400 , 50 )
682
- X = np . tile ( np . linspace ( 0 , 1 , IMAGE_SIZE [ 0 ]), ( IMAGE_SIZE [1 ], 1 ))
698
+ X = np . tile ( np . linspace ( 0 , 1 , _REPR_PNG_SIZE [ 0 ]),
699
+ ( _REPR_PNG_SIZE [1 ], 1 ))
683
700
pixels = self (X , bytes = True )
684
701
png_bytes = io .BytesIO ()
685
702
title = self .name + ' color map'
@@ -696,12 +713,36 @@ def _repr_html_(self):
696
713
"""Generate an HTML representation of the Colormap."""
697
714
png_bytes = self ._repr_png_ ()
698
715
png_base64 = base64 .b64encode (png_bytes ).decode ('ascii' )
699
- return ('<strong>' + self .name + '</strong>' +
700
- '<img ' +
701
- 'alt="' + self .name + ' color map" ' +
702
- 'title="' + self .name + '"' +
703
- 'style="border: 1px solid #555;" ' +
704
- 'src="data:image/png;base64,' + png_base64 + '">' )
716
+ def color_block (color ):
717
+ hex_color = to_hex (color , keep_alpha = True )
718
+ return (f'<div title="{ hex_color } " '
719
+ 'style="display: inline-block; '
720
+ 'width: 1em; height: 1em; '
721
+ 'margin: 0; '
722
+ 'vertical-align: middle; '
723
+ 'border: 1px solid #555; '
724
+ f'background-color: { hex_color } ;"></div>' )
725
+
726
+ return ('<div style="vertical-align: middle;">'
727
+ f'<strong>{ self .name } </strong> '
728
+ '</div>'
729
+ '<div class="cmap"><img '
730
+ f'alt="{ self .name } color map" '
731
+ f'title="{ self .name } " '
732
+ 'style="border: 1px solid #555;" '
733
+ f'src="data:image/png;base64,{ png_base64 } "></div>'
734
+ '<div style="vertical-align: middle; '
735
+ f'max-width: { _REPR_PNG_SIZE [0 ]+ 2 } px; '
736
+ 'display: flex; justify-content: space-between;">'
737
+ '<div style="float: left;">'
738
+ f'{ color_block (self .get_under ())} under'
739
+ '</div>'
740
+ '<div style="margin: 0 auto; display: inline-block;">'
741
+ f'bad { color_block (self .get_bad ())} '
742
+ '</div>'
743
+ '<div style="float: right;">'
744
+ f'over { color_block (self .get_over ())} '
745
+ '</div>' )
705
746
706
747
707
748
class LinearSegmentedColormap (Colormap ):
0 commit comments