diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 31857c2df6e6..df488bba7811 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..aa7f90ac4b32 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,15 @@ 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 + 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)