我使用 Metpy 的风刺没有出现在我的绘图上

问题描述 投票:0回答:1

我正在尝试使用 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()
python plot cartopy
1个回答
0
投票

在其他绘图方法中,您传递

transform=ccrs.PlateCarree()
,它告诉 Cartopy 将该绘图方法的 x/y 值解释为 lon/lat。要使倒刺方法发挥作用,您需要执行类似的操作:

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())
© www.soinside.com 2019 - 2024. All rights reserved.