yoshiserry
yoshiserry

Reputation: 21345

subplot with pandas dataframes

I would like to create multiple subplot on a figure using a pandas dataframe (called df).

My original plot is here:

df.plot(x='month', y='number', title='open by month',color="blue")

I've tried multiple attempts at the "working with figures and subplots" section of this site pyplot tutorial from matplotlib

[ 1 ]

plt.figure(1)
df.plot.(figure(1), sublot(211), x='month', y='number', title='open byXXX"
df.plot.(figure(1), sublot(212), x='month', y='number', title='open byXXX"

[ 2 ]

plt.figure(1)
df.plot.subplot(211)(x='month', y='number', title='open byXXX")
df.plot.subplot(212)(x='month', y='number', title='open byXXX")

Upvotes: 5

Views: 12983

Answers (1)

Paul H
Paul H

Reputation: 68146

You plot against Axes, not Figures. Pandas really has nothing to do with plotting/matplotlib. Pandas devs simply added a quick interface to matplotlib for convenience.

You really should learn to use matplotlib without going through pandas first. But for your problem at hand, you simply need to pass the Axes objects to the dataframe's plot method.

fig, axes = plt.subplots(nrows=2, ncols=1)
df1.plot(..., ax=axes[0, 0])
df2.plot(..., ax=axes[1, 0])

Upvotes: 8

Related Questions