Skip to content

Commit 58b92b2

Browse files
committed
Merge pull request #7552 from dstansby/lognorm-colorbar-fix
Correctly extend a lognormed colorbar
1 parent 76635ec commit 58b92b2

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/matplotlib/colorbar.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,18 @@ def _process_values(self, b=None):
686686
expander=0.1)
687687

688688
b = self.norm.inverse(self._uniform_y(self.cmap.N + 1))
689-
if self._extend_lower():
690-
b[0] = b[0] - 1
691-
if self._extend_upper():
692-
b[-1] = b[-1] + 1
689+
690+
if isinstance(self.norm, colors.LogNorm):
691+
# If using a lognorm, ensure extensions don't go negative
692+
if self._extend_lower():
693+
b[0] = 0.9 * b[0]
694+
if self._extend_upper():
695+
b[-1] = 1.1 * b[-1]
696+
else:
697+
if self._extend_lower():
698+
b[0] = b[0] - 1
699+
if self._extend_upper():
700+
b[-1] = b[-1] + 1
693701
self._process_values(b)
694702

695703
def _find_range(self):

lib/matplotlib/tests/test_colorbar.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from matplotlib.testing.decorators import image_comparison, cleanup
1111
import matplotlib.pyplot as plt
1212
from matplotlib import rcParams
13-
from matplotlib.colors import BoundaryNorm
13+
from matplotlib.colors import BoundaryNorm, LogNorm
1414
from matplotlib.cm import get_cmap
1515
from matplotlib import cm
1616
from matplotlib.colorbar import ColorbarBase
@@ -298,6 +298,15 @@ def test_colorbar_ticks():
298298
assert len(cbar.ax.xaxis.get_ticklocs()) == len(clevs)
299299

300300

301+
@cleanup
302+
def test_colorbar_lognorm_extension():
303+
# Test that colorbar with lognorm is extended correctly
304+
f, ax = plt.subplots()
305+
cb = ColorbarBase(ax, norm=LogNorm(vmin=0.1, vmax=1000.0),
306+
orientation='vertical', extend='both')
307+
assert cb._values[0] >= 0.0
308+
309+
301310
if __name__ == '__main__':
302311
import nose
303312
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)