cartopy: `Spilhaus` projection not working correctly when plotting raster data

I’m trying to plot raster data over a Spilhaus projection but I’m getting some artefacts

Image

This code is MWE to produce the image

import xarray as xr
import fsspec
import pandas as pd
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import numpy as np
import time
from aiohttp.client_exceptions import ServerDisconnectedError

date = pd.Timestamp("2024-01-01")
url = f"https://www.star.nesdis.noaa.gov/pub/socd/mecb/crw/data/5km/v3.1_op/nc/v1.0/daily/sst/{date.year}/coraltemp_v3.1_{date.strftime('%Y%m%d')}.nc"
while True:
    try:
        file = fsspec.open_local(
            f"simplecache::{url}", simplecache={"cache_storage": "/tmp/"}
        )
        ds = xr.open_dataset(file, engine="netcdf4").squeeze()
        break
    except ServerDisconnectedError:
        time.sleep(0.5)

# Set up the figure and the projection for the plot
fig, ax = plt.subplots(
    figsize=(10, 10),
    subplot_kw={
        "projection": ccrs.Spilhaus()
    },
)

ax.add_feature(
    cfeature.LAND.with_scale("110m"), edgecolor="black" , linewidth=0.25, zorder=2
)
lon2d, lat2d = np.meshgrid(ds["lon"].values, ds["lat"].values)

ax.pcolormesh(
    lon2d,
    lat2d,
    ds["analysed_sst"].values,
    transform=ccrs.PlateCarree(),
)

when trying with imshow I get this instead, I guess the data is not properly rotated (but no artefacts).

Image

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 16 (10 by maintainers)

Most upvoted comments

Is the data outside of the projection footprint ever masked when plotting with pclormesh, contourf…? For large arrays I’ve always seen a big difference in plotting time when masking (or not) the input data, which always made me think that cartopy is trying to plot the data also where is not actually visible. But I always had to do it manually beforehand, and I usually employ a simple slicing on the coordinates of the input array using the extent of the target projection converted into plate carree, which is quite a raw approach.

Sorry, sloppy choice of words on my part. The data isn’t masked within Cartopy because we need to transform everything to see where it ends up in the map projection and enable setting smaller/larger limits. So what you are doing to speed things up makes sense when that is possible.

To clarify what I meant though, in pcolormesh we do “mask” some cells that cross the dateline of projections and wrap around the left to right side of the map. Those show up as these streaks that you see here and we try to hide them and instead draw those point with a pcolor() call for just those “masked” cells. That is something to try here, do a pcolor() call instead of pcolormesh() and see if that fixes those streaks or not.