将两个y轴刻度添加到同一图表中

时间:2017-06-28 19:30:16

标签: python pandas datetime matplotlib

我想配置y轴,以便更可解释这些值。 I would like to configure the y-axis so the values are more interpretable. Preferably I would have 'mean_Kincaid' on the left y-axis and 'mean_Score' on the right y-axis.

这是我尝试过的代码:

ax = Statements.plot(x='Date', y='mean_Kincaid', legend=True, 
title="Fed Statement Kincaid and Sentiment scores over time")
Statements.plot(x='Date', y='mean_Score', ax=ax)
Statements.plot(secondary_y='mean_Kincaid', style='g', ax=ax)

Resulting in:

运行时:

ts = Statements.set_index('Date')
ts.mean_Kincaid.plot()
ts.mean_Score.plot(secondary_y=True)

我明白了:

enter image description here

这与我原来的图表有很大不同。造成这种情况的原因是什么?如何解决?

1 个答案:

答案 0 :(得分:1)

您正在寻找secondary_y关键字参数。

ts = Statements.set_index('Date')
ts.mean_Kincaid.plot()
ts.mean_Score.plot(secondary_y=True)

如果您想要显示图例和轴标签,请使用

之类的内容
ts = Statements.set_index('Date')
ax = ts.mean_Kincaid.plot(legend=True)
ts.mean_Score.plot(legend=True, secondary_y=True)
ax.set_ylabel('mean_Kincaid')
ax.right_ax.set_ylabel('mean_Score')