pandas plotting different frequency data with same x axis

2,459 views
Skip to first unread message

Fabrizio Pollastri

unread,
Sep 4, 2012, 11:44:22 AM9/4/12
to pystat...@googlegroups.com
Hello,
I wish to plot data from two data frame, where the second one is a stacked version of the first one, in two subplots having the same x axis with the tick frequency of the first data frame. For example

df1
              1 open     2 close
1/1/2012  1.01        1.02
1/2/2012  1.03        1.04

df2 (stacked version)

1/1/2012  1 open   1.01
               2 close  1.02
1/2/2012  1 open   1.03
               2 close  1.04

df1 has two curves with two samples, df2 has one curve with four samples, both have the same x axis limits.

It is possible to plot df2 with the same x axis of df1, with the same ticks?

Thanks in advance,
Fabrizio


Paul Hobson

unread,
Sep 4, 2012, 4:36:24 PM9/4/12
to pystat...@googlegroups.com
Fabrizio,

I think this does what you need:

import numpy as np
import pandas
import matplotlib.pyplot as plt

# fake data
x = np.random.normal(size=(20,))
y = np.random.normal(size=(7,))

# data frames
xdf = pandas.DataFrame(x)
ydf = pandas.DataFrame(y)

# plot x data, get an MPL axes object
ax = xdf.plot()

# plot y data, using the axes already created
ydf.plot(ax=ax)



plt.show()

Area234

unread,
Sep 4, 2012, 6:02:18 PM9/4/12
to pystat...@googlegroups.com
On 09/04/2012 10:36 PM, Paul Hobson wrote:
> import numpy as np
> import pandas
> import matplotlib.pyplot as plt
>
> # fake data
> x = np.random.normal(size=(20,))
> y = np.random.normal(size=(7,))
>
> # data frames
> xdf = pandas.DataFrame(x)
> ydf = pandas.DataFrame(y)
>
> # plot x data, get an MPL axes object
> ax = xdf.plot()
>
> # plot y data, using the axes already created
> ydf.plot(ax=ax)
>
>
>
> plt.show()

Paul,

your suggestion plots the two data series into the same plot frame, but the two series have different x axis limits:
the shortest ends at 7, the longest ends at 20.

My case is different, the two series have the same x axis limits (both have the same start time and the same end time)
but different sampling frequency: in the general case, the second series has N (integer) samples for each samples of
the first one. So each packet of N samples of the second series has the same time of the corresponding sample in the
first series.

I know that using df.reset_index(), df2 can be changed from multi index with time and col name, to a single level
numeric index having a double sampling frequency with respect to df1.

At this point, I have two series,
df1 with M samples starting from time T1 and ending at time T2, with a date index,
df2 with 2xM samples starting from time T1 and ending at time T2, with a numeric index.

I wish to plot df1 in one subplot with x axis ranging from T1 to T2 and to plot df2 into another subplot with the
same time axis of df1.

Wouter Overmeire

unread,
Sep 5, 2012, 3:48:36 AM9/5/12
to pystat...@googlegroups.com


2012/9/5 Area234 <are...@gmail.com>



screenshot: http://i.imgur.com/CV38p.png

In [133]: df1.head()
Out[133]:
                open     close
2010-10-11  1.137040 -0.841830
2010-10-12 -2.072695 -0.534179
2010-10-13  1.082141 -0.153102
2010-10-14  0.739454  0.536811
2010-10-15 -0.873062  3.102334

In [134]: df2.head()
Out[134]:
2010-10-11  open     1.137040
            close   -0.841830
2010-10-12  open    -2.072695
            close   -0.534179
2010-10-13  open     1.082141

In [135]: fig, axes = subplots(2,1)

In [136]: df1.plot(ax=axes[0])
Out[136]: <matplotlib.axes.AxesSubplot at 0xaa8420c>

In [137]: df2.reset_index()[::2].set_index('level_0')[0].plot(ax=axes[1], style='or')
Out[137]: <matplotlib.axes.AxesSubplot at 0xab615ec>

In [138]: hold(True)

In [139]: df2.reset_index()[1::2].set_index('level_0')[0].plot(ax=axes[1], style='og')
Out[139]: <matplotlib.axes.AxesSubplot at 0xab615ec>


Reply all
Reply to author
Forward
0 new messages