如何在已生成的绘图上叠加p_value<0.05的显著性标记点
你不需要额外引入专用库,基于你已使用的matplotlib和cartopy库即可实现该需求,以下是具体实现方案:
注意:你现有代码存在维度不匹配的潜在问题:你的lat数组长度为26,但最后将array1、array2 reshape为(28,62),运行时会出现维度不匹配报错,建议替换为
reshape(len(lat), len(lon))适配经纬度长度。
常用实现方案
方法1:叠加显著区散点标记(推荐,不遮挡底图配色)
这是地学领域显著性绘图最常用的方案,直接在p<0.05的格点位置绘制指定样式的标记(如黑点、白叉),核心代码如下:
# 生成格点化的经纬度网格 lon_grid, lat_grid = np.meshgrid(lon, lat) # 生成显著性掩码:p<0.05的位置标记为1,其余设为NaN sig_mask = np.where(array2 < 0.05, 1, np.nan) # 给左图(相关性系数图)叠加显著性黑点 ax1.scatter(lon_grid[sig_mask==1], lat_grid[sig_mask==1], c='black', s=10, marker='o', transform=ccrs.PlateCarree()) # 给右图(p值图)叠加显著性白叉 ax2.scatter(lon_grid[sig_mask==1], lat_grid[sig_mask==1], c='white', s=15, marker='x', transform=ccrs.PlateCarree())
方法2:叠加显著区斜纹填充
如果需要更明显的区域区分,可以用带斜纹的透明填充层标记显著区域:
# 给左图叠加显著区灰色半透明斜纹层 ax1.contourf(lon, lat, sig_mask, levels=[0.5,1.5], hatches=['////'], colors='none', transform=ccrs.PlateCarree())
修正后完整绘图代码
import matplotlib.pyplot as plt import cartopy.crs as ccrs # 修正数组维度错误 array1 = array1.reshape(len(lat), len(lon)) array2 = array2.reshape(len(lat), len(lon)) fig= plt.figure(figsize=(24,12)) ax1 = fig.add_subplot(1,2,1, projection=ccrs.PlateCarree()) ax2 = fig.add_subplot(1,2,2, projection=ccrs.PlateCarree()) # 绘制底图 im1 = ax1.pcolormesh(lon, lat, array1, cmap='rainbow', transform=ccrs.PlateCarree()) im2 = ax2.pcolormesh(lon, lat, array2, cmap='rainbow', transform=ccrs.PlateCarree()) # 补充色标 plt.colorbar(im1, ax=ax1, shrink=0.6, label='相关系数') plt.colorbar(im2, ax=ax2, shrink=0.6, label='p值') # 叠加显著性标记 lon_grid, lat_grid = np.meshgrid(lon, lat) sig_mask = np.where(array2 < 0.05, 1, np.nan) ax1.scatter(lon_grid[sig_mask==1], lat_grid[sig_mask==1], c='k', s=8, transform=ccrs.PlateCarree()) ax2.scatter(lon_grid[sig_mask==1], lat_grid[sig_mask==1], c='w', s=12, marker='x', transform=ccrs.PlateCarree()) # 补充海岸线(地学绘图标准配置) ax1.coastlines() ax2.coastlines() plt.show()
内容的提问来源于stack exchange,提问作者Ranjan Kumar Sahu
相关产品推荐
相关产品推荐

