【问题标题】:Add two y-axis scales to the same graph将两个 y 轴刻度添加到同一个图表
【发布时间】:2017-12-02 07:44:51
【问题描述】:

我想配置 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)

我明白了:

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

【问题讨论】:

标签: python pandas datetime matplotlib


【解决方案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')

【讨论】:

  • 谢谢,但这似乎扭曲了我的数据。我已经更新了我的问题,你知道发生了什么吗?再次感谢
  • 我猜它是以不同的比例绘制线条。如何将比例标记为正确的变量?此外,如果您知道如何更改颜色(可能是蓝色虚线而不是橙色虚线)并添加一个很棒的图例!谢谢!
  • @Graham 我添加了一个示例,该示例显示了如何标记轴并添加图例。我不确定您所说的“扭曲我的数据”是什么意思。你的情节对我来说看起来不错,这两个系列只是在不同的尺度上,这是第二轴的全部点。
  • 请通读docs 了解替代线条样式的示例。
  • 谢谢,很抱歉提出风格问题!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-01
  • 1970-01-01
  • 2017-03-28
相关资源
最近更新 更多