Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2fd8ed0

Browse files
committed
Add under/over/bad colors to Colormap _repr_html_.
1 parent c7115f0 commit 2fd8ed0

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

lib/matplotlib/colors.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -726,12 +726,30 @@ def _repr_html_(self):
726726
"""Generate an HTML representation of the Colormap."""
727727
png_bytes = self._repr_png_()
728728
png_base64 = base64.b64encode(png_bytes).decode('ascii')
729-
return ('<strong>' + self.name + '</strong>' +
730-
'<img ' +
729+
def color_block(name, color):
730+
hex_color = to_hex(color, keep_alpha=True)
731+
return ('<span style="margin: 0 0.4em 0 0.4em;">' +
732+
'<span>' + name + ':</span> ' +
733+
'<div title="' + hex_color + '" ' +
734+
'style="display: inline-block; ' +
735+
'width: 1em; height: 1em; ' +
736+
'margin: 0; ' +
737+
'vertical-align: middle; ' +
738+
'border: 1px solid #555; ' +
739+
'background-color: ' + hex_color + ';"></div>' +
740+
'</span>')
741+
742+
return ('<div style="vertical-align: middle;">' +
743+
'<strong>' + self.name + '</strong> ' +
744+
color_block('under', self.get_under()) +
745+
color_block('over', self.get_over()) +
746+
color_block('bad', self.get_bad()) +
747+
'</div>' +
748+
'<div><img ' +
731749
'alt="' + self.name + ' color map" ' +
732750
'title="' + self.name + '"' +
733751
'style="border: 1px solid #555;" ' +
734-
'src="data:image/png;base64,' + png_base64 + '">')
752+
'src="data:image/png;base64,' + png_base64 + '"></div>')
735753

736754

737755
class LinearSegmentedColormap(Colormap):

0 commit comments

Comments
 (0)