From 722bb94958c03a782e0d0a0824773e868bccbd92 Mon Sep 17 00:00:00 2001 From: Quinna Halim Date: Wed, 7 Dec 2022 11:14:57 -0500 Subject: [PATCH] Attempted fix to colorbar as suggested in previous PR --- lib/matplotlib/colorbar.py | 14 ++++++++++---- lib/matplotlib/tests/test_colorbar.py | 9 +++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index a733a1224a25..3c20e3f94a74 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1249,8 +1249,11 @@ def _proportional_y(self): if (isinstance(self.norm, colors.BoundaryNorm) or self.boundaries is not None): y = (self._boundaries - self._boundaries[self._inside][0]) - y = y / (self._boundaries[self._inside][-1] - - self._boundaries[self._inside][0]) + if (self._boundaries[self._inside][-1] + != self._boundaries[self._inside][0]): + y = y / (self._boundaries[self._inside][-1] - + self._boundaries[self._inside][0]) + # need yscaled the same as the axes scale to get # the extend lengths. if self.spacing == 'uniform': @@ -1271,10 +1274,13 @@ def _proportional_y(self): yscaled = np.ma.filled(norm(yscaled), np.nan) # make the lower and upper extend lengths proportional to the lengths # of the first and last boundary spacing (if extendfrac='auto'): - automin = yscaled[1] - yscaled[0] - automax = yscaled[-1] - yscaled[-2] extendlength = [0, 0] if self._extend_lower() or self._extend_upper(): + automin = yscaled[0] + automax = yscaled[0] + if len(yscaled) > 1: + automin = yscaled[1] - yscaled[0] + automax = yscaled[-1] - yscaled[-2] extendlength = self._get_extension_lengths( self.extendfrac, automin, automax, default=0.05) return y, extendlength diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 5ab3ccf13f80..9b7a378866aa 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -244,6 +244,15 @@ def test_contour_colorbar(): fig.colorbar(CS, orientation='vertical') +def test_contour_uniformfield_colorbar(): + # Smoke test for issue + fig, ax = plt.subplots() + with pytest.warns(Warning) as record: + cs = ax.contour([[1, 1], [1, 1]]) + assert len(record) == 1 + fig.colorbar(cs, ax=ax) + + @image_comparison(['cbar_with_subplots_adjust.png'], remove_text=True, savefig_kwarg={'dpi': 40}) def test_gridspec_make_colorbar():