From 5d4f70d5d6b475c7db0753230cc56c7acf9aa451 Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Wed, 20 Apr 2022 17:14:00 +0200 Subject: [PATCH 1/2] Fix issue with colorbar extend and drawedges --- lib/matplotlib/colorbar.py | 10 ++++++++-- lib/matplotlib/tests/test_colorbar.py | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index c59b0ac815fe..57d7b50f89f8 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -651,8 +651,14 @@ def _add_solids(self, X, Y, C): if not self.drawedges: if len(self._y) >= self.n_rasterize: self.solids.set_rasterized(True) - self.dividers.set_segments( - np.dstack([X, Y])[1:-1] if self.drawedges else []) + if self.drawedges: + start_idx = 0 if self._extend_lower() else 1 + if self._extend_upper(): + self.dividers.set_segments(np.dstack([X, Y])[start_idx:]) + else: + self.dividers.set_segments(np.dstack([X, Y])[start_idx:-1]) + else: + self.dividers.set_segments([]) def _add_solids_patches(self, X, Y, C, mappable): hatches = mappable.hatches * len(C) # Have enough hatches. diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index ae3ab92c0d43..ee12d722b6b9 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -919,6 +919,30 @@ def test_proportional_colorbars(): fig.colorbar(CS3, spacing=spacings[j], ax=axs[i, j]) +@pytest.mark.parametrize("extend, coloroffset, res", [ + ('both', 1, [np.array([[0., 0.], [0., 1.]]), + np.array([[1., 0.], [1., 1.]]), + np.array([[2., 0.], [2., 1.]])]), + ('min', 0, [np.array([[0., 0.], [0., 1.]]), + np.array([[1., 0.], [1., 1.]])]), + ('max', 0, [np.array([[1., 0.], [1., 1.]]), + np.array([[2., 0.], [2., 1.]])]), + ('neither', -1, [np.array([[1., 0.], [1., 1.]])]) + ]) +def test_colorbar_extend_drawedges(extend, coloroffset, res): + cmap = plt.get_cmap("viridis") + bounds = np.arange(3) + nb_colors = len(bounds) + coloroffset + colors = cmap(np.linspace(100, 255, nb_colors).astype(int)) + cmap, norm = mcolors.from_levels_and_colors(bounds, colors, extend=extend) + + plt.figure(figsize=(5, 1)) + ax = plt.subplot(111) + cbar = Colorbar(ax, cmap=cmap, norm=norm, orientation='horizontal', + drawedges=True) + assert np.all(np.equal(cbar.dividers.get_segments(), res)) + + def test_negative_boundarynorm(): fig, ax = plt.subplots(figsize=(1, 3)) cmap = plt.get_cmap("viridis") From c6729ba3c0335ea80b852c9cc637cfb8c9dce5b4 Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Tue, 14 Jun 2022 10:06:09 +0200 Subject: [PATCH 2/2] Update lib/matplotlib/colorbar.py Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/colorbar.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 57d7b50f89f8..81467d0c79d6 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -653,10 +653,8 @@ def _add_solids(self, X, Y, C): self.solids.set_rasterized(True) if self.drawedges: start_idx = 0 if self._extend_lower() else 1 - if self._extend_upper(): - self.dividers.set_segments(np.dstack([X, Y])[start_idx:]) - else: - self.dividers.set_segments(np.dstack([X, Y])[start_idx:-1]) + end_idx = len(X) if self._extend_upper() else -1 + self.dividers.set_segments(np.dstack([X, Y])[start_idx:end_idx]) else: self.dividers.set_segments([])