pydata / xarray

N-D labeled arrays and datasets in Python
https://xarray.dev
Apache License 2.0
3.59k stars 1.08k forks source link

plt.pcolormesh will infer interval breaks per default #4364

Open mathause opened 4 years ago

mathause commented 4 years ago

Looking at some warnings in #3266 I saw that matplotlib will deprecate the old behaviour of pcolormesh when the shape of the data and the coordinates are equal (they silently cut a row and a column of the data). With the new behaviour they will interpolate the coordinates.

import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3])
y = np.array([1, 2, 3, 4, 5])

data = np.random.randn(*y.shape + x.shape)

f, axes = plt.subplots(1, 2)

for ax, shading, behavior in zip(axes, ["flat", "nearest"], ["old", "new"]):
  ax.pcolormesh(x, y, data, shading=shading, vmin=-0.75, vmax=0.75)
  ax.set_title(f"{behavior}: shading='{shading}'")

shading_old_new

This is a good thing in general - we already do this for a long time with the infer_intervals keyword. Unfortunately they don't check if the data is monotonic (matplotlib/matplotlib#18317) which can lead to problems for maps (scitools/cartopy#1638). I don't think there is a need to do something right now - let's see what they think upstream.

This change was introduced in mpl 3.3.0

leroyvn commented 3 years ago

I have a similar issue with nonmonotonic coordinates (mapping a 2D image to a zenith-azimuth space). Solving this is easy when using Matplotlib's API directly, since I can compute mesh coordinates by myself (on a array with one row and one column more) and supply these as pcolormesh()'s x and y arguments; but I can't inject these into xarray's pcolormesh() function. Wouldn't allowing infer_interval=False and passing ordinary arrays as x and y be a good solution?

fmaussion commented 3 years ago

Another consequence is that this example in the docs is now meaningless / misleading. I've just come across the series of discussions in cartopy about this issue - it looks quite complex and I'm also not sure yet what to do in xarray in the mean time.

Wouldn't allowing infer_interval=False and passing ordinary arrays as x and y be a good solution?

I'm not sure what you mean by that? You can always set infer_interval=False at the xarray level?

leroyvn commented 3 years ago

I'm not sure what you mean by that? You can always set infer_interval=False at the xarray level?

Yes indeed, but you cannot pass custom Numpy arrays as the x and y parameters to set.