2017-06-28 61 views
1

我想配置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.将两个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) 

Resulting in:

当运行:

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

我得到:

enter image description here

这与我的原始图大不相同。这是什么原因造成的,我该如何解决?

+1

https://pandas.pydata.org/pandas-docs/stable/visualization.html#plotting -on-a-secondary-y-axis? – MaxU

回答

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

谢谢,但这似乎扭曲了我的数据。我已经更新了我的问题,你知道发生了什么吗?再次感谢 –

+0

我想它是以不同的比例绘制线条。我如何将磅秤标记为适当的变量?此外,如果你知道如何改变颜色(或许是一条蓝色虚线而不是橙色线)并添加一个很棒的传奇!谢谢! –

+1

@Graham我已经添加了一个示例,显示如何标记轴并添加图例。我不确定你的意思是“扭曲我的数据”。你的情节对我来说很好看,这两个系列只是在不同的尺度上,这是一个辅助轴的整个点。 –