爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 7546|回复: 4

[求助] python eof

[复制链接]

新浪微博达人勋

发表于 2022-4-20 22:14:55 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
只出了第一模态的图,后面的图没出来

hgt = xr.open_dataset(r"D:\python\hgt.mon.mean.nc").hgt.loc["1991-01-01":"2020-12-31",'500',70:20,40:140]
hgt_ano=hgt.loc[hgt.time.dt.month==1]-hgt.loc[hgt.time.dt.month==1].mean('time')

llat = np.array(hgt_ano.lat)
coslat =np.cos(np.deg2rad(llat))#b度权重
wgts =np.sqrt(coslat)[...,np.newaxis]#加权
solver = Eof(hgt_ano.values, weights=wgts)#加权eof
eof = solver.eofsAsCorrelation(neofs=3)#做前3个模态
pc = solver.pcs(npcs=3, pcscaling=1)#方差
var = solver.varianceFraction(neigs=3)

color1=[]
color2=[]
color3=[]
for i in range(len(hgt_ano.time)):
    if pc[i,0] >=0:
        color1.append('indianred')
    elif pc[i,0]<0:
        color1.append('teal')
    if pc[i,1]>=0:
        color2.append('indianred')
    elif pc[i,1]<0:
        color2 .append('teal')
    if pc[i,2]>=0:
        color3. append('indianred')
    elif pc[i,2]<0:
        color3 .append('teal')

def eof_draw(ax,data,eofs,title,levels,colors,lon,lat):
    lon_formatter = LongitudeFormatter(zero_direction_label=False)
    lat_formatter = LatitudeFormatter()
    ax[0].coastlines('110m')
    ax[0].set_extent([40,140,20,70], crs=ccrs.PlateCarree())
    ax[0].set_xticks([40,70,100,130], crs=ccrs.PlateCarree())
    ax[0].set_yticks([30,50,70], crs=ccrs.PlateCarree())
    C1=ax[0].contourf(lon[::],lat[::],data[0][eofs],
                      levels=levels,extend='both',cmap="RdBu_r",
                      transform=ccrs.PlateCarree())#画填色图
    ax[0].xaxis.set_major_formatter(lon_formatter)#修改经纬度表达形式
    ax[0].yaxis.set_major_formatter(lat_formatter)
    ax[0].set_title(title[0],loc='left',fontweight='heavy')
    ax[0].set_title('%.2f%%' % (var[eofs]*100),loc='right',fontweight='heavy')

    ax[1].set_title(title[1],loc='left',fontweight='heavy')
    ax[1].axhline(0,linestyle="--")
    ax[1].bar(np.arange(data[1].shape[0]),data[1][:,eofs],color=colors)
    ax[1].set_xticks(np.arange(1,data[1].shape[0],5))
    ax[1].set_xticklabels(np.arange(1980,2020+5,5))
    return C1

plt.rcParams['font.sans-serif']='Times New Roman'
plt.style.use('seaborn')
mpl.rc('font',size=20,weight='normal')#设理全局字体大小

fig = plt.figure(figsize=(15,15))
proj = ccrs.PlateCarree(central_longitude=200)

fig_ax1 = fig.add_axes([0.1,0.7,0.5,0.3],projection = proj)
fig_ax2 = fig.add_axes([0.1,0.35,0.5,0.3],projection = proj)
fig_ax3 = fig.add_axes([0.1,0.0,0.5,0.3],projection = proj)

fig_ax4 = fig.add_axes([0.1+0.55,0.725,0.47,0.25])
fig_ax5 = fig.add_axes([0.65,0.375,0.47,0.25])
fig_ax6 = fig.add_axes([0.65,0.025,0.47,0.25])

c1 = eof_draw([fig_ax1,fig_ax4],[eof,pc],0,["(a) EOF1","(b) PC1"],np.arange(-1,1.01,0.1),color1,hgt.lon,hgt.lat)
c2 = eof_draw([fig_ax2,fig_ax5],[-eof,-pc],1,["(c) EOF2", "(d) PC2"],np.arange(-1,1.01,0.1),color2,hgt.lon,hgt.lat)
c3 = eof_draw([fig_ax3,fig_ax6],[-eof,-pc],2,["(e) EOF3","(f) Pc3"],np.arange(-1,1.01,0.1),color3,hgt.lon,hgt.lat)
cax = plt.axes([0.1,-0.05,1,0.02])
cbar=plt.colorbar(c1,cax=cax, orientation='horizontal')


密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2022-4-21 11:43:46 | 显示全部楼层

回帖奖励 +2 金钱

错误出现在ax[1].set_xticklabels(np.arange(1980,2020+5,5)),5可改成8.
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2022-4-21 11:54:09 | 显示全部楼层
另外建议以后可以附上报错原因,更加便捷方便。
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2022-6-30 16:49:17 | 显示全部楼层

回帖奖励 +2 金钱

少年易 发表于 2022-4-21 11:54
另外建议以后可以附上报错原因,更加便捷方便。

ax[1].set_xticks(np.arange(1,data[1].shape[0]+5,5))
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2022-6-30 16:53:45 | 显示全部楼层
ax[1].set_xticks(np.arange(1,data[1].shape[0]+5,5))
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表