Skip to content

Commit e66d2f1

Browse files
authored
Merge pull request #7552 from dstansby/lognorm-colorbar-fix
Correctly extend a lognormed colorbar
2 parents dfd38f7 + 56cb141 commit e66d2f1

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/matplotlib/colorbar.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -692,10 +692,18 @@ def _process_values(self, b=None):
692692
expander=0.1)
693693

694694
b = self.norm.inverse(self._uniform_y(self.cmap.N + 1))
695-
if self._extend_lower():
696-
b[0] = b[0] - 1
697-
if self._extend_upper():
698-
b[-1] = b[-1] + 1
695+
696+
if isinstance(self.norm, colors.LogNorm):
697+
# If using a lognorm, ensure extensions don't go negative
698+
if self._extend_lower():
699+
b[0] = 0.9 * b[0]
700+
if self._extend_upper():
701+
b[-1] = 1.1 * b[-1]
702+
else:
703+
if self._extend_lower():
704+
b[0] = b[0] - 1
705+
if self._extend_upper():
706+
b[-1] = b[-1] + 1
699707
self._process_values(b)
700708

701709
def _find_range(self):

lib/matplotlib/tests/test_colorbar.py

+10-1
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
@@ -325,6 +325,15 @@ def test_colorbar_get_ticks():
325325
assert defTicks.get_ticks().tolist() == levels
326326

327327

328+
@cleanup
329+
def test_colorbar_lognorm_extension():
330+
# Test that colorbar with lognorm is extended correctly
331+
f, ax = plt.subplots()
332+
cb = ColorbarBase(ax, norm=LogNorm(vmin=0.1, vmax=1000.0),
333+
orientation='vertical', extend='both')
334+
assert cb._values[0] >= 0.0
335+
336+
328337
if __name__ == '__main__':
329338
import nose
330339
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)