From 7c9e413c806dec2c517885009de8dff368957097 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Fri, 2 Dec 2016 11:43:01 +0000 Subject: [PATCH 1/3] Correctly extend a lognormed colorbar --- lib/matplotlib/colorbar.py | 16 ++++++++++++---- lib/matplotlib/tests/test_colorbar.py | 10 +++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 31857c2df6e6..4dab0844c613 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -692,10 +692,18 @@ def _process_values(self, b=None): expander=0.1) b = self.norm.inverse(self._uniform_y(self.cmap.N + 1)) - if self._extend_lower(): - b[0] = b[0] - 1 - if self._extend_upper(): - b[-1] = b[-1] + 1 + + if isinstance(self.norm, colors.LogNorm): + # If using a lognorm, ensure extensions don't go negative + if self._extend_lower(): + b[0] = 0.9 * b[0] + if self._extend_upper(): + b[-1] = 1.1 * b[-1] + else: + if self._extend_lower(): + b[0] = b[0] + 1 + if self._extend_upper(): + b[-1] = b[-1] + 1 self._process_values(b) def _find_range(self): diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 007c006a9022..cbc157241da8 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -10,7 +10,7 @@ from matplotlib.testing.decorators import image_comparison, cleanup import matplotlib.pyplot as plt from matplotlib import rcParams -from matplotlib.colors import BoundaryNorm +from matplotlib.colors import BoundaryNorm, LogNorm from matplotlib.cm import get_cmap from matplotlib import cm from matplotlib.colorbar import ColorbarBase @@ -325,6 +325,14 @@ def test_colorbar_get_ticks(): assert defTicks.get_ticks().tolist() == levels +@cleanup +def test_colorbar_lognorm_extension(): + # Test that colorbar with lognorm is extended correctly + ax = plt.gca() + cb = ColorbarBase(ax, norm=LogNorm(vmin=0.1, vmax=1000.0), + orientation='vertical', extend='both') + assert cb._values[0] >= 0.0 + if __name__ == '__main__': import nose nose.runmodule(argv=['-s', '--with-doctest'], exit=False) From a311b969bb03b56aff193046bbeda501a5a112be Mon Sep 17 00:00:00 2001 From: David Stansby Date: Fri, 2 Dec 2016 13:51:12 +0000 Subject: [PATCH 2/3] Fix +/- typo --- lib/matplotlib/colorbar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 4dab0844c613..df488bba7811 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -701,7 +701,7 @@ def _process_values(self, b=None): b[-1] = 1.1 * b[-1] else: if self._extend_lower(): - b[0] = b[0] + 1 + b[0] = b[0] - 1 if self._extend_upper(): b[-1] = b[-1] + 1 self._process_values(b) From 56cb141d60fb8dcbc3d0685c74cb8b7bf5172f6c Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sat, 3 Dec 2016 10:18:08 +0000 Subject: [PATCH 3/3] Cleanup lognorm colorbar test --- lib/matplotlib/tests/test_colorbar.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index cbc157241da8..aa7f90ac4b32 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -328,11 +328,12 @@ def test_colorbar_get_ticks(): @cleanup def test_colorbar_lognorm_extension(): # Test that colorbar with lognorm is extended correctly - ax = plt.gca() + f, ax = plt.subplots() cb = ColorbarBase(ax, norm=LogNorm(vmin=0.1, vmax=1000.0), orientation='vertical', extend='both') assert cb._values[0] >= 0.0 + if __name__ == '__main__': import nose nose.runmodule(argv=['-s', '--with-doctest'], exit=False)