如何将y轴与辅助y轴切换?

时间:2020-05-19 09:28:43

标签: python-3.x

我面临一个小问题(美学),但我找不到答案。

我有一个x轴和两个y轴的绘图(请参见下图)。我希望黑线位于条形图的顶部,以便您可以清晰地阅读图形。但是,为此,我在ax1上绘制了条形图。因此,第一个y轴显示了第二重要的条形图值。我想在将条形图保持在“背景”的同时切换两个y轴。我该怎么做呢?我的代码显示在图像下方。

提前谢谢! plot 代码:

 fig,ax1=plt.subplots(figsize=(11,8))
 ax2=ax1.twinx()

 ax1.bar(Ballingho_monthly.index, qdi_bar["sum_discharge"].iloc[3:56], width = 15, color='lightblue', label='discharge')
 ax2.plot(Ballingho_monthly.index, Ballingho_monthly, 'k-', label='measurements')
 ax2.plot(Ballingho_monthly.index, salinity.iloc[11:170:3,64], 'k:', label='model')

 plt.title('Salinity measurements and prediction at Balingho')
 ax1.set_ylabel('Monthly discharge [m3/s]', color='black')
 ax2.set_ylabel('Salinity [g/kg]', color='black')
 plt.gcf().autofmt_xdate()
 plt.savefig(save_im + str('/measurements-prediction'))

 h1, l1 = ax1.get_legend_handles_labels()
 h2, l2 = ax2.get_legend_handles_labels()
 ax1.legend(h1+h2, l1+l2, loc=9)

 plt.show()

0 个答案:

没有答案