pythondataframeplot

Plot multiple dataframes's columns in same subplots using df.plot


I have some 60 dataframes all having the same 6 columns and would like to plot these in the same figure with one column per dataframe. I tried using df.plot put then it just created a new plot for each dataframe.

df1 = pd.DataFrame(np.random.rand(50,6)*100, columns = ['Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6'])
df2 = pd.DataFrame(np.random.rand(50,6)*100, columns = ['Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6'])
df3 = pd.DataFrame(np.random.rand(50,6)*100, columns = ['Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6'])
df = [df1,df2,df3]

for frame in df:
    frame.plot(subplots = True, layout = (2,3))

Which yields three different figures similar to this oneenter image description here

I want one single figure with 'Data1' of all dataframes in the first subplot and so on. enter image description here

To create this I did

fig, ax = plt.subplots(2,3)
for frame in df:
    ax[0,0].plot(frame['Data1'])
    ax[0,1].plot(frame['Data2']) 
# and so on 

Is there a nicer way to do this other than looping over ax?

I do not want each dataframe in a separate subplot so How to plot multiple dataframes in subplots does not help me.


Solution

  • This should work (after fixing the dimensino of the dummy data)

    df1 = pd.DataFrame(np.random.rand(50,6)*100, columns = ['Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6'])
    df2 = pd.DataFrame(np.random.rand(50,6)*100, columns = ['Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6'])
    df3 = pd.DataFrame(np.random.rand(50,6)*100, columns = ['Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6'])
    df = [df1,df2,df3]
    
    fig, axs = plt.subplots(2,3)
    for frame in df:
        for col, ax in zip(frame.columns, axs.ravel()):
            ax.plot(frame[col])