绘制 Xarray 全局数据的经纬度子集

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

我在试图从 python 数组中绘制经纬度数据的子集时陷入困境。我有一个维度的 xarray,我们可以将其称为 (73, 144) 的“global_data”,其中数据按经纬度 (73) 和经度 (144) 排列,它用我的代码为整个地球绘制了一个漂亮的图。然而,我需要选择这个经纬度数据的子集,以便它在给定的范围内绘制(仅适用于美国)。 “global_data”看起来像这样:

<xarray.DataArray (lat: 73, lon: 144)>
array([[ 1.1423118 ,  1.1418152 ,  1.1437986 , ...,  1.1403227 ,
         1.1362497 ,  1.1439507 ],
       [ 0.49379024,  0.42622158,  0.3565474 , ...,  0.70473236,
         0.63061965,  0.5644286 ],
       [ 0.1380711 ,  0.19678137,  0.2836361 , ...,  0.24298143,
         0.16488086,  0.12564908],
       ...,
       [ 0.18887411,  0.25456694,  0.30384657, ..., -0.08306076,
         0.01299069,  0.10468105],
       [ 0.37176454,  0.4389612 ,  0.50888765, ...,  0.16366327,
         0.23381352,  0.30179456],
       [ 0.6100794 ,  0.61286193,  0.6167843 , ...,  0.6154521 ,
         0.6117071 ,  0.610261  ]], dtype=float32)
Coordinates:
  * lat      (lat) float32 90.0 87.5 85.0 82.5 80.0 ... -82.5 -85.0 -87.5 -90.0
  * lon      (lon) float32 0.0 2.5 5.0 7.5 10.0 ... 350.0 352.5 355.0 357.5

我需要一个新的 xarray“usa_data”,其边界为 lat_range = 25, 50 和 lon_range = -125, -66。我尝试使用 .sel、.isel 和切片来处理上面的数据,但我要么没有数据可以绘制,要么在一个完全令人惊讶的地方绘制数据,例如“非洲”,我认为是美国坐标。谢谢您的救命!!

我在这里包含了我的第一个全球数据图:

这是使用“isel”绘制子集的尝试,但最终的美国地图是通过反复试验获得的,我不确定它是否正确或为什么下面的坐标对于美国地图“有效”。有什么想法吗?

usa_data = global_data.isel(lon=slice(-55,-23),lat=slice(13,29))
ax = plt.axes(projection=ccrs.PlateCarree())
contourf = anomspeed_us.plot.contourf(
    ax=ax, levels=np.arange(-5,5.1,0.5),extend=
    'both',cmap='RdBu_r',
    add_colorbar=True,
    cbar_kwargs={'label': 'Anomalous Wind Speed'})
ax.coastlines()
states = cfeature.NaturalEarthFeature(category='cultural', 
                                      name='admin_1_states_provinces_lines',
                                      scale='50m', facecolor='none')
ax.add_feature(states, linewidth=0.5, edgecolor='black')
ax.add_feature(cfeature.BORDERS, linestyle='-')
ax.gridlines()
plt.title('Anomalous Wind Speed for Week')
plt.show()

python plot subset coordinates python-xarray
1个回答
0
投票

您应该使用 Xarray 的

.sel()
方法,该方法用于基于坐标的选择。

# lat_range = 25, 50 and lon_range = -125, -66 (or 235, 294)
regional_data = global_data.isel(
    lon=slice(360-125, 360-23), lat=slice(25, 50)
)

regional_data.plot.contourf(...)
© www.soinside.com 2019 - 2024. All rights reserved.