From 0858774a9a4dfefa628700b9d2e7ecc76c734375 Mon Sep 17 00:00:00 2001 From: Andes0113 <54319516+Andes0113@users.noreply.github.com> Date: Wed, 21 Sep 2022 23:05:47 -0400 Subject: [PATCH 1/7] Fixed colorbar bug when yscaled length is 1 --- lib/matplotlib/colorbar.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 8cf8c0e6c1eb..f0265858df02 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1243,10 +1243,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 From 1f2418a6900d7d6326a76ed3603780ce103e34c4 Mon Sep 17 00:00:00 2001 From: Andes0113 <54319516+Andes0113@users.noreply.github.com> Date: Thu, 22 Sep 2022 15:08:32 -0400 Subject: [PATCH 2/7] Added test --- lib/matplotlib/tests/test_colorbar.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 149ed4c3d22e..17ba578a284d 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -241,6 +241,10 @@ def test_contour_colorbar(): fig.colorbar(CS, orientation='horizontal', extend='both') fig.colorbar(CS, orientation='vertical') +def test_contour_uniformfield_colorbar(): + fig, ax = plt.subplots() + cs = ax.contour([[1, 1], [1, 1]]) + fig.colorbar(cs, ax=ax) @image_comparison(['cbar_with_subplots_adjust.png'], remove_text=True, savefig_kwarg={'dpi': 40}) From 3338d20ed0b961d770941df16aa5084d1c725fde Mon Sep 17 00:00:00 2001 From: Andes0113 <54319516+Andes0113@users.noreply.github.com> Date: Thu, 22 Sep 2022 16:05:46 -0400 Subject: [PATCH 3/7] Fixed flake issue with test --- lib/matplotlib/tests/test_colorbar.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 17ba578a284d..597bee74a7d9 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -241,11 +241,13 @@ def test_contour_colorbar(): fig.colorbar(CS, orientation='horizontal', extend='both') fig.colorbar(CS, orientation='vertical') + def test_contour_uniformfield_colorbar(): fig, ax = plt.subplots() cs = ax.contour([[1, 1], [1, 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(): From b99e7585d32c464595edabfd5f93221c421e53c0 Mon Sep 17 00:00:00 2001 From: Andes0113 <54319516+Andes0113@users.noreply.github.com> Date: Thu, 29 Sep 2022 13:53:43 -0400 Subject: [PATCH 4/7] Added smoke test for gh#23817 --- lib/matplotlib/tests/test_colorbar.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 597bee74a7d9..467665f9c5fe 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -243,9 +243,12 @@ def test_contour_colorbar(): def test_contour_uniformfield_colorbar(): + # Smoke test for gh#23817 fig, ax = plt.subplots() - cs = ax.contour([[1, 1], [1, 1]]) - fig.colorbar(cs, ax=ax) + with pytest.warns(Warning) as record: + cs = ax.contour([[1, 1], [1, 1]]) + fig.colorbar(cs, ax=ax) + assert len(record) == 1 @image_comparison(['cbar_with_subplots_adjust.png'], remove_text=True, From b2e5dfa62a6709a1feb19ccd08ddcf8b21ba861e Mon Sep 17 00:00:00 2001 From: Andes0113 <54319516+Andes0113@users.noreply.github.com> Date: Thu, 29 Sep 2022 15:20:37 -0400 Subject: [PATCH 5/7] Added smoke test for gh#23817 --- lib/matplotlib/tests/test_colorbar.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 467665f9c5fe..58ce4d2469e3 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -243,11 +243,10 @@ def test_contour_colorbar(): def test_contour_uniformfield_colorbar(): - # Smoke test for gh#23817 + # Smoke test for gh#23817 fig, ax = plt.subplots() with pytest.warns(Warning) as record: cs = ax.contour([[1, 1], [1, 1]]) - fig.colorbar(cs, ax=ax) assert len(record) == 1 From 501c00aa0d53f7f2b9a1029d767a3be98c4d59ab Mon Sep 17 00:00:00 2001 From: Andes0113 <54319516+Andes0113@users.noreply.github.com> Date: Sat, 1 Oct 2022 14:37:17 -0400 Subject: [PATCH 6/7] Fixed uniformfield test --- lib/matplotlib/tests/test_colorbar.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 58ce4d2469e3..428627f2ccef 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -248,6 +248,7 @@ def test_contour_uniformfield_colorbar(): 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, From 5e581f9675168ad57d1198bf1b6e2779e71aa664 Mon Sep 17 00:00:00 2001 From: Andes0113 <54319516+Andes0113@users.noreply.github.com> Date: Sun, 2 Oct 2022 14:31:33 -0400 Subject: [PATCH 7/7] Added check for contoured uniform fields --- lib/matplotlib/colorbar.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index f0265858df02..ec88549dc49f 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1221,8 +1221,10 @@ 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':