SciTools / cartopy

Cartopy - a cartographic python library with matplotlib support
https://scitools.org.uk/cartopy/docs/latest
BSD 3-Clause "New" or "Revised" License
1.43k stars 364 forks source link

Error when plotting polar data using contour #791

Open jhamman opened 8 years ago

jhamman commented 8 years ago

I'm trying to plot some raster data using Cartopy in a polar projection. I'd like to use a contour plot but it seems cartopy isn't able to handle wrapped coordinates. I'm wondering if there is a work around for this behavior.

The unprojected data looks like this:

plt.contour(ds.mask.to_masked_array(), levels=[0, 1])

fig1 (for the untrained eye, the north pole is in the center of the plot, Europe in the top right)

The projected plot has all sorts of problems with wrapping the longitudes

fig, ax = plt.subplots(figsize=(8.5, 5.5), 
                       subplot_kw=dict(projection=ccrs.NorthPolarStereo(-114)))

ax.contour(ds.xc, ds.yc, 
           ds.mask.to_masked_array(), 
           levels=[0, 1], transform=ccrs.PlateCarree())

Returns these errors (warnings actually).

IllegalArgumentException: Invalid number of points in LinearRing found 3 - must be 0 or >= 4
Shell is not a LinearRing
IllegalArgumentException: Invalid number of points in LinearRing found 3 - must be 0 or >= 4
Shell is not a LinearRing

fig2

Just to confirm, no problem the raster data using pcolormesh

fig, ax = plt.subplots(figsize=(8.5, 5.5),
                       subplot_kw=dict(projection=ccrs.NorthPolarStereo(-114)))

m = ax.pcolormesh(ds.xc, ds.yc, 
                  ds.mask.to_masked_array(), 
                  transform=ccrs.PlateCarree())
plt.colorbar(mappable=m)

fig3

yngwaz commented 8 years ago

I get the same warnings (and eventually errors) using non-polar (non-wrapped) coordinates. Warnings/Errors appear with cartopy 0.14.2 whereas everything works fine with version 0.12.

QuLogic commented 7 years ago

Those warnings/errors look like #883, so your case might be fixed by #885?

r5r3 commented 7 years ago

I get the very same error message when I try to plot a contour onto a rotated lat/lon-grid. I tried to apply the fix in #885, but that did not help.

The error occurs for path_codes == [1, 2, 2, 79]. In this particular case, we have three segments plus Path.CLOSEPOLY, which means we try to create the polygon from three instead of four segments. That means, if the last code is Path.CLOSEPOLY we need at least five segments.

pelson commented 6 years ago

Possibly solved by #1021. There have been a number of enhancements to the geometry transformation pipeline recently, so correct input is far more likely to produce correct output at this point. If this is not solved on master (I'm unable to reproduce to check your data), please could you re-open the issue and add a self-contained example?

Thanks!

lvankampenhout commented 5 years ago

Please re-open, as this issue has not been resolved for my example below (using Cartopy 0.17)

Example notebook: https://github.com/lvankampenhout/stackoverflow-rotatedpole/blob/master/Minimum%20example.ipynb

Example data: http://www.staff.science.uu.nl/~kampe004/files/snow_rlat_rlon.nc

PS. workaround here: https://stackoverflow.com/questions/55062406/cartopy-fails-to-correctly-contour-data-on-rotated-grid

htonchia commented 4 years ago

For me it looks like issue #1076 So I think the work around presented in #1421 should work. It very much seem to be an issue of overlapping cells crashing the contouring.