Graham Streich
Graham Streich

Reputation: 924

Add two y-axis scales to the same graph

I would like to configure the y-axis so the values are more interpretable. 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.

This is the code I tried:

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:

When running:

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

I get:

enter image description here

Which is much different from my original graph. What is causing this and how do I fix it?

Upvotes: 1

Views: 255

Answers (1)

Igor Raush
Igor Raush

Reputation: 15240

You are looking for the secondary_y keyword argument.

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

If you'd like to display a legend and axis labels, use something like

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

Upvotes: 1

Related Questions