Invert secondary Y axis?

1,123 views
Skip to first unread message

dartdog

unread,
Jan 22, 2013, 4:45:42 PM1/22/13
to pyd...@googlegroups.com
I have two variables (that are inversely correlated and I'm using the secondary plot axis on the right ie
df.total.plot()
df.UnemplRt.plot(secondary_y=True, style='g')

But I'd like the secondary_y to be inverted To show the correlation better.

I see in matplotlib there is plt.gca().invert_yaxis() 

Can I use that somehow?

Skipper Seabold

unread,
Jan 22, 2013, 5:51:39 PM1/22/13
to pyd...@googlegroups.com
That should work AFAIK. You might need to call draw_if_interactive to have it show up.

Something like this should also work and is a bit more explicit.

ax = df.a.plot()
ax = df.b.plot(secondary_y=True, style='g', ax=ax)
ax.invert_yaxis()

If you're working interactively, you might need to call

plt.draw_if_interactive()


Skipper

dartdog

unread,
Jan 22, 2013, 6:34:30 PM1/22/13
to pyd...@googlegroups.com
Perfect, I did not realize I needed to assign the plot to ax
so this did it as suggested
ax=df.total.plot()
ax=df.UnemplRt.plot(secondary_y=True, style='g')
ax.invert_yaxis()

dartdog

unread,
Jan 22, 2013, 6:39:09 PM1/22/13
to pyd...@googlegroups.com
Darn It is inverting the primary Y not the secondary, how do I target the secondary?

Skipper Seabold

unread,
Jan 22, 2013, 6:55:57 PM1/22/13
to pyd...@googlegroups.com
On Tue, Jan 22, 2013 at 6:39 PM, dartdog <tombr...@gmail.com> wrote:
Darn It is inverting the primary Y not the secondary, how do I target the secondary?


Hmm, right. Since pandas decided to return the axes instead of the figure, you need to get the figure.

ax = df.a.plot()
ax = df.b.plot(secondary_y=True, style='g', ax=ax)
fig = ax.get_figure()
fig.axes[1].invert_yaxis()
plt.draw_if_interactive()

Skipper

 
--
 
 

dartdog

unread,
Jan 22, 2013, 8:23:42 PM1/22/13
to pyd...@googlegroups.com
great, thanks!
Reply all
Reply to author
Forward
0 new messages