-
Notifications
You must be signed in to change notification settings - Fork 365
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
Mask A Contour With Coastlines #2251
Comments
From the basemap docs
So it seems this is about masking the input array rather than the plot itself? There is an open WIP PR in Iris to mask data with a shapefile. Since Iris uses Cartopy underneath, it’s plausible that that could be adapted for inclusion in Cartopy (if Cartopy maintainers wanted it). Disclaimer: I am not a Cartopy maintainer nor have I been involved in that masking work. So I do not have authority to make decisions on either side! cc @acchamber |
I think this would be a nice feature to add to Cartopy! I think the request would be some way of making it easier to add a clip path to matplotlib. The Iris version is interesting though to do it via Shapely intersections first and create an entirely new object. I'm not sure what would be preferable (clip first through shapely, or keep all data intact and let the renderer do the clipping in matplotlib) Here is a quick example of using the import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.patch import geos_to_path
from matplotlib import pyplot as plt
import matplotlib.path as mpath
import numpy as np
proj = ccrs.PlateCarree()
fig, (ax1, ax2) = plt.subplots(nrows=2, subplot_kw={"projection": proj})
xx, yy = np.meshgrid(np.linspace(-180, 180, 360), np.linspace(-90, 90, 180))
zz = xx**2 + yy**2
ax1.pcolormesh(xx, yy, zz)
mesh = ax2.pcolormesh(xx, yy, zz)
# Get the path of the land polygons
land_path = geos_to_path(list(cfeature.LAND.geometries()))
land_path = mpath.Path.make_compound_path(*land_path)
plate_carre_data_transform = proj._as_mpl_transform(ax2)
mesh.set_clip_path(land_path, plate_carre_data_transform)
ax1.coastlines()
ax2.coastlines()
plt.show() |
I agree this would be a really awesome feature to add to help display data in I think this could possibly be a suite of operations for grid based boolean operations, deciding what is inside or outside of a contour. I have only just started with cartopy but your example helps alot! I think this might be interesting as part of the feature call... if it returned a |
Hello,
I am in the process from switching from
matplotlib.basemap
to cartopy and the standout feature that is missing is themaskoceans
/maskland
feature set.I see that there are
ax.addFeature(LAND/OCEAN...)
however this completely ruins the nice basemap I have included.The data is already there, so how do we make a mask out of it. I understand that projection transforms are somewhat complicated but it should be doable ultimately.
Example of the map I am trying to make.
Several requests for similar features, the only way to do this would be to plot twice and use PIL to mask the image from the LAND data
https://stackoverflow.com/questions/72078262/how-to-mask-data-that-appears-in-the-ocean-using-cartopy-and-matplotlib
The text was updated successfully, but these errors were encountered: