Skip to content

Correctly extend a lognormed colorbar #7552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 10 additions & 1 deletion lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more blank line.


if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)