From 924aa61d90470292d903b631b9e01cf64860847e Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sun, 12 Jul 2020 12:28:29 -0500 Subject: [PATCH 01/20] Add get_bad, get_under, get_over. --- lib/matplotlib/colors.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 95151c7b9779..7620f93df715 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -614,6 +614,12 @@ def __copy__(self): cmapobject._global = False return cmapobject + def get_bad(self): + """Get the color for masked values.""" + if not self._isinit: + self._init() + return self._lut[self._i_bad] + def set_bad(self, color='k', alpha=None): """Set the color for masked values.""" _warn_if_global_cmap_modified(self) @@ -621,6 +627,14 @@ def set_bad(self, color='k', alpha=None): if self._isinit: self._set_extremes() + def get_under(self): + """ + Get the color for low out-of-range values when ``norm.clip = False``. + """ + if not self._isinit: + self._init() + return self._lut[self._i_under] + def set_under(self, color='k', alpha=None): """ Set the color for low out-of-range values when ``norm.clip = False``. @@ -630,6 +644,14 @@ def set_under(self, color='k', alpha=None): if self._isinit: self._set_extremes() + def get_over(self): + """ + Get the color for high out-of-range values when ``norm.clip = False``. + """ + if not self._isinit: + self._init() + return self._lut[self._i_over] + def set_over(self, color='k', alpha=None): """ Set the color for high out-of-range values when ``norm.clip = False``. From c7115f04ca543d0f06af3c1cdfc669dadad32e1c Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sun, 12 Jul 2020 12:28:42 -0500 Subject: [PATCH 02/20] Add missing docstring to is_gray. --- lib/matplotlib/colors.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 7620f93df715..73e8697b6de3 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -677,6 +677,7 @@ def _init(self): raise NotImplementedError("Abstract class only") def is_gray(self): + """Determine if the color map is grayscale.""" if not self._isinit: self._init() return (np.all(self._lut[:, 0] == self._lut[:, 1]) and From 2fd8ed0bae33ef72ea4d84219de5c8d7b0cef4dc Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sun, 12 Jul 2020 13:07:38 -0500 Subject: [PATCH 03/20] Add under/over/bad colors to Colormap _repr_html_. --- lib/matplotlib/colors.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 73e8697b6de3..2bd549fec833 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -726,12 +726,30 @@ def _repr_html_(self): """Generate an HTML representation of the Colormap.""" png_bytes = self._repr_png_() png_base64 = base64.b64encode(png_bytes).decode('ascii') - return ('' + self.name + '' + - '' + + '' + name + ': ' + + '
' + + '') + + return ('
' + + '' + self.name + ' ' + + color_block('under', self.get_under()) + + color_block('over', self.get_over()) + + color_block('bad', self.get_bad()) + + '
' + + '
') + 'src="data:image/png;base64,' + png_base64 + '">
') class LinearSegmentedColormap(Colormap): From 4514171d7ad93286753d97bde8bf3d1773bf36e2 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sun, 12 Jul 2020 13:41:13 -0500 Subject: [PATCH 04/20] Add tests for getters. --- lib/matplotlib/tests/test_colors.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 5e0b2044bcf8..bc25913ca31f 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -303,6 +303,9 @@ def test_BoundaryNorm(): cmref.set_under('white') cmshould = mcolors.ListedColormap(['white', 'blue', 'red', 'black']) + assert_array_equal(cmref.get_over(), mcolors.to_rgba('black')) + assert_array_equal(cmref.get_under(), mcolors.to_rgba('white')) + refnorm = mcolors.BoundaryNorm(bounds, cmref.N) mynorm = mcolors.BoundaryNorm(bounds, cmshould.N, extend='both') assert mynorm.vmin == refnorm.vmin @@ -323,6 +326,8 @@ def test_BoundaryNorm(): cmref.set_under('white') cmshould = mcolors.ListedColormap(['white', 'blue', 'red']) + assert_array_equal(cmref.get_under(), mcolors.to_rgba('white')) + assert cmref.N == 2 assert cmshould.N == 3 refnorm = mcolors.BoundaryNorm(bounds, cmref.N) @@ -339,6 +344,8 @@ def test_BoundaryNorm(): cmref.set_over('black') cmshould = mcolors.ListedColormap(['blue', 'red', 'black']) + assert_array_equal(cmref.get_over(), mcolors.to_rgba('black')) + assert cmref.N == 2 assert cmshould.N == 3 refnorm = mcolors.BoundaryNorm(bounds, cmref.N) @@ -1161,3 +1168,9 @@ def test_repr_html(): html = cmap._repr_html_() assert len(html) > 0 assert cmap.name in html + +def test_get_under_over_bad(): + cmap = plt.get_cmap('viridis') + assert_array_equal(cmap.get_under(), cmap(0.0)) + assert_array_equal(cmap.get_over(), cmap(1.0)) + assert_array_equal(cmap.get_bad(), cmap(np.nan)) From 90dc80eb29b10690279a39aeb7bfb2ab2094a1fe Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sun, 12 Jul 2020 14:18:17 -0500 Subject: [PATCH 05/20] Add What's New. --- doc/users/next_whats_new/colormap_get_under_over_bad.rst | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 doc/users/next_whats_new/colormap_get_under_over_bad.rst diff --git a/doc/users/next_whats_new/colormap_get_under_over_bad.rst b/doc/users/next_whats_new/colormap_get_under_over_bad.rst new file mode 100644 index 000000000000..b99858df22ae --- /dev/null +++ b/doc/users/next_whats_new/colormap_get_under_over_bad.rst @@ -0,0 +1,7 @@ +Get over/under/bad colors of Colormap objects +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The `matplotlib.colors.Colormap` object now has methods +`~colors.Colormap.get_under`, `~colors.Colormap.get_over`, +`~colors.Colormap.get_bad` for the colors used for out-of-range and masked +values. From 8dcb3eab5d0f83f6771ac2f0391c6d32f7238a3c Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sun, 12 Jul 2020 14:24:20 -0500 Subject: [PATCH 06/20] Fix flake8, reorder words. --- doc/users/next_whats_new/colormap_get_under_over_bad.rst | 2 +- lib/matplotlib/tests/test_colors.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/users/next_whats_new/colormap_get_under_over_bad.rst b/doc/users/next_whats_new/colormap_get_under_over_bad.rst index b99858df22ae..b7e697ba4823 100644 --- a/doc/users/next_whats_new/colormap_get_under_over_bad.rst +++ b/doc/users/next_whats_new/colormap_get_under_over_bad.rst @@ -1,4 +1,4 @@ -Get over/under/bad colors of Colormap objects +Get under/over/bad colors of Colormap objects ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The `matplotlib.colors.Colormap` object now has methods diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index bc25913ca31f..47c7db42a55b 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -1169,6 +1169,7 @@ def test_repr_html(): assert len(html) > 0 assert cmap.name in html + def test_get_under_over_bad(): cmap = plt.get_cmap('viridis') assert_array_equal(cmap.get_under(), cmap(0.0)) From c6fc9896edb4bb28671d1c5dbb14734b7a180715 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sun, 12 Jul 2020 19:04:42 -0500 Subject: [PATCH 07/20] Use same_color for testing. --- lib/matplotlib/tests/test_colors.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 47c7db42a55b..c00253f451e7 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -303,8 +303,8 @@ def test_BoundaryNorm(): cmref.set_under('white') cmshould = mcolors.ListedColormap(['white', 'blue', 'red', 'black']) - assert_array_equal(cmref.get_over(), mcolors.to_rgba('black')) - assert_array_equal(cmref.get_under(), mcolors.to_rgba('white')) + assert mcolors.same_color(cmref.get_over(), 'black') + assert mcolors.same_color(cmref.get_under(), 'white') refnorm = mcolors.BoundaryNorm(bounds, cmref.N) mynorm = mcolors.BoundaryNorm(bounds, cmshould.N, extend='both') @@ -326,7 +326,7 @@ def test_BoundaryNorm(): cmref.set_under('white') cmshould = mcolors.ListedColormap(['white', 'blue', 'red']) - assert_array_equal(cmref.get_under(), mcolors.to_rgba('white')) + assert mcolors.same_color(cmref.get_under(), 'white') assert cmref.N == 2 assert cmshould.N == 3 @@ -344,7 +344,7 @@ def test_BoundaryNorm(): cmref.set_over('black') cmshould = mcolors.ListedColormap(['blue', 'red', 'black']) - assert_array_equal(cmref.get_over(), mcolors.to_rgba('black')) + assert mcolors.same_color(cmref.get_over(), 'black') assert cmref.N == 2 assert cmshould.N == 3 From feb1f87262a299b811e6d2078e9ae4b4a8c3567d Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sun, 12 Jul 2020 16:27:55 -0500 Subject: [PATCH 08/20] Apply suggestions from code review Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- doc/users/next_whats_new/colormap_get_under_over_bad.rst | 2 +- lib/matplotlib/colors.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/users/next_whats_new/colormap_get_under_over_bad.rst b/doc/users/next_whats_new/colormap_get_under_over_bad.rst index b7e697ba4823..076f97cfb958 100644 --- a/doc/users/next_whats_new/colormap_get_under_over_bad.rst +++ b/doc/users/next_whats_new/colormap_get_under_over_bad.rst @@ -1,7 +1,7 @@ Get under/over/bad colors of Colormap objects ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -The `matplotlib.colors.Colormap` object now has methods +`matplotlib.colors.Colormap` now has methods `~colors.Colormap.get_under`, `~colors.Colormap.get_over`, `~colors.Colormap.get_bad` for the colors used for out-of-range and masked values. diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 2bd549fec833..8be93d2db4a9 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -677,7 +677,7 @@ def _init(self): raise NotImplementedError("Abstract class only") def is_gray(self): - """Determine if the color map is grayscale.""" + """Return whether the color map is grayscale.""" if not self._isinit: self._init() return (np.all(self._lut[:, 0] == self._lut[:, 1]) and From 642477ebac73ddb964982f275fcc1285ac920e3d Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sun, 12 Jul 2020 19:08:16 -0500 Subject: [PATCH 09/20] Use values outside [0, 1] for testing under/over. --- lib/matplotlib/tests/test_colors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index c00253f451e7..b59750efac34 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -1172,6 +1172,6 @@ def test_repr_html(): def test_get_under_over_bad(): cmap = plt.get_cmap('viridis') - assert_array_equal(cmap.get_under(), cmap(0.0)) - assert_array_equal(cmap.get_over(), cmap(1.0)) + assert_array_equal(cmap.get_under(), cmap(-np.inf)) + assert_array_equal(cmap.get_over(), cmap(np.inf)) assert_array_equal(cmap.get_bad(), cmap(np.nan)) From c43cceb983341583e2221f479cc8080f553742ac Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Tue, 14 Jul 2020 11:57:59 -0500 Subject: [PATCH 10/20] Remove reference to norm.clip in docstrings. --- lib/matplotlib/colors.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 8be93d2db4a9..05cb96b1fe59 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -629,7 +629,7 @@ def set_bad(self, color='k', alpha=None): def get_under(self): """ - Get the color for low out-of-range values when ``norm.clip = False``. + Get the color for low out-of-range values. """ if not self._isinit: self._init() @@ -637,7 +637,7 @@ def get_under(self): def set_under(self, color='k', alpha=None): """ - Set the color for low out-of-range values when ``norm.clip = False``. + Set the color for low out-of-range values. """ _warn_if_global_cmap_modified(self) self._rgba_under = to_rgba(color, alpha) @@ -646,7 +646,7 @@ def set_under(self, color='k', alpha=None): def get_over(self): """ - Get the color for high out-of-range values when ``norm.clip = False``. + Get the color for high out-of-range values. """ if not self._isinit: self._init() @@ -654,7 +654,7 @@ def get_over(self): def set_over(self, color='k', alpha=None): """ - Set the color for high out-of-range values when ``norm.clip = False``. + Set the color for high out-of-range values. """ _warn_if_global_cmap_modified(self) self._rgba_over = to_rgba(color, alpha) From 9ca18937855906189bef1a9b808fb1920d61954f Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sun, 9 Aug 2020 00:00:15 -0500 Subject: [PATCH 11/20] Move under/bad/over below the colormap. --- lib/matplotlib/colors.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 05cb96b1fe59..3ec194c61374 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -728,7 +728,7 @@ def _repr_html_(self): png_base64 = base64.b64encode(png_bytes).decode('ascii') def color_block(name, color): hex_color = to_hex(color, keep_alpha=True) - return ('' + + return ('' + '' + name + ': ' + '
' + '' + self.name + ' ' + - color_block('under', self.get_under()) + - color_block('over', self.get_over()) + - color_block('bad', self.get_bad()) + '
' + '
') + 'src="data:image/png;base64,' + png_base64 + '">' + + '
' + + color_block('under', self.get_under()) + + color_block('bad', self.get_bad()) + + color_block('over', self.get_over()) + + '
') class LinearSegmentedColormap(Colormap): From 12fd588b9ef88446d0bb94d179cba2bbaaf360b0 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sun, 9 Aug 2020 19:54:10 -0500 Subject: [PATCH 12/20] Apply CSS suggestions from @greglucas. --- lib/matplotlib/colors.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 3ec194c61374..d105774527f8 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -726,31 +726,35 @@ def _repr_html_(self): """Generate an HTML representation of the Colormap.""" png_bytes = self._repr_png_() png_base64 = base64.b64encode(png_bytes).decode('ascii') - def color_block(name, color): + def color_block(color): hex_color = to_hex(color, keep_alpha=True) - return ('' + - '' + name + ': ' + - '
' + - '
') + 'background-color: ' + hex_color + ';">' + + '') return ('
' + '' + self.name + ' ' + '
' + - '
' + - '
' + - color_block('under', self.get_under()) + - color_block('bad', self.get_bad()) + - color_block('over', self.get_over()) + + '
' + + '
' + + color_block(self.get_under()) + ' under' + + '
' + + '
' + + 'bad ' + color_block(self.get_bad()) + + '
' + + '
' + + 'over ' + color_block(self.get_over()) + '
') From df1cd26980c1c941d53aabc805a2b27a1d656327 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sun, 9 Aug 2020 20:15:19 -0500 Subject: [PATCH 13/20] Fix references to documentation. --- doc/users/next_whats_new/colormap_get_under_over_bad.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/users/next_whats_new/colormap_get_under_over_bad.rst b/doc/users/next_whats_new/colormap_get_under_over_bad.rst index 076f97cfb958..d21e7b4349cc 100644 --- a/doc/users/next_whats_new/colormap_get_under_over_bad.rst +++ b/doc/users/next_whats_new/colormap_get_under_over_bad.rst @@ -2,6 +2,6 @@ Get under/over/bad colors of Colormap objects ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ `matplotlib.colors.Colormap` now has methods -`~colors.Colormap.get_under`, `~colors.Colormap.get_over`, -`~colors.Colormap.get_bad` for the colors used for out-of-range and masked +`~.colors.Colormap.get_under`, `~.colors.Colormap.get_over`, +`~.colors.Colormap.get_bad` for the colors used for out-of-range and masked values. From 64faef05ea29abcc4a30a53ef28f719fffbae1e5 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sun, 9 Aug 2020 20:53:34 -0500 Subject: [PATCH 14/20] Use max-width to make content more responsive. --- lib/matplotlib/colors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index d105774527f8..75799a3c822a 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -745,7 +745,7 @@ def color_block(color): 'title="' + self.name + '" ' + 'style="border: 1px solid #555;" ' + 'src="data:image/png;base64,' + png_base64 + '">
' + - '
' + '
' + color_block(self.get_under()) + ' under' + From a55cbf70a65105069ca2dacda49aeb482aa8d7f2 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sat, 15 Aug 2020 21:29:13 -0500 Subject: [PATCH 15/20] Use one line for short docstrings. --- lib/matplotlib/colors.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 75799a3c822a..c20245522119 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -628,34 +628,26 @@ def set_bad(self, color='k', alpha=None): self._set_extremes() def get_under(self): - """ - Get the color for low out-of-range values. - """ + """Get the color for low out-of-range values.""" if not self._isinit: self._init() return self._lut[self._i_under] def set_under(self, color='k', alpha=None): - """ - Set the color for low out-of-range values. - """ + """Set the color for low out-of-range values.""" _warn_if_global_cmap_modified(self) self._rgba_under = to_rgba(color, alpha) if self._isinit: self._set_extremes() def get_over(self): - """ - Get the color for high out-of-range values. - """ + """Get the color for high out-of-range values.""" if not self._isinit: self._init() return self._lut[self._i_over] def set_over(self, color='k', alpha=None): - """ - Set the color for high out-of-range values. - """ + """Set the color for high out-of-range values.""" _warn_if_global_cmap_modified(self) self._rgba_over = to_rgba(color, alpha) if self._isinit: From 5b095f93a13cfa45bf1f9c3f2ce81303cb263ce4 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sat, 15 Aug 2020 21:33:23 -0500 Subject: [PATCH 16/20] Use f-strings and implicit continuations. --- lib/matplotlib/colors.py | 53 ++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index c20245522119..0e95f3ee1fbe 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -720,33 +720,32 @@ def _repr_html_(self): png_base64 = base64.b64encode(png_bytes).decode('ascii') def color_block(color): hex_color = to_hex(color, keep_alpha=True) - return ('
' + - '
') - - return ('
' + - '' + self.name + ' ' + - '
' + - '
' + - '
' + - '
' + - color_block(self.get_under()) + ' under' + - '
' + - '
' + - 'bad ' + color_block(self.get_bad()) + - '
' + - '
' + - 'over ' + color_block(self.get_over()) + + return (f'
') + + return ('
' + f'{self.name} ' + '
' + '
' + '
' + '
' + f'{color_block(self.get_under())} under' + '
' + '
' + f'bad {color_block(self.get_bad())}' + '
' + '
' + f'over {color_block(self.get_over())}' '
') From 4581ab804b541a48804ce8e53c960e4730d4e8cf Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sat, 15 Aug 2020 21:53:06 -0500 Subject: [PATCH 17/20] Use private module constant for PNG representation size. --- lib/matplotlib/colors.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 0e95f3ee1fbe..ae4416114a4b 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -110,6 +110,8 @@ def __delitem__(self, key): _colors_full_map.update(BASE_COLORS) _colors_full_map = _ColorMapping(_colors_full_map) +_REPR_PNG_SIZE = (512, 64) + def get_named_colors_mapping(): """Return the global mapping of names to named colors.""" @@ -700,8 +702,7 @@ def reversed(self, name=None): def _repr_png_(self): """Generate a PNG representation of the Colormap.""" - IMAGE_SIZE = (400, 50) - X = np.tile(np.linspace(0, 1, IMAGE_SIZE[0]), (IMAGE_SIZE[1], 1)) + X = np.tile(np.linspace(0, 1, _REPR_PNG_SIZE[0]), (_REPR_PNG_SIZE[1], 1)) pixels = self(X, bytes=True) png_bytes = io.BytesIO() title = self.name + ' color map' @@ -736,7 +737,8 @@ def color_block(color): f'title="{self.name}" ' 'style="border: 1px solid #555;" ' f'src="data:image/png;base64,{png_base64}">
' - '
' '
' f'{color_block(self.get_under())} under' From 057dc55fb88b111d3fba431dceb14d5d93ccd781 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sat, 15 Aug 2020 21:53:14 -0500 Subject: [PATCH 18/20] Improve HTML repr tests. --- lib/matplotlib/tests/test_colors.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index b59750efac34..57fa8c962c3a 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -1167,7 +1167,11 @@ def test_repr_html(): cmap = plt.get_cmap('viridis') html = cmap._repr_html_() assert len(html) > 0 + png = cmap._repr_png_() + assert len(html) > len(png) assert cmap.name in html + assert html.startswith('') def test_get_under_over_bad(): From e398f92722d8ff9f792cc8c0cc9d9724e65a8929 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sat, 15 Aug 2020 21:57:00 -0500 Subject: [PATCH 19/20] Assert base64 encoded png is in HTML representation. --- lib/matplotlib/tests/test_colors.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 57fa8c962c3a..6180e789ac95 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -5,6 +5,7 @@ import numpy as np from PIL import Image import pytest +import base64 from numpy.testing import assert_array_equal, assert_array_almost_equal @@ -1168,7 +1169,7 @@ def test_repr_html(): html = cmap._repr_html_() assert len(html) > 0 png = cmap._repr_png_() - assert len(html) > len(png) + assert base64.b64encode(png).decode('ascii') in html assert cmap.name in html assert html.startswith('') From 977677a915e0329b5ec9ca2a7bf4914b7b257945 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Sat, 15 Aug 2020 21:57:57 -0500 Subject: [PATCH 20/20] Fix flake8. --- lib/matplotlib/colors.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index ae4416114a4b..57293aaa664a 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -702,7 +702,8 @@ def reversed(self, name=None): def _repr_png_(self): """Generate a PNG representation of the Colormap.""" - X = np.tile(np.linspace(0, 1, _REPR_PNG_SIZE[0]), (_REPR_PNG_SIZE[1], 1)) + X = np.tile(np.linspace(0, 1, _REPR_PNG_SIZE[0]), + (_REPR_PNG_SIZE[1], 1)) pixels = self(X, bytes=True) png_bytes = io.BytesIO() title = self.name + ' color map'