【问题标题】:Average a Data Set while maintaining its variables?在保持变量的同时平均数据集?
【发布时间】:2020-04-15 22:52:16
【问题描述】:

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

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

【问题讨论】:

    标签: python python-3.x numpy cartopy datasheet


    【解决方案1】:

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

    ann_total_5tg = ann_total_5tg.reshape((180, 180))
    

    【讨论】:

      猜你喜欢
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 2017-09-14
      相关资源
      最近更新 更多