【问题标题】:Twin y axes plot with monthly average as x-axis over multiple years with xarray使用 xarray 多年的双 y 轴图,将月平均值作为 x 轴
【发布时间】:2021-10-09 05:54:00
【问题描述】:

感谢您对这篇文章感兴趣。

我希望创建一个双 y 轴图来说明 2 个变量(例如 y1=融雪和 y2=排放径流)。由于我刚开始学习编码,我对 python 的了解非常有限,我很困惑如何去做。

我能够构建一个双 y 轴图和一个表示多年月平均值的图。但是,我不确定如何将这些 matplotlib 代码组合在一起,并创建一个双 y 轴图,并将月平均值作为多年的 x 轴。

月平均代码


disda = xr.open_mfdataset(sorted(['1980.nc','1981.nc', '1982.nc', '1983.nc','1984.nc','1985.nc','1986.nc','1987.nc', '1988.nc','1989.nc', '1990.nc', '1991.nc', '1992.nc', '1993.nc', '1994.nc', '1996.nc', '1997.nc', '1998.nc','1999.nc']))
snowdepth_we = xr.open_dataarray('Snow depth water equivalent.nc')


disyrs = disda.sel(time=slice('1981','1999'))
snyrs = snowdepth_we.sel(time=slice('1981','1999'))


dismonthlymean = disyrs.dis24

dislatlonmean = dismonthlymean.mean(dim=['latitude','longitude']).resample(time='M').sum()
snowlatlonmean = snmonthlymean.mean(dim=['latitude','longitude'])


disgroupby = dislatlonmean.groupby("time.month").mean("time")
sngroupby = snowlatlonmean.groupby("time.month").mean("time")

#graph commands
myfig, myax = plt.subplots(figsize=(12,6))

    
TYs = np.unique(dislatlonmean["time.year"])
disgroupby.plot.line('b-', color='blue', linestyle='-', linewidth=4, label='Discharge Mean')
for YY in TYs: 
    plt.plot(np.arange(1,13), dislatlonmean.sel(time=YY.astype("str")), 'b-', color='blue', alpha=0.2)        

TYs = np.unique(dislatlonmean["time.year"])
sngroupby.plot.line('b-', color='red', linestyle='-', linewidth=4, label='Snowdepth Mean')
for YY in TYs: 
    plt.plot(np.arange(1,13), dislatlonmean.sel(time=YY.astype("str")), 'b-', color='blue', alpha=0.2)        
        

    
myax.set_title('Western Himalayas Snow Depth and River Indus Discharge Run-off 1981-1999')
myax.set_ylabel('m of water equivalent')
myax.set_xlabel('Month')
myax.set_xticks(range(1, 13))
myax.set_xticklabels(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'])
myax.grid()
myax.legend(['Discharge Mean','Snowdepth'])

由于这不是双 y 轴图,我无法将 2 个具有不同测量值的变量一起绘制,其中一个变量在图中变成一条平线,位于另一个图下方

另一方面,我能够使用这些命令创建一个双 y 轴图

disda_dis248199 = disda_dis24.loc['1981':'1999'].mean(dim=['latitude','longitude']).resample(time='M').mean()
snow8199 = snowdepth_we.loc['1981':'1999'].mean(dim=['latitude','longitude']).resample(time='M').mean()

x = disda_dis248199.time
y1 = snow8199
y2 = disda_dis248199

#plotting commands
fig, ax1 = plt.subplots(figsize=(14,8))

ax1.set_xlabel('Year')
ax1.set_ylabel('Snow depth water equivalent (m of w.e)')
ax1.plot(x, y1, color='red', label='River Discharge')
ax1.tick_params(axis='y', labelcolor='red')

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

ax2.set_ylabel('River Discharge (m3s−1)')  # we already handled the x-label with ax1
ax2.plot(x, y2, color='blue',alpha=0.4, label='Snow depth water equivalent')
ax2.tick_params(axis='y', labelcolor='blue')

fig.legend(loc="upper right")
ax1.set_title('Western Himalayas Snow Depth and River Indus Tributaries Comparison 1981 - 2016')
ax2.grid()

我曾尝试将这两个命令合并在一起,但无济于事。

因此,我想知道是否有任何方法可以绘制像第二张图一样的双 y 轴图,但以月份为 x 轴,图上的两条线将代表 2 个不同的变量。

此外,正如您在图 1 中看到的那样,不知何故,图例仅显示了 1981-1999 年的排放平均值(粗线)和实际年平均值(细线),因此,我想知道我如何能够解决这个问题?

非常感谢您的帮助!!如果您需要更多信息,请告诉我,我会尽快回复。

【问题讨论】:

    标签: python matplotlib plot python-xarray timeserieschart


    【解决方案1】:

    由于您只提供了部分代码,我无法运行它,因此也无法为您更改它。但是,我将尝试解释第一段代码中应该更改的内容。

    作为第一步,您要定义第二个 Axes 对象,类似于您在第二段代码中的做法:

    myax2 = myax.twinx()
    

    由于您现在有两个Axes 实例,因此您需要指定要在哪个Axes 实例(myaxmyax2)上绘制数据。这意味着,而不是

    disgroupby.plot.line('b-', color='blue', linestyle='-', linewidth=4, label='Discharge Mean')
    sngroupby.plot.line('b-', color='red', linestyle='-', linewidth=4, label='Snowdepth Mean')
    

    你应该做一些事情,比如

    disgroupby.plot.line('b-', ax=myax, color='blue', linestyle='-', linewidth=4, label='Discharge Mean')
    sngroupby.plot.line('b-', ax=myax2, color='red', linestyle='-', linewidth=4, label='Snowdepth Mean')
    

    注意在(关键字)参数列表中添加了ax=...。有关示例,请参阅this part of xarray's documentation。此外,您不再想使用plt.plot,因为它没有指定在哪个Axes 实例上绘制数据。相反,您想调用要在其上绘制数据的Axes 实例的plot 方法。例如,第一次出现的

    plt.plot(np.arange(1,13), dislatlonmean.sel(time=YY.astype("str")), 'b-', color='blue', alpha=0.2)  
    

    应该改为

    myax.plot(np.arange(1,13), dislatlonmean.sel(time=YY.astype("str")), 'b-', color='blue', alpha=0.2)
    

    第二次出现应该改为

    myax2.plot(np.arange(1,13), dislatlonmean.sel(time=YY.astype("str")), 'b-', color='blue', alpha=0.2)
    

    设置标签、标题等与您在第二段代码中的操作类似。

    关于您的plt.plot 语句的小注释,您在参数列表中有..., 'b-', color='blue', ...。虽然这有效,但您指定了两次颜色。您可以删除color='blue' 部分或将'b-' 更改为'-'

    【讨论】:

    • 效果很好!非常感谢您的帮助!我真的很感激!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 2021-05-02
    • 2016-06-12
    • 2019-10-09
    相关资源
    最近更新 更多