Python 向同一图形添加两个y轴比例

Python 向同一图形添加两个y轴比例,python,pandas,datetime,matplotlib,Python,Pandas,Datetime,Matplotlib,我想配置y轴,使值更易于解释 这是我尝试的代码: 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='

我想配置y轴,使值更易于解释

这是我尝试的代码:

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)

运行时:

ts = Statements.set_index('Date')
ts.mean_Kincaid.plot()
ts.mean_Score.plot(secondary_y=True)
我得到:

这和我原来的图表有很大的不同。是什么导致了这种情况,我如何修复它?

您正在查找关键字参数

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')

谢谢,但这似乎扭曲了我的数据。我更新了我的问题,你知道发生了什么吗?谢谢你,我猜这是在用不同的比例画线。如何将刻度标记为其适当的变量?此外,如果你知道如何改变颜色(也许是蓝色虚线而不是橙色虚线),并添加一个传奇,这将是伟大的!谢谢@Graham我添加了一个示例,演示如何标记轴和添加图例。我不确定你所说的“扭曲我的数据”是什么意思。你的图在我看来很好,两个系列只是在不同的比例上,这是一个次轴的整个点。请通读,以获得替代线条样式的示例。谢谢,很抱歉提出样式问题!