Skip to content

Fix Logscale color normalization in contourf #10561

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

Closed
wants to merge 8 commits into from
Closed
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
5 changes: 4 additions & 1 deletion xarray/plot/dataarray_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1581,11 +1581,14 @@ def newplotfunc(
)

if "contour" in plotfunc.__name__:
from matplotlib import ticker

# extend is a keyword argument only for contour and contourf, but
# passing it to the colorbar is sufficient for imshow and
# pcolormesh
kwargs["extend"] = cmap_params["extend"]
kwargs["levels"] = cmap_params["levels"]
if not (isinstance(kwargs.get("locator"), ticker.LogLocator)):
kwargs["levels"] = cmap_params["levels"]
# if colors == a single color, matplotlib draws dashed negative
# contours. we lose this feature if we pass cmap and not colors
if isinstance(colors, str):
Expand Down
10 changes: 10 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,16 @@ def test_levels(self) -> None:
artist = self.plotmethod(levels=3)
assert artist.extend == "neither"

def test_cbar_logscale(self) -> None:
_darray = self.darray.copy()
_darray.values = np.abs(_darray.values)
_darray.plot.contourf(
norm=mpl.colors.LogNorm(vmin=0.1, vmax=10), locator=mpl.ticker.LogLocator()
)
ax = plt.gca()
cbar = ax.figure.colorbar(ax.collections[0], ax=ax)
np.testing.assert_array_equal(cbar.get_ticks(), np.logspace(-3, 1, num=5))


@pytest.mark.slow
class TestContour(Common2dMixin, PlotTestCase):
Expand Down
Loading