2

我目前正在尝试将一些数据绘制到 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:输入 z 必须是二维数组。

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

4

1 回答 1

2

我怀疑您必须重塑您的 numpy 数组才能将其与contour方法一起使用。使用您的变量名可以这样做:

ann_total_5tg = ann_total_5tg.reshape((180, 180))
于 2019-12-25T09:39:50.327 回答