oceanspy icon indicating copy to clipboard operation
oceanspy copied to clipboard

New matplotlib shading

Open malmans2 opened this issue 4 years ago • 1 comments

Matplotlib changed the default shading and flat will be deprecated soon. This issue explains quite well the problems that we can encounter: https://github.com/SciTools/cartopy/issues/1638

At some point we will have to make some modifications to the horizontal map plots. I just realized that the way I was plotting before it's not the best. You are supposed to use the edge coordinates (e.g., use XG and YG if you are plotting variables on the pressure points). shading='flat' was working with the central point coordinates (coordinates do not have an extra column/row, such as XC YC Temp), but I think it was silently removing the first and last row of data.

It's not urgent as I don't think there will be issues with monotonically increasing rectilinear coordinates, but there are problems with curvilinear grids (e.g., in global maps there are spurious horizontal lines).

malmans2 avatar Sep 29 '20 09:09 malmans2

@malmans2 is this still a live issue?

ThomasHaine avatar Jul 06 '22 19:07 ThomasHaine

I think this is doable but not very necessary. I can imagine there is going to be a lot of difficuties implementing this. And I don't think this will fix plotting on curvilinear grid once and for all.

Xarray does not have this slightly more advanced contour/pcolormesh scheme as well. So we are also not the worst student in class I guess.

I think we can keep this issue open

MaceKuailv avatar Nov 14 '22 21:11 MaceKuailv

OK. What are the workarounds if folks want to avoid the spurious lines on curvilinear plots? If that's straightforward, maybe close the issue?

ThomasHaine avatar Nov 15 '22 13:11 ThomasHaine

What usually works for me is just to divide the dataset into smaller parts, and make several contours on the same figure.

A almost foolproof way to do it is to transform the points with the projection object and then do a tricontour/tricontourf. It will be pretty slow. And I don't think we should implement it.

I don't have much experience with the shading options of pcolormesh. So, I am not confident that the shading thing will just take care of all the weird stuff that comes with curvilinear grids.

The way oceanspy do the plotting is to treat contour/contourf/pcolormesh as essentially different flavor of the same function. This will run into problem with the 'flat' shading, because contourf/contour does not support it.

# some preparations, load the data etc.
import matplotlib.pyplot as plt
import numpy as np
import cartopy.crs as ccrs
import xarray as xr
rasm = xr.tutorial.load_dataset("rasm")
proj = ccrs.NorthPolarStereo()
ax = plt.subplot(111, projection=proj)
ax.set_global()
thing = rasm.isel(time=1).Tair

### THE PLOTTING PART###

# This works pretty well
plt.pcolormesh(rasm['xc'],rasm['yc'],thing[:-1,:-1],shading = 'auto',transform = ccrs.PlateCarree())

# This is going to give you strange blobs
# plt.pcolormesh(rasm['xc'],rasm['yc'],thing,shading = 'auto',transform = ccrs.PlateCarree())

# Albeit similar to the first one, this will result in an error. 
# plt.contourf(rasm['xc'],rasm['yc'],thing[:-1,:-1],shading = 'auto',transform = ccrs.PlateCarree())


ax.set_extent([-180, 180, 43, 90], ccrs.PlateCarree())
ax.coastlines()

If the user wants a contour plot, there is still no way we can avoid the weird blobs and lines showing up.

MaceKuailv avatar Nov 17 '22 02:11 MaceKuailv

I think after users get the horizontal cutout, oceanspy has already done a good job. If it can also plot it, it would be great. But if it can't, the users really can't blame oceanspy, because there are simply too many niche situations.

MaceKuailv avatar Nov 17 '22 02:11 MaceKuailv

Sounds good. I agree that OceanSpy shouldn't have to support a fully-functional production plotting system.

ThomasHaine avatar Nov 17 '22 12:11 ThomasHaine