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

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    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 one[​IMG]

    I want one single figure with 'Data1' of all dataframes in the first subplot and so on. [​IMG]

    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.

    Continue reading...

Compartilhe esta Página