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()
我尝试过遮罩功能,但它显示 类型错误:“NoneType”对象不可迭代
我还尝试删除不包含任何值的 id 列,但仍然显示相同的错误
要屏蔽印度地区以外的值,您可以使用 NumPy 创建一个屏蔽,然后将其应用于您的数据:
import numpy as np
# Create a mask for points outside the Indian region
mask = ~((tp.longitude >= 68) & (tp.longitude <= 100) & (tp.latitude >= 5) & (tp.latitude <= 35))
tp_masked = np.ma.array(tp.tprate[0,:,:]*1000, mask=mask)
vp_fill = plt.contourf(tp.longitude, tp.latitude, tp_masked, clevs,
transform=ccrs.PlateCarree(), cmap=plt.cm.viridis_r, extend='both')