Skip to content

Commit 907c109

Browse files
committed
FIX: make colorbars for log normed contour plots have logscale
1 parent bfb7310 commit 907c109

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

lib/matplotlib/colorbar.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,8 @@ def _reset_locator_formatter_scale(self):
12121212
self._minorlocator = None
12131213
self._formatter = None
12141214
self._minorformatter = None
1215-
if isinstance(self.norm, colors.LogNorm):
1215+
if (isinstance(self.mappable, contour.ContourSet) and
1216+
isinstance(self.norm, colors.LogNorm)):
12161217
# if contours have lognorm, give them a log scale...
12171218
self._set_scale('log')
12181219
elif (self.boundaries is not None or

lib/matplotlib/tests/test_contour.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from numpy.testing import assert_array_almost_equal
88
import matplotlib as mpl
99
from matplotlib.testing.decorators import image_comparison
10-
from matplotlib import pyplot as plt, rc_context
10+
from matplotlib import pyplot as plt, rc_context, ticker
1111
from matplotlib.colors import LogNorm, same_color
1212
import pytest
1313

@@ -154,6 +154,27 @@ def test_given_colors_levels_and_extends():
154154
plt.colorbar(c, ax=ax)
155155

156156

157+
@image_comparison(['contour_log_locator.png'], style='mpl20', remove_text=True)
158+
def test_log_locator_levels():
159+
160+
fig, ax = plt.subplots()
161+
162+
N = 100
163+
x = np.linspace(-3.0, 3.0, N)
164+
y = np.linspace(-2.0, 2.0, N)
165+
166+
X, Y = np.meshgrid(x, y)
167+
168+
Z1 = np.exp(-X**2 - Y**2)
169+
Z2 = np.exp(-(X * 10)**2 - (Y * 10)**2)
170+
data = Z1 + 50 * Z2
171+
172+
c = ax.contourf(data, locator=ticker.LogLocator())
173+
assert_array_almost_equal(c.levels, np.power(10.0, np.arange(-6, 3)))
174+
cb = fig.colorbar(c, ax=ax)
175+
assert_array_almost_equal(cb.ax.get_yticks(), c.levels)
176+
177+
157178
@image_comparison(['contour_datetime_axis.png'], style='mpl20')
158179
def test_contour_datetime_axis():
159180
fig = plt.figure()

0 commit comments

Comments
 (0)