在 python 中无法使用箭袋绘制风向量
Cannot draw wind vectors using quiver in python
我试图在 Python 中使用箭袋绘制风矢量,但出现
错误
TypeError: Argument 'x' has incorrect type (expected numpy.ndarray,
got DataArray)
如何解决这个问题?
我的密码是
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np
#%% u wind import
fname='/home/SIMS/P1/uwind/*.nc'
du=xr.open_mfdataset(fname)
uwnd=du.uwnd
uwind=du.mean('time')
#%% v wind import
fname='/home/SIMS/P1/vwind/*.nc'
dv=xr.open_mfdataset(fname)
#ds=xr.open_mfdataset(fname,concat_dim="time")
vwnd=dv.vwnd
vwind=dv.mean('time')
lat = du.uwind.lat
lon = du.uwind.lon
u=uwind.uwnd[0,:,:]
v=vwind.vwnd[0,:,:]
print(lat.shape)
print(lon.shape)
print(u.shape)
print(v.shape)
lon,lat = np.meshgrid(lon,lat)
ax = plt.axes(projection=ccrs.Mercator(central_longitude=80.0, min_latitude=-10.0,
max_latitude=20.0, globe=None, latitude_true_scale=0.0))
ax.coastlines()
ax.coastlines()
ax.quiver(lat,lon,u,v)
plt.show()
lat,lon,u,v 的形状如下
(15,)
(73,)
(15, 73)
(15, 73)
经
lon Out[192]: array([[ 50. , 52.5, 55. , ..., 225. , 227.5, 230. ],
[ 50. , 52.5, 55. , ..., 225. , 227.5, 230. ],
[ 50. , 52.5, 55. , ..., 225. , 227.5, 230. ],
...,
[ 50. , 52.5, 55. , ..., 225. , 227.5, 230. ],
[ 50. , 52.5, 55. , ..., 225. , 227.5, 230. ],
[ 50. , 52.5, 55. , ..., 225. , 227.5, 230. ]], dtype=float32)
正在绘制没有任何风向量的空白图像
当我尝试使用经度、纬度、u 和 v 的 .values 时,我可以绘制它。
lat = du.uwnd.lat.values
lon = du.uwnd.lon.values
u=anomaly_u.uwnd.values[0,:,:]
v=anomaly_v.vwnd.values[0,:,:]
我试图在 Python 中使用箭袋绘制风矢量,但出现
错误TypeError: Argument 'x' has incorrect type (expected numpy.ndarray, got DataArray)
如何解决这个问题?
我的密码是
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np
#%% u wind import
fname='/home/SIMS/P1/uwind/*.nc'
du=xr.open_mfdataset(fname)
uwnd=du.uwnd
uwind=du.mean('time')
#%% v wind import
fname='/home/SIMS/P1/vwind/*.nc'
dv=xr.open_mfdataset(fname)
#ds=xr.open_mfdataset(fname,concat_dim="time")
vwnd=dv.vwnd
vwind=dv.mean('time')
lat = du.uwind.lat
lon = du.uwind.lon
u=uwind.uwnd[0,:,:]
v=vwind.vwnd[0,:,:]
print(lat.shape)
print(lon.shape)
print(u.shape)
print(v.shape)
lon,lat = np.meshgrid(lon,lat)
ax = plt.axes(projection=ccrs.Mercator(central_longitude=80.0, min_latitude=-10.0,
max_latitude=20.0, globe=None, latitude_true_scale=0.0))
ax.coastlines()
ax.coastlines()
ax.quiver(lat,lon,u,v)
plt.show()
lat,lon,u,v 的形状如下
(15,)
(73,)
(15, 73)
(15, 73)
经
lon Out[192]: array([[ 50. , 52.5, 55. , ..., 225. , 227.5, 230. ], [ 50. , 52.5, 55. , ..., 225. , 227.5, 230. ], [ 50. , 52.5, 55. , ..., 225. , 227.5, 230. ], ..., [ 50. , 52.5, 55. , ..., 225. , 227.5, 230. ], [ 50. , 52.5, 55. , ..., 225. , 227.5, 230. ], [ 50. , 52.5, 55. , ..., 225. , 227.5, 230. ]], dtype=float32)
正在绘制没有任何风向量的空白图像
当我尝试使用经度、纬度、u 和 v 的 .values 时,我可以绘制它。
lat = du.uwnd.lat.values
lon = du.uwnd.lon.values
u=anomaly_u.uwnd.values[0,:,:]
v=anomaly_v.vwnd.values[0,:,:]