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.

sapo_cosmico
  • 6,274
  • 12
  • 45
  • 58
  • 1
    have you tried the secondary axis from http://stackoverflow.com/questions/24183101/pandas-bar-plot-with-two-bars-and-two-y-axis?rq=1? Note that `data.plot` has an `ax` argument to specify what axis to plot onto. – cphlewis Apr 16 '15 at 21:49

2 Answers2

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

cphlewis
  • 15,759
  • 4
  • 46
  • 55
  • 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? – sapo_cosmico Apr 17 '15 at 00: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 '15 at 01: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 '15 at 01: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 '20 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

N Kattler
  • 123
  • 2
  • 6