Contour plot is empty due to internal reprojection issue

I have some data that I want to get a contour plot from. This is my code (the data is open source):

import xarray as xr
import hvplot.xarray
import cartopy.crs as ccrs

ds = xr.open_dataset('http://geoport.usgs.esipfed.org/thredds/dodsC/bathy/etopo1_bed_g2')

na = ds.topo.sel(lon=slice(-130,-50),lat=slice(15,50))   # North America
na = na.where(na < 0)
na = na.where(na > -1000)

contours = na.hvplot.contour(x='lon', y='lat', levels=5, cmap=['#000000'], 
                             geo=True, crs=ccrs.PlateCarree())
contours

This creates an empty plot with the warning:

WARNING:param.project_path: While projecting a Contours element from a PlateCarree coordinate reference system (crs) to a PlateCarree projection none of the projected paths were contained within the bounds specified by the projection. Ensure you have specified the correct coordinate system for your data.

However, if I plot that same data as a quadmesh, it plots fine and the projection is correct.

na.hvplot.quadmesh(x='lon', y='lat', rasterize=True, geo=True, project=True, cmap='viridis', frame_width=800, crs=ccrs.PlateCarree())

Also, if I add project=True to the contour plot, I get this error:

File /home/conda/global/d346bceda0a73e0d4a1d000f6e408ff98f97300889e28f2363f683db19a0f74c-20230620-190627-268969-5-hytest-workshop/lib/python3.10/site-packages/holoviews/plotting/bokeh/element.py:903, in ElementPlot._update_ranges(self, element, ranges)
    901 if fixed_height:
    902     plot.frame_height = height
--> 903     plot.frame_width = int(height/aspect)
    904     plot.width, plot.height = None, None
    905 elif fixed_width:

ValueError: cannot convert float NaN to integer

Am I doing something wrong on the contours plot or is this a bug?

I don’t think you are doing anything wrong and I would expect it to work. This seems to work for me:
contours = na.hvplot.contour(x='lon', y='lat', levels=5, cmap=['#000000']).opts(projection=ccrs.PlateCarree())

Can you please open an issue on hvplot with a small MRE example (maybe lanXlon with a size of 10X10)

ValueError: cannot convert float NaN to integer is another issue. I don’t know if it is Holoviews or hvplot, though. Maybe try to search and see if you can find an issue. If not, please open an issue on hvplot too.

Ah, you removed geo=True. If you add that back in to your example, it no longer works and will create an empty plot with the projection warning above.

na.hvplot.contour(x='lon', y='lat', levels=5, cmap=['#000000'], geo=True).opts(projection=ccrs.PlateCarree())

Ok, I see where the issue is coming from. contour can’t handle the NaNs in the dataset. The original dataset will contour fine, but when I did

na = na.where(na<0)
na = na.where(na>-1000)

I introduced NaNs which aren’t being handled properly.

I’ll open an issue on hvplot.

We need to update our contouring code to use contourpy anyway. Could you file an issue with the bug on HoloViews and suggest we rewrite the operation using contourpy?

1 Like

Done: Contour doesn't handle NaNs properly · Issue #5779 · holoviz/holoviews · GitHub