平均数据集,同时保持其变量?

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

我目前正在尝试将一些数据绘制到Cartopy中,但是我遇到了一些问题。

我有一个数据表,其形状分别为(180、180、360)time,lat和lon。

我想获得这些数据的年度平均值。我一直在使用代码

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:输入z必须是2D数组。

是否有可能在保持数据表内变量的同时获得年度平均值?

python python-3.x numpy cartopy datasheet
1个回答
0
投票

我怀疑您必须reshape您的numpy数组才能与contour method一起使用。使用您的变量名可以像这样完成:

contour
© www.soinside.com 2019 - 2024. All rights reserved.