我使用Metpy的风矢量在图中未显示

0 投票
1 回答
25 浏览
提问于 2025-04-13 17:27

我正在尝试使用Metpy的风向标功能,在一个包含风速和地形的图上叠加风向标。不管我怎么调整zorder(图层顺序)或者注释掉风速和地形的绘图,风向标就是不显示在图上。我定义我的子图轴是
ax = fig.add_subplot(111,projection=ccrs.Miller(central_longitude=0.0, globe=None)) 奇怪的是,如果我把风向标放在一个单独的子图上,并把它定义为 ax_barbs = fig.add_subplot(111, projection=ccrs.PlateCarree()),风向标就会出现。但是如果我试着把“ax”的投影改成“ccrs.PlateCarree”,那么其余的代码就不工作了。有人能告诉我我哪里做错了吗?我的数据分辨率很高,所以我需要使用三角剖分功能来绘制风速。这也是我想在图上每隔1/200绘制风向标的原因。完整代码如下

for i in np.arange(variable.time.size):
   fig = plt.figure(figsize=(16, 8))
   ax1=fig.add_axes([0.2,0.05,0.6,0.02])

   ax = fig.add_subplot(111,projection=ccrs.Miller(central_longitude=0.0, globe=None))
   time = data.time[i].values
   print(data.time[i].values)
   levels = np.linspace(0,35,30)
   topo_levels = np.arange(0, 4000, 700)
   cmap = plt.cm.get_cmap('BuPu')
       
   #Calculate tri and plot
   triang = tri.Triangulation(variable.lon, variable.lat)
   cmap = plt.cm.get_cmap('BuPu')
   cmap.set_under(color='white')
   plot = plt.tricontourf(triang,variable.wind_speed[i,:].data,alpha=0.70,levels=levels,cmap=cmap, transform=ccrs.PlateCarree()) 
   
       #Add topography info
   plot3 = plt.contour(topo.longitude[:],topo.latitude[:],topo.data,levels=topo_levels,colors='0.2',linewidths=0.5,alpha=0.95,transform=ccrs.PlateCarree()) 
    #Add wind barbs
   ax.barbs(variable.lon[::200].values, variable.lat[::200].values, variable.u[i,::200].data, variable.v[i,::200].data, color='black', length=5, alpha=0.5,zorder = 7)

   #add colorbar
   cbar_tickloc = [0,5,10,15,20,25,30,35]
   cbar_ticklab = ['0','5','10','15','20','25','30','35']

   cb1 = plt.colorbar(plot,extend='both',orientation='horizontal',cax=ax1,shrink=0.8,pad=0.09)
   cb1.ax.set_xlabel('wind speed (m s' + r'$^{-1}$)',fontsize=16)
   cb1.ax.tick_params(labelsize=16)

   cb1.set_ticks(cbar_tickloc)
   cb1.ax.set_xticklabels(cbar_ticklab)
   ax1.set_position([0.2, 0.25, 0.6, 0.02])  # Adjust these values as needed

   ax.set_title('IFS '+str(time)+'',fontsize=18)   
   ax.coastlines(resolution='10m',linewidth=0.6)
   ax.set_extent ((-5, 10, 34, 45), crs=ccrs.PlateCarree())

   gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,linewidth=0.7, color='gray', alpha=0.8, zorder=5, linestyle='--')   # ax.set_extent([lon[0],lon[1],lat[0],lat[1]], crs=ccrs.PlateCarree())
   gl.xlabels_bottom = False
   gl.ylabels_right = False
   gl.xlocator = mticker.FixedLocator([-10,0,10])
   gl.ylocator = mticker.FixedLocator([35,40,45,50,55])

   gl.xlabel_style = {'size': 14,}
   gl.ylabel_style = {'size': 14,}
   lat_formatter = LatitudeFormatter()
   ax.yaxis.set_major_formatter(lat_formatter)
   lon_formatter = LongitudeFormatter(zero_direction_label=True)
   ax.xaxis.set_major_formatter(lon_formatter)

   ax.tick_params(labelsize=16)
   ax.set_xlabel(None)
   ax.set_ylabel(None)
   plt.show()

1 个回答

2

在你其他的绘图方法中,你传入了 transform=ccrs.PlateCarree(),这告诉Cartopy这个绘图方法的x/y值是经度和纬度。为了让barbs方法正常工作,你需要做类似的事情:

ax.barbs(variable.lon[::200].values, variable.lat[::200].values,
    variable.u[i,::200].data, variable.v[i,::200].data, color='black',
    length=5, alpha=0.5, transform=ccrs.PlateCarree())

撰写回答