FooBar
FooBar

Reputation: 16488

Plot series different frequencies

I have some time series, for which I created both quarterly and annual sums. I wanted to plot the two together, but the plot only showed the more frequent one. merged.plot() gives:

both frequencies

Here's some sample data.

>>> merged.head()
           shopping_weightedQ             shopping_weightedA            
status                    emp       unemp                emp       unemp
date                                                                    
2003-01-01         653.964346  696.178441         695.374248  755.180039
2003-04-01         702.233863  647.461856                NaN         NaN
2003-07-01         665.619252  774.226719                NaN         NaN
2003-10-01         757.189689  932.586052                NaN         NaN
2004-01-01         670.114570  751.718014         703.479640  839.858502

Upvotes: 2

Views: 995

Answers (1)

cge
cge

Reputation: 9890

Your problem are the NaNs. Pandas passes data to matplotlib for plotting. When matplotlib sees a NaN, it stops the line it was making, and doesn't start again until it sees another plottable value. Since you're plotting lines, you don't see anything for the annual data, because there are never two values that can be connected by a line. If you were to plot with points, with merged.plot(style='o'), for example, you'd see all the data.

But you'd probably like lines. I can think of two ways to do this.

The first drops down into matplotlib

import matplotlib.pylab as plt
plt.figure()
plt.plot( merged.shopping_weightedQ.index, merged.shopping_weightedQ )
plt.plot( merged.shopping_weightedA.dropna().index, merged.shopping_weightedA.dropna() )

The second uses pandas plotting, with a shared matplotlib axis:

import matplotlib.pylab as plt
plt.figure()
ax = plt.gca()
merged.shopping_weightedA.dropna().plot(ax=ax)
merged.shopping_weightedQ.plot(ax=ax)

Note that for reasons I don't fully understand at the moment, probably relating to the indexes and the x-axis, the pandas method doesn't work if the two plot commands are reversed, and you plot A after Q, unless you also add the x_compat=True argument to each of the plot commands.

In each of these cases, you'll probably have to mess around with the legend names.

Upvotes: 3

Related Questions