matplotlib 从netcdf文件绘制quiver时出错

alen0pnh  于 2023-01-17  发布在  Etcd
关注(0)|答案(1)|浏览(198)

我试图从NetCDF文件中绘制风的 Flutter ,但是,我只能绘制一行 Flutter ,直接穿过底图,如图所示。代码如下。非常感谢您的帮助:)
数据在这里,请替换为onedrive文件夹,谢谢https://drive.google.com/file/d/160121aFx0Ys6G1jdQZOCT2Ve9eZgOyUy/view?usp=sharing

import xarray as xr
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

data = xr.open_dataset(<Insert google drive file data here>)

data = data.sel(time='2010-04-14T00:00:00.000000000')

X = data.longitude; Y = data.latitude
U = data.u10[200]; V = data.v10[200]

plt.figure()
ax = plt.subplot(111,projection=ccrs.PlateCarree())

ax.quiver(X[::5], Y[::5], U[::5], V[::5],color='green')

plt.show()

我希望所有的箭图都被绘制出来,所以图中应该充满了绿色箭头
当前打印的图像如下:

nmpmafwu

nmpmafwu1#

你只需要从data.u10[200]的最后一行获取数据。而不是箭袋,使坐标为2D数组和绘图,例如每5个点的数据集。这是我的解决方案,我下载并保存您的数据为“exdata.nc“。

#!/usr/bin/env ipython
# --------------------
import xarray as xr
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import numpy as np
# --------------------------------------------------------------------
data = xr.open_dataset('exdata.nc')

data = data.sel(time='2010-04-14T00:00:00.000000000')

X = data.longitude; Y = data.latitude
# --------------------------------------------------------------------
XM,YM = np.meshgrid(X,Y)
U = data.u10; V = data.v10
skipx, skipy = 5,5
# ----------------------------------------------------------------
plt.figure(figsize=(12,12))
ax = plt.subplot(111,projection=ccrs.PlateCarree())

ax.quiver(XM[::skipy,::skipx], YM[::skipy,::skipx], U[::skipy,::skipx], V[::skipy,::skipx],color='green')
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
plt.show()
# ---------------------------------

希望这作为一个例子有帮助!

相关问题