Skip to content

Commit

Permalink
FIX: make colorbars for log normed contour plots have logscale
Browse files Browse the repository at this point in the history
  • Loading branch information
jklymak committed Jul 6, 2022
1 parent bfb7310 commit 907c109
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/matplotlib/colorbar.py
Expand Up @@ -1212,7 +1212,8 @@ def _reset_locator_formatter_scale(self):
self._minorlocator = None
self._formatter = None
self._minorformatter = None
if isinstance(self.norm, colors.LogNorm):
if (isinstance(self.mappable, contour.ContourSet) and
isinstance(self.norm, colors.LogNorm)):
# if contours have lognorm, give them a log scale...
self._set_scale('log')
elif (self.boundaries is not None or
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 22 additions & 1 deletion lib/matplotlib/tests/test_contour.py
Expand Up @@ -7,7 +7,7 @@
from numpy.testing import assert_array_almost_equal
import matplotlib as mpl
from matplotlib.testing.decorators import image_comparison
from matplotlib import pyplot as plt, rc_context
from matplotlib import pyplot as plt, rc_context, ticker
from matplotlib.colors import LogNorm, same_color
import pytest

Expand Down Expand Up @@ -154,6 +154,27 @@ def test_given_colors_levels_and_extends():
plt.colorbar(c, ax=ax)


@image_comparison(['contour_log_locator.png'], style='mpl20', remove_text=True)
def test_log_locator_levels():

fig, ax = plt.subplots()

N = 100
x = np.linspace(-3.0, 3.0, N)
y = np.linspace(-2.0, 2.0, N)

X, Y = np.meshgrid(x, y)

Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X * 10)**2 - (Y * 10)**2)
data = Z1 + 50 * Z2

c = ax.contourf(data, locator=ticker.LogLocator())
assert_array_almost_equal(c.levels, np.power(10.0, np.arange(-6, 3)))
cb = fig.colorbar(c, ax=ax)
assert_array_almost_equal(cb.ax.get_yticks(), c.levels)


@image_comparison(['contour_datetime_axis.png'], style='mpl20')
def test_contour_datetime_axis():
fig = plt.figure()
Expand Down

0 comments on commit 907c109

Please sign in to comment.