【发布时间】: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