【问题标题】:TypeError: can't convert type 'MaskedArray' to numerator/denominatorTypeError:无法将类型“MaskedArray”转换为分子/分母
【发布时间】:2022-01-17 08:17:07
【问题描述】:
>>> i = 0
>>> j = 26
>>> sla = nc['sla']
>>> lon = nc['longitude'][:]
>>> lat = nc['latitude'][:]
>>> cs = ax.contourf(lon, lat, mean(sla[i:i+j])*100, levels = 25, cmap = 'bwr', transform=ccrs.PlateCarree())

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\ProgramData\Miniconda3\lib\statistics.py", line 316, in mean
    T, total, count = _sum(data)
  File "C:\ProgramData\Miniconda3\lib\statistics.py", line 166, in _sum
    for n, d in map(_exact_ratio, values):
  File "C:\ProgramData\Miniconda3\lib\statistics.py", line 248, in _exact_ratio
    raise TypeError(msg.format(type(x).__name__))
TypeError: can't convert type 'MaskedArray' to numerator/denominator

谁能帮助恢复这个错误?

【问题讨论】:

标签: python error-handling netcdf


【解决方案1】:

如果不访问数据,就无法重现您的错误。无论如何,您可以通过更简单的方式获得相同的结果:

da = nc.isel(time = slice(i, i + j)) * 100
da.plot.contourf(levels = 25, cmap = 'bwr')

您可以阅读the doc on xarray and contourf here

请注意,根据您要绘制的内容,使用 contourf 可能是一种不好的做法,因为它不允许用户了解原始数据的空间分辨率。我建议改用 pcolormesh,您可以使用 da.plot()

【讨论】:

  • 很好的帮助....我会检查并告诉你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-18
  • 1970-01-01
  • 2021-10-21
  • 2018-06-11
  • 2021-04-13
相关资源
最近更新 更多