python pandas DataFrame subplot in columns and rows

11,943

Solution 1

In current versions of Pandas, DataFrame.plot features the layout keyword for this purpose.

df.plot(subplots=True, layout=(2,2), ...)

Solution 2

cplcloud's answer works, but following code will give you a bit more structure so that you can start configuring more if you do not need the loop.

fig, axes = plt.subplots(nrows=2, ncols=2)
fig.set_figheight(6)
fig.set_figwidth(8)
df[0].plot(ax=axes[0,0], style='r', label='Series'); axes[0,0].set_title(0)
df[1].plot(ax=axes[0,1]); axes[0,1].set_title(1)
df[2].plot(ax=axes[1,0]); axes[1,0].set_title(2)
df[3].plot(ax=axes[1,1]); axes[1,1].set_title(3)
fig.tight_layout()

Added some example on axis 0 to show how you can further configure it.

Share:
11,943

Related videos on Youtube

tesla1060
Author by

tesla1060

Updated on September 15, 2022

Comments

  • tesla1060
    tesla1060 over 1 year

    I would like to produce a subplot from data 4 column DataFrame into 2 rows and 2 columns

    df =pd.DataFrame(np.random.randn(6,4),index=pd.date_range('1/1/2000',periods=6, freq='1h'))
    

    However below will give a 4 row and 1 column plot

     df.plot(use_index=False, title=f, subplots=True, sharey=True, figsize=(8, 6))
    

    Thanks.

    • Thorsten Kranz
      Thorsten Kranz about 11 years
      You should do it by hand, import matplotlib.pyplot as plt, then something like for i in df: plt.subplot(2,2,i+1);plt.plot(df[i]);
    • Phillip Cloud
      Phillip Cloud almost 11 years
      @tesla1060 i suppose pandas could allow some sort of figshape argument...
  • mbarete
    mbarete over 4 years
    This is the quickest implementation of OP's request