我目前正在尝试将一些数据绘制到 cartopy 中,但遇到了一些问题。
我有一个数据表,其形状分别为 (180, 180, 360) 时间、纬度和经度。
我想获得该数据的年平均值。我一直在使用代码
def global_mean_3D(var, weights):
# make sure masking is correct, otherwise we get nans
var = np.ma.masked_invalid(var)
# resulting variable should have dimensions of depth and time (x)
ave = np.zeros([var.shape[0], var.shape[1]])
# loop over time
for t in np.arange(var.shape[0]):
# loop over each depth slice
for d in np.arange(var.shape[1]):
ave[t,d] = np.ma.average(var[t,d,:], weights = weights)
return ave
然后我用它来绘制
ax=plt.axes(projection=ccrs.Robinson())
ax.coastlines()
ax.contourf(x,y, ann_total_5tg)
但是随着时间的推移,这段代码给了我一个一维形状,我无法使用 pcolor 网格将其绘制到 cartopy 中。
我留下了错误
TypeError: Input z must be a 2D array.
是否有可能获得年度平均值,同时保留数据表中的变量?
最佳答案
我怀疑你必须reshape你的 numpy 数组将其与 contour
method 一起使用。
使用您的变量名称可以这样完成:
ann_total_5tg = ann_total_5tg.reshape((180, 180))
关于python - 平均数据集同时保持其变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59475829/