Skip to content

Plotting secondary axis #9302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JohnNapier opened this issue Jan 19, 2015 · 5 comments
Closed

Plotting secondary axis #9302

JohnNapier opened this issue Jan 19, 2015 · 5 comments
Labels
Milestone

Comments

@JohnNapier
Copy link

This could be either a bug, or an error in the documentation, or just my fault (hopefully the third reason).

Pandas 0.15.2 documentation explains how to create one plot with two axes (http://pandas.pydata.org/pandas-docs/dev/visualization.html?highlight=visualization#plotting-on-a-secondary-y-axis). For example:

import numpy as np,pandas as pd,matplotlib.pyplot as plt
x=pd.DataFrame(range(100))**.5
y=x+np.random.normal(size=(x.shape[0],1))

# Does not work ()
x.plot()
y.plot(secondary_y=True,style='g:')
plt.show()

The problem is, two plots are generated, rather than only one with two axes.

This alternative would work:

# Works (1 figure, 2 axes)
fig,ax=plt.subplots()
ax2=ax.twinx()
x.plot(ax=ax)
y.plot(ax=ax2,style='g:')
plt.show()

Question: Does the example in the documentation actually generate a plot with 2 axes? What I get is two separate plots. This seems to be a bug. If this is not a bug, then I would suggest the example in the documentation is modified to explain the second alternative.

@TomAugspurger
Copy link
Contributor

Hmm, those examples are plotting Series.

Your code works if you do y[0].plot(secondary_y=True). I'd need to think about whether this should work for DataFrames (my initial reaction is that it should).

@TomAugspurger
Copy link
Contributor

Related issue: #8776

@TomAugspurger TomAugspurger added the Visualization plotting label Jan 19, 2015
@TomAugspurger TomAugspurger added this to the 0.16.0 milestone Jan 19, 2015
@JohnNapier
Copy link
Author

Thank you, @TomAugspurger. I also think it should work for dataframes. The documentation makes no distinction, and why to limit the plot functionality anyway...

@TomAugspurger
Copy link
Contributor

I guess the real solution is to catch the axes that's returned in the first plot and pass that into the second.

In [33]: ax = x.plot()

In [34]: y.plot(secondary_y=True,style='g:', ax=ax)
Out[34]: <matplotlib.axes._subplots.AxesSubplot at 0x10086630>

This will work for multiple columns.

If you don't mind, I'm going to close this issue, since it's going to be "fixed" by however we handle #8776. If you haven't looked at that issue, it's about how Series.plot() plots on the currently active axis, while DataFrame.plot() plots on a new one.

@JohnNapier
Copy link
Author

@TomAugspurger, indeed, that's a nice workaround. I think the documentation could make that clear. The example presented only works for series, and the documentation does not specifically exclude dataframes.

Thanks for the hint!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants