简体   繁体   中英

TypeError: can't convert type 'MaskedArray' to numerator/denominator

>>> 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

Can anyone help to recover this error?

It is not possible to reproduce your error without access to the data. Regardless, you could get the same result in a simpler way:

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

You can read the doc on xarray and contourf here .

Note that depending on what you are plotting, the use of contourf may be a bad practice as it doesn't allow the user to understand the spatial resolution of the original data. I would recommand to use pcolormesh instead, you can do it with da.plot() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM