4

I'm trying to plot two series together in Pandas, from different dataframes.

Both their axis are datetime objects, so they can be plotted together:

amazon_prices.Close.plot()
data[amazon].BULL_MINUS_BEAR.resample("W").plot()
plt.plot()

Yields:

enter image description here

All fine, but I need the green graph to have its own scale. So I use the

amazon_prices.Close.plot()
data[amazon].BULL_MINUS_BEAR.resample("W").plot(secondary_y=True)
plt.plot()

This secondary_y creates a problem, as instead of having the desired graph, I have the following:

enter image description here

Any help with this is hugely appreciated.

(Less relevant notes: I'm (evidently) using Pandas, Matplotlib, and all this is in an Ipython notebook)

EDIT: I've since noticed that removing the resample("W") solves the issue. It is still a problem however as the non-resampled data is too noisy to be visible. Being able to plot sampled data with a secondary axis would be hugely helpful.

1

2 Answers 2

11
import matplotlib.pyplot as plt
import pandas as pd
from numpy.random import random

df = pd.DataFrame(random((15,2)),columns=['a','b'])
df.a = df.a*100

fig, ax1 = plt.subplots(1,1)
df.a.plot(ax=ax1, color='blue', label='a')
ax2 = ax1.twinx()
df.b.plot(ax=ax2, color='green', label='b')
ax1.set_ylabel('a')
ax2.set_ylabel('b')
ax1.legend(loc=3)
ax2.legend(loc=0)
plt.show()

enter image description here

4
  • 1
    Thanks for your answer, while this would undoubtedly solve the problem. I'm also interested in understanding why my solution didn't work. Any thoughts on that? Apr 17, 2015 at 0:50
  • Can't tell without seeing your data! How about posting head of the various dataframes (incl. the result of resample('W'), etc.). Also, this version works with your real resampled data? (just checking)
    – cphlewis
    Apr 17, 2015 at 1:07
  • 1
    ...also, in general, the unspecified interactive style of calling plot and hoping all the currently-active-defaults are lined up right seems brittle to me. I just do the explicit fig and ax from the start, now.
    – cphlewis
    Apr 17, 2015 at 1:15
  • Note that secondary_y=True is poorly documented. So it's hard to figure out what exactly it does. It might also change in the future. I'd just stick with the good old twinx() as was done in this answer.
    – Joooeey
    Apr 11, 2020 at 15:09
5

I had the same issue, always getting a strange plot when I wanted a secondary_y.

I don't know why no-one mentioned this method in this post, but here's how I got it to work, using the same example as cphlewis:

import matplotlib.pyplot as plt
import pandas as pd
from numpy.random import random

df = pd.DataFrame(random((15,2)),columns=['a','b'])
ax = df.plot(secondary_y=['b'])
plt.show()

Here's what it'll look like

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.