簡體   English   中英

XARRAY 等高線圖 AttributeError: Contourf

[英]XARRAY Contour Plot AttributeError: Contourf

我正在嘗試使用 NCEP Reanalysis netcdf4 數據在全局地圖上以等高線形式繪制 xarray 變量。 我使用 xarray 計算變量“slp”的標准化異常,方法是將本月的 2 月與 2016 年至 2019 年的去年 2 月的平均值進行比較。 我能夠獲得最終變量 (stdn" 作為 xarray.core.dataset。但是,我的錯誤消息如下所示,我已將 matplotlib 更新到當前版本 3.2。我一直在搜索此錯誤,但沒有任何幫助。謝謝你!我的代碼如下,我不斷收到這個錯誤 - AttributeError: '_Dataset_PlotMethods' object has no attribute 'contourf'。

from sys import exit
import netCDF4 as nc4
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm
import datetime 
import pandas as pd
import xarray as xr
import bottleneck as bn
import cartopy.crs as ccrs

#The easiest way to read the data is:
path = "//porfiler03/gtdshare/IDL/wtypes/data/slp.2020.nc"
DSC = xr.open_mfdataset(path,combine='nested',concat_dim='time')
DSC = DSC.sel(time=slice('2020-02-01','2020-02-29'))
damc = DSC.groupby('time.month').mean(dim='time')#current year/month Feb 2020 mean slp

mfdataDIR = "//porfiler03/gtdshare/IDL/wtypes/data/avgyears/*.nc"
DS = xr.open_mfdataset(mfdataDIR,combine='nested',concat_dim='time')
da = DS.slp.ffill(dim='time')
da = da.sel(time=slice('2016-01-01','2019-12-31'))
dam = da.groupby('time.month').mean(dim='time')# monthly mean of 2016-2019 dim 12x73x144
damcm = dam.sel(month=slice('2'))#current month mean slp
dams = da.groupby('time.month').std(dim='time')#
damscm = dams.sel(month=slice('2'))#current month stdev slp

stdn = (damc - damcm)/ damscm#standard normal for Feb 2020 
#
stdn.plot.contourf()

exit()

# same error using the below code
ax = plt.axes(projection=ccrs.Orthographic(-80, 35))
ax.set_global()
stdn.plot.contourf(ax=ax, transform=ccrs.PlateCarree())
ax.coastlines()

或者,我已經嘗試過這個,但出現以下錯誤:

stdn.plot(robust=True, figsize=(10,6))

ValueError:無法直接調用 Dataset.plot。 使用顯式繪圖方法,例如 ds.plot.scatter(...)

為了充分利用 xarray 的繪圖功能,最好先從stdn提取底層DataArray 然后你就可以只做da_stdn.plot()並且它應該可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM