Plotting DataFrame vs. Series

15 views
Skip to first unread message

Paul Blelloch

unread,
May 22, 2013, 11:48:44 AM5/22/13
to pyd...@googlegroups.com
I've noticed a subtle difference between plotting a number of DataFrame objects, vs. plotting a number of Series objects.  The DataFrame objects each seem to generate their own plots, while the Series are combined into a single plot.  For example, consider the following code:

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

x1=np.random.normal(0,1,100)

x2=np.random.normal(0,2,100)

d1=pd.Series(x1)

d2=pd.Series(x2)

d1.plot()

d2.plot()

plt.show()

d1=pd.DataFrame(x1)

d2=pd.DataFrame(x2)

d1.plot()

d2.plot()

plt.show()


In the first case (plotting Series objects) the plots are combined, while in the 2nd (same data, but in DataFrame objects) I get two separate plots.  Is this intentional?  Can I get a bunch of DataFrame plots to combine?


THANKS, Paul


Skipper Seabold

unread,
May 22, 2013, 12:22:51 PM5/22/13
to pyd...@googlegroups.com
On Wed, May 22, 2013 at 11:48 AM, Paul Blelloch <paul.b...@gmail.com> wrote:
I've noticed a subtle difference between plotting a number of DataFrame objects, vs. plotting a number of Series objects.  The DataFrame objects each seem to generate their own plots, while the Series are combined into a single plot.  For example, consider the following code:

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

x1=np.random.normal(0,1,100)

x2=np.random.normal(0,2,100)

d1=pd.Series(x1)

d2=pd.Series(x2)

d1.plot()

d2.plot()

plt.show()

d1=pd.DataFrame(x1)

d2=pd.DataFrame(x2)

d1.plot()

d2.plot()


Try,

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fig = d1.plot(ax=ax)
fig = d2.plot(ax=fig.axes)
plt.show()

Something like this should work. I can never remember if DataFrames return figures or axes.

Skipper
Reply all
Reply to author
Forward
0 new messages