Skip to content

Use better contour defaults with logarithmic norms #10565

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

Illviljan
Copy link
Contributor

@Illviljan Illviljan commented Jul 24, 2025

Contour and contourf does some strange things when norm is changed from the defaults. Using a logarithmic norm is not uncommon.
It seems better to let matplotlib handle most of the default values.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

import xarray as xr


# %%
N = 100
x = np.linspace(-3, 3, N)
y = np.linspace(-2, 2, N)

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

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

Z = Z1 + 50 * Z2

vmin = Z.min()
vmax = Z.max()
norm = LogNorm(vmin=vmin, vmax=vmax)


# %% xarray
da_z = xr.DataArray(data=(Z), coords=dict(x=("x", x), y=("y", y)))

fig, axs = plt.subplots(2, 3, layout="constrained")

for i, plotfunc in enumerate(("pcolormesh", "contourf", "contour")):
    p = getattr(da_z.plot, plotfunc)
    p(x="x", y="y", ax=axs[0, i], vmin=vmin, vmax=vmax, add_colorbar=True)
    axs[0, i].set(title=f"{plotfunc} linear")
    p(x="x", y="y", ax=axs[1, i], norm=norm, add_colorbar=True)
    axs[1, i].set(title=f"{plotfunc} log")

PR:
image

Main:
image

@Illviljan Illviljan changed the title Remove default re-definition Use better contour defaults with logarithmic norms Jul 24, 2025
@Illviljan Illviljan marked this pull request as ready for review July 24, 2025 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Logscale color normalization in contourf
1 participant