import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from cartopy.io import shapereader
ax = plt.axes(projection=ccrs.PlateCarree())
clevs = [0, 0.05, 0.1, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60]
vp_fill = plt.contourf(tp.longitude, tp.latitude, tp.tprate[0,:,:]*1000, clevs,
transform=ccrs.PlateCarree(),cmap=plt.cm.viridis_r, extend='both')
cbar = plt.colorbar(vp_fill, orientation='vertical')
cbar.ax.tick_params(labelsize=14)
# Set ticks and extent for the Indian region
ax.set_xticks([70, 75, 80, 85, 90, 95, 100], crs=ccrs.PlateCarree())
ax.set_yticks([5, 10, 15, 20, 25, 30, 35], crs=ccrs.PlateCarree())
ax.set_extent([68, 100, 5, 35])
plt.rcParams["figure.figsize"] = (12, 10)
ax.coastlines(alpha=0.8)
# Add states boundaries using the provided shapefile
shp = r'C:\Users\IMD\Swapi\india polygon\india-polygon.shp' # Replace with your actual path
ax.add_geometries(shapereader.Reader(shp).geometries(), ccrs.PlateCarree(), edgecolor='k',
facecolor='none')
plt.show()
I have tried mask function but it is showing TypeError: 'NoneType' object is not iterable
I also tried removing id column which contains none values but still showing same error
You could call
ax.add_geometries()only for the shapes related to India. Here is an example of plotting states and provinces from the Natural Earthadmin_1_states_provincesonly if they are within India:The result:
In case you want to show your filled contour data within India only, you can try to clip it with matplotlib.artist.Artist.set_clip_path() using a compound path generated from the patches corresponding to the plotted states of India:
The output: