Skip to content
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

Working with Multidimensional Coordinates - Plotting PlateCarree projection looks strange #4596

Closed
ahuang11 opened this issue Nov 19, 2020 · 5 comments

Comments

@ahuang11
Copy link
Contributor

The pixels seem stretched; https://xarray.pydata.org/en/stable/examples/multidimensional-coords.html

@mathause
Copy link
Collaborator

Yes, this is due to an upstream change in matplotlib - see also #4364. mpl tries to infer the edges of the gridcell - which does not work if the data is not monotonic. In the short term we can fix this by passing shading="flat":

ds.Tair[0].plot.pcolormesh(ax=ax, transform=ccrs.PlateCarree(), x='xc', y='yc', add_colorbar=False)

however, mpl will eventually remove shading="flat". I am not sure what the best way forward is. mpl recommends to drop a row and column of the data (which it did earlier automatically). See:

SciTools/cartopy#1638 (comment)

@djuncu
Copy link

djuncu commented Dec 23, 2020

I believe the best (albeit tedious) solution is to resample the coordinates from nx*ny to (nx+1)*(ny+1) before passing the grid to pcolormesh. They should be resampled in such way that the new coordinate positions are on the corner of the quadrilateral as is expected by pcolormesh with flat shading. If only needs to be ensured that none of the 'new' quadliterals crosses the map boundary.

The problem with dropping the last row and column is that it will effectively lead to the position of the values on the map being shifted from their actual position, because pcolormesh assumes that position of value and coordinate different. (May be a minor shift in many cases but still.)

I could not find anything about mpl removing flat shading, are you sure about that?

@mathause
Copy link
Collaborator

I could not find anything about mpl removing flat shading, are you sure about that?

It's in the deprecation warning: MatplotlibDeprecationWarning: shading='flat' when X and Y have the same dimensions as C is deprecated since 3.3. Either specify the corners of the quadrilaterals with X and Y, or pass shading='auto', 'nearest' or 'gouraud', or set rcParams['pcolor.shading']. This will become an error two minor releases later.

I believe the best (albeit tedious) solution is to resample the coordinates from nxny to (nx+1)(ny+1)

We are currently doing this (if the coordinates are monotonic). And mpl also started doing it (and now gives a warning on non-monotonic coordinates). The problem is that with non-monotonic coordinates it leads to wrong results.

If only needs to be ensured that none of the 'new' quadliterals crosses the map boundary.

I think that's not trivial - see also: SciTools/cartopy#1638 and matplotlib/matplotlib#18317 and has to be fixed upstream. There is a proposed fix, see: SciTools/cartopy#1646.

@djuncu
Copy link

djuncu commented Dec 23, 2020

OK I see, thanks for the links.
But the warning says that shading='flat' will be removed as an option only for cases where X/Y dimensions are the same as C; not in general. Sorry, maybe that's just what you meant.

But if the grid is resampled before passing to mpl, flat shading could still be used in the future and non-monotonic coordinates for plotting wouldn't be an issue- well except for the resampling itself.. but they have a good idea for solving that in that cartopy fix. If I understand correctly, their fix 1646 might solve this issue here as well then?

@mathause
Copy link
Collaborator

I think can be closed as fixed upstream

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants