Create subplot of multiple features (columns), by overlapping dataframes with a baseline dataframe, for every group/id in Python

huangapple go评论70阅读模式
英文:

Create subplot of multiple features (columns), by overlapping dataframes with a baseline dataframe, for every group/id in Python

问题

I have translated the code as requested. Here are the translated code sections for df_1, df_2, and df_3 overlapping with df_base:

For df_1 overlapped with df_base:

plt_fig_verify = plt.figure(figsize=(10,8))

##########################################################################

## Plots for id1, overlap for df_1 and df_base

## Salary plot
plt.subplot(3,3,1)
plt.plot(df_base.groupby(by="id").get_group(1)['cycle_base'], df_base.groupby(by="id").get_group(1)['Salary_base'], 'b',  linewidth = '1', label ='Salary: df_base')
plt.plot(df_1.groupby(by="id").get_group(1)['cycle'], df_1.groupby(by="id").get_group(1)['Salary_1'], 'r',  linewidth = '1', label ='Salary: df_1')
plt.xlabel('cycle')
plt.ylabel('wrt to id1')
plt.legend()

## Children plot
plt.subplot(3,3,2)
plt.plot(df_base.groupby(by="id").get_group(1)['cycle_base'], df_base.groupby(by="id").get_group(1)['Children_base'], 'b',  linewidth = '1', label ='Children: df_base')
plt.plot(df_1.groupby(by="id").get_group(1)['cycle'], df_1.groupby(by="id").get_group(1)['Children_1'], 'r',  linewidth = '1', label ='Children: df_1')
plt.xlabel('cycle')
plt.legend()

## Expenditure plot
plt.subplot(3,3,3)
plt.plot(df_base.groupby(by="id").get_group(1)['cycle_base'], df_base.groupby(by="id").get_group(1)['Expenditure_base'], 'b',  linewidth = '1', label ='Expenditure: df_base')
plt.plot(df_1.groupby(by="id").get_group(1)['cycle'], df_1.groupby(by="id").get_group(1)['Expenditure_1'], 'r',  linewidth = '1', label ='Expenditure: df_1')
plt.xlabel('cycle')
plt.legend()

##########################################################################

## Plots for id2, overlap for df_1 and df_base

## Salary plot
plt.subplot(3,3,4)
plt.plot(df_base.groupby(by="id").get_group(2)['cycle_base'], df_base.groupby(by="id").get_group(2)['Salary_base'], 'b',  linewidth = '1', label ='Salary: df_base')
plt.plot(df_1.groupby(by="id").get_group(2)['cycle'], df_1.groupby(by="id").get_group(2)['Salary_1'], 'r',  linewidth = '1', label ='Salary: df_1')
plt.xlabel('cycle')
plt.ylabel('wrt to id2')
plt.legend()

## Children plot
plt.subplot(3,3,5)
plt.plot(df_base.groupby(by="id").get_group(2)['cycle_base'], df_base.groupby(by="id").get_group(2)['Children_base'], 'b',  linewidth = '1', label ='Children: df_base')
plt.plot(df_1.groupby(by="id").get_group(2)['cycle'], df_1.groupby(by="id").get_group(2)['Children_1'], 'r',  linewidth = '1', label ='Children: df_1')
plt.xlabel('cycle')
plt.legend()

## Expenditure plot
plt.subplot(3,3,6)
plt.plot(df_base.groupby(by="id").get_group(2)['cycle_base'], df_base.groupby(by="id").get_group(2)['Expenditure_base'], 'b',  linewidth = '1', label ='Expenditure: df_base')
plt.plot(df_1.groupby(by="id").get_group(2)['cycle'], df_1.groupby(by="id").get_group(2)['Expenditure_1'], 'r',  linewidth = '1', label ='Expenditure: df_1')
plt.xlabel('cycle')
plt.legend()

##########################################################################

## Plots for id3, overlap for df_1 and df_base

## Salary plot
plt.subplot(3,3,7)
plt.plot(df_base.groupby(by="id").get_group(3)['cycle_base'], df_base.groupby(by="id").get_group(3)['Salary_base'], 'b',  linewidth = '1', label ='Salary: df_base')
plt.plot(df_1.groupby(by="id").get_group(3)['cycle'], df_1.groupby(by="id").get_group(3)['Salary_1'], 'r',  linewidth = '1', label ='Salary: df_1')
plt.xlabel('cycle')
plt.ylabel('wrt to id3')
plt.legend()

## Children plot
plt.subplot(3,3,8)
plt.plot(df_base.groupby(by="id").get_group(3)['cycle_base'], df_base.groupby(by="id").get_group(3)['Children_base'], 'b',  linewidth = '1', label ='Children: df_base')
plt.plot(df_

<details>
<summary>英文:</summary>

I have a baseline dataframe (df_base) and 3 other dataframes (df_1/df_2/df_3) as such:

    #Load the required libraries
    import pandas as pd
    import matplotlib.pyplot as plt
    
    ########################################################################################
    
    #Create dataframe_Baseline
    data_set_base = {&#39;id&#39;: [1, 1, 1,1, 1, 1, 1, 1, 1,1,1,
                   2, 2, 2, 2,2,2,
                   3, 3, 3, 3, 3, 3, 3,3,],
            &#39;cycle_base&#39;: [0.0, 0.2,0.4, 0.6, 0.8, 1,1.2,1.4,1.6,1.8,2.0,
                      0.0, 0.2,0.4, 0.6,0.8,1,
                      0.0, 0.2,0.4, 0.6, 0.8,1.0,1.2,1.4,],
            &#39;Salary_base&#39;: [1, 4, 3, 8,2,9,10,5,1,6,1,
                       1, 0, 1, 5,3,8,
                       12, 3,29,10,11,1,0,3,],
            &#39;Children_base&#39;: [2, 0, 1, 0, 2, 0, 0,1, 0,0,1,
                         2, 1, 0, 0,1,1, 
                         0, 1,2, 1, 1,0, 0,1,],
            &#39;Expenditure_base&#39;: [20, 10, 128, 76, 30, 40, 85, 60, 27,45,84,
                     25, 100, 120,82,100,110,
                     28,  15,90, 80, 66, 120, 35, 67,],
            }
    
    #Convert to dataframe_Baseline
    df_base = pd.DataFrame(data_set_base)
    print(&quot;\n df_base = \n&quot;,df_base)
    
    ########################################################################################
    
    
    #Create dataframe_1
    data_set_1 = {&#39;id&#39;: [1, 1, 1,1, 1, 1, 1, 1, 1,
                   2, 2, 2, 2,
                   3, 3, 3, 3, 3, 3, 3,3,],
            &#39;cycle&#39;: [0.0, 0.2,0.4, 0.6, 0.8, 1,1.2,1.4,1.6,
                      0.0, 0.2,0.4, 0.6,
                      0.0, 0.2,0.4, 0.6, 0.8,1.0,1.2,1.4,],
            &#39;Salary_1&#39;: [6, 7, 7, 7,8,9,10,11,12,
                       30, 10, 20, 4,
                       2, 1,19,0,11,2,28,5,],
            &#39;Children_1&#39;: [1, 2, 1, 0, 1, 1, 0,2, 1,
                         0, 1, 1, 2,  
                         1, 2,0, 1, 2,0, 1,0,],
            &#39;Expenditure_1&#39;: [141, 123, 128, 66, 66, 120, 141, 52, 52,
                     141, 96, 120,120, 
                     141,  15,123, 128, 66, 120, 141, 141,],
            }
    
    #Convert to dataframe_1
    df_1 = pd.DataFrame(data_set_1)
    print(&quot;\n df_1 = \n&quot;,df_1)
    
    
    ########################################################################################
    
    
    #Create dataframe_2
    data_set_2 = {&#39;id&#39;: [1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1,
                   2, 2, 2, 2, 2, 2, 2,
                   3, 3, 3, 3, 3, 3, 3,3,],
            &#39;cycle&#39;: [0.0, 0.2,0.4, 0.6, 0.8, 1,1.2,1.4,1.6,1.8,2.0,2.2,
                      0.0, 0.2,0.4, 0.6,0.8,1.0,1.2,
                      0.0, 0.2,0.4, 0.6, 0.8,1.0,1.2,1.4,],
            &#39;Salary_2&#39;: [7, 8, 8, 8,8,9,14,21,12,19,14,20,
                       1, 6, 3, 8,4,9,8,
                       6, 4,9,10,4,12,13,6,],
            &#39;Children_2&#39;: [1, 0, 1, 1, 0, 1, 2,2, 0, 1, 0, 2,
                         1, 0, 1, 2, 1, 1, 1, 
                         1, 2,1, 0, 2,0, 1,0,],
            &#39;Expenditure_2&#39;: [79, 82, 128, 66, 42, 120, 141, 52,96, 120, 141, 52,
                     30, 96, 86,120, 91, 52,96,
                     28,  15,51, 128, 76, 120, 87, 141,],
            }
    
    #Convert to dataframe_2
    df_2 = pd.DataFrame(data_set_2)
    print(&quot;\n df_2 = \n&quot;,df_2)
    
    
    ########################################################################################
    
    
    #Create dataframe_3
    data_set_3 = {&#39;id&#39;: [1, 1, 1,1, 1, 1, 
                   2, 2, 2, 
                   3, 3, 3, 3, 3, 3, 3,],
            &#39;cycle&#39;: [0.0, 0.2,0.4, 0.6, 0.8, 1,
                      0.0, 0.2,0.4, 
                      0.0, 0.2,0.4, 0.6, 0.8,1.0,1.2,],
            &#39;Salary_3&#39;: [2, 5, 2, 7,2,1,
                       3, 7, 4, 
                       2, 8,6,10,11,4,13,],
            &#39;Children_3&#39;: [1, 0, 1, 0, 1, 0, 
                         0, 1, 0, 
                         1, 0,0, 1, 1,0,2,],
            &#39;Expenditure_3&#39;: [100, 50, 10, 66, 66, 80, 
                     10, 5, 80,
                     70,  15,40, 85, 66, 93, 60, ],
            }
    
    #Convert to dataframe_1
    df_3 = pd.DataFrame(data_set_3)
    print(&quot;\n df_3 = \n&quot;,df_3)






The features are: &#39;Salary&#39;, &#39;Children&#39;, and &#39;Expenditure&#39;.

Every dataframe (df_base/df_1/df_2/df_3) has ids from 1 to 4.

Now for dataframes (df_1/df_2/df_3), for every id, I need to plot the features(Salary/Children/Expenditure) vs cycle, such that they overlap with df_base, in one single plot.

For df_1 overlapping with df_base these are the codes:

    plt_fig_verify = plt.figure(figsize=(10,8))
    
    ##########################################################################
    
    ## Plots for id1, overlap for df_1 and df_base
    
    ## Salary plot
    plt.subplot(3,3,1)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;Salary_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_base&#39;)
    plt.plot(df_1.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle&#39;], df_1.groupby(by=&quot;id&quot;).get_group(1)[&#39;Salary_1&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_1&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.ylabel(&#39;wrt to id1&#39;)
    plt.legend()
    
    ## Children plot
    plt.subplot(3,3,2)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;Children_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_base&#39;)
    plt.plot(df_1.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle&#39;], df_1.groupby(by=&quot;id&quot;).get_group(1)[&#39;Children_1&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_1&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    ## Expenditure plot
    plt.subplot(3,3,3)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;Expenditure_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_base&#39;)
    plt.plot(df_1.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle&#39;], df_1.groupby(by=&quot;id&quot;).get_group(1)[&#39;Expenditure_1&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_1&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    ##########################################################################
    
    ## Plots for id2, overlap for df_1 and df_base
    
    ## Salary plot
    plt.subplot(3,3,4)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;Salary_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_base&#39;)
    plt.plot(df_1.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle&#39;], df_1.groupby(by=&quot;id&quot;).get_group(2)[&#39;Salary_1&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_1&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.ylabel(&#39;wrt to id2&#39;)
    plt.legend()
    
    ## Children plot
    plt.subplot(3,3,5)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;Children_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_base&#39;)
    plt.plot(df_1.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle&#39;], df_1.groupby(by=&quot;id&quot;).get_group(2)[&#39;Children_1&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_1&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    ## Expenditure plot
    plt.subplot(3,3,6)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;Expenditure_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_base&#39;)
    plt.plot(df_1.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle&#39;], df_1.groupby(by=&quot;id&quot;).get_group(2)[&#39;Expenditure_1&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_1&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    ##########################################################################
    
    ## Plots for id3, overlap for df_1 and df_base
    
    ## Salary plot
    plt.subplot(3,3,7)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;Salary_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_base&#39;)
    plt.plot(df_1.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle&#39;], df_1.groupby(by=&quot;id&quot;).get_group(3)[&#39;Salary_1&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_1&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.ylabel(&#39;wrt to id3&#39;)
    plt.legend()
    
    ## Children plot
    plt.subplot(3,3,8)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;Children_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_base&#39;)
    plt.plot(df_1.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle&#39;], df_1.groupby(by=&quot;id&quot;).get_group(3)[&#39;Children_1&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_1&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    ## Expenditure plot
    plt.subplot(3,3,9)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;Expenditure_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_base&#39;)
    plt.plot(df_1.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle&#39;], df_1.groupby(by=&quot;id&quot;).get_group(3)[&#39;Expenditure_1&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_1&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    plt.show()


The plot looks as such:

[![enter image description here][1]][1]

Likewise for df_2 overlapped with df_base: 

    plt_fig_verify = plt.figure(figsize=(10,8))
    
    ##########################################################################
    
    ## Plots for id1, overlap for df_2 and df_base
    
    ## Salary plot
    plt.subplot(3,3,1)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;Salary_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_base&#39;)
    plt.plot(df_2.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle&#39;], df_2.groupby(by=&quot;id&quot;).get_group(1)[&#39;Salary_2&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_2&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.ylabel(&#39;wrt to id1&#39;)
    plt.legend()
    
    ## Children plot
    plt.subplot(3,3,2)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;Children_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_base&#39;)
    plt.plot(df_2.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle&#39;], df_2.groupby(by=&quot;id&quot;).get_group(1)[&#39;Children_2&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_2&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    ## Expenditure plot
    plt.subplot(3,3,3)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;Expenditure_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_base&#39;)
    plt.plot(df_2.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle&#39;], df_2.groupby(by=&quot;id&quot;).get_group(1)[&#39;Expenditure_2&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_2&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    ##########################################################################
    
    ## Plots for id2, overlap for df_2 and df_base
    
    ## Salary plot
    plt.subplot(3,3,4)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;Salary_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_base&#39;)
    plt.plot(df_2.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle&#39;], df_2.groupby(by=&quot;id&quot;).get_group(2)[&#39;Salary_2&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_2&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.ylabel(&#39;wrt to id2&#39;)
    plt.legend()
    
    ## Children plot
    plt.subplot(3,3,5)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;Children_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_base&#39;)
    plt.plot(df_2.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle&#39;], df_2.groupby(by=&quot;id&quot;).get_group(2)[&#39;Children_2&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_2&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    ## Expenditure plot
    plt.subplot(3,3,6)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;Expenditure_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_base&#39;)
    plt.plot(df_2.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle&#39;], df_2.groupby(by=&quot;id&quot;).get_group(2)[&#39;Expenditure_2&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_2&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    ##########################################################################
    
    ## Plots for id3, overlap for df_2 and df_base
    
    ## Salary plot
    plt.subplot(3,3,7)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;Salary_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_base&#39;)
    plt.plot(df_2.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle&#39;], df_2.groupby(by=&quot;id&quot;).get_group(3)[&#39;Salary_2&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_2&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.ylabel(&#39;wrt to id3&#39;)
    plt.legend()
    
    ## Children plot
    plt.subplot(3,3,8)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;Children_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_base&#39;)
    plt.plot(df_2.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle&#39;], df_2.groupby(by=&quot;id&quot;).get_group(3)[&#39;Children_2&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_2&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    ## Expenditure plot
    plt.subplot(3,3,9)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;Expenditure_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_base&#39;)
    plt.plot(df_2.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle&#39;], df_2.groupby(by=&quot;id&quot;).get_group(3)[&#39;Expenditure_2&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_2&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    plt.show()



[![enter image description here][2]][2]

And df_3 overlapped with df_base:

    plt_fig_verify = plt.figure(figsize=(10,8))
    
    ##########################################################################
    
    ## Plots for id1, overlap for df_3 and df_base
    
    ## Salary plot
    plt.subplot(3,3,1)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;Salary_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_base&#39;)
    plt.plot(df_3.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle&#39;], df_3.groupby(by=&quot;id&quot;).get_group(1)[&#39;Salary_3&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_3&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.ylabel(&#39;wrt to id1&#39;)
    plt.legend()
    
    ## Children plot
    plt.subplot(3,3,2)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;Children_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_base&#39;)
    plt.plot(df_3.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle&#39;], df_3.groupby(by=&quot;id&quot;).get_group(1)[&#39;Children_3&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_3&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    ## Expenditure plot
    plt.subplot(3,3,3)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(1)[&#39;Expenditure_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_base&#39;)
    plt.plot(df_3.groupby(by=&quot;id&quot;).get_group(1)[&#39;cycle&#39;], df_3.groupby(by=&quot;id&quot;).get_group(1)[&#39;Expenditure_3&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_3&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    ##########################################################################
    
    ## Plots for id2, overlap for df_3 and df_base
    
    ## Salary plot
    plt.subplot(3,3,4)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;Salary_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_base&#39;)
    plt.plot(df_3.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle&#39;], df_3.groupby(by=&quot;id&quot;).get_group(2)[&#39;Salary_3&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_3&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.ylabel(&#39;wrt to id2&#39;)
    plt.legend()
    
    ## Children plot
    plt.subplot(3,3,5)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;Children_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_base&#39;)
    plt.plot(df_3.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle&#39;], df_3.groupby(by=&quot;id&quot;).get_group(2)[&#39;Children_3&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_3&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    ## Expenditure plot
    plt.subplot(3,3,6)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(2)[&#39;Expenditure_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_base&#39;)
    plt.plot(df_3.groupby(by=&quot;id&quot;).get_group(2)[&#39;cycle&#39;], df_3.groupby(by=&quot;id&quot;).get_group(2)[&#39;Expenditure_3&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_3&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    ##########################################################################
    
    ## Plots for id3, overlap for df_3 and df_base
    
    ## Salary plot
    plt.subplot(3,3,7)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;Salary_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_base&#39;)
    plt.plot(df_3.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle&#39;], df_3.groupby(by=&quot;id&quot;).get_group(3)[&#39;Salary_3&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Salary: df_3&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.ylabel(&#39;wrt to id3&#39;)
    plt.legend()
    
    ## Children plot
    plt.subplot(3,3,8)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;Children_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_base&#39;)
    plt.plot(df_3.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle&#39;], df_3.groupby(by=&quot;id&quot;).get_group(3)[&#39;Children_3&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Children: df_3&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    ## Expenditure plot
    plt.subplot(3,3,9)
    plt.plot(df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(3)[&#39;Expenditure_base&#39;], &#39;b&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_base&#39;)
    plt.plot(df_3.groupby(by=&quot;id&quot;).get_group(3)[&#39;cycle&#39;], df_3.groupby(by=&quot;id&quot;).get_group(3)[&#39;Expenditure_3&#39;], &#39;r&#39;,  linewidth = &#39;1&#39;, label =&#39;Expenditure: df_3&#39;)
    plt.xlabel(&#39;cycle&#39;)
    plt.legend()
    
    plt.show()

[![enter image description here][3]][3]


Here I need to write the codes for the subplot function NINE times for df_1 vs df_base, NINE times for df_2 vs df_base, and NINE times for df_3 vs df_base. 

SO a **total of 27 subplot function needs to be written.**


However, is there any way out, by which we can have some iterative function and write the subplot function only once and get all the subplots in Python?

Can somebody please help me out with this ?



















































  [1]: https://i.stack.imgur.com/XF2Yy.png
  [2]: https://i.stack.imgur.com/dyH0k.png
  [3]: https://i.stack.imgur.com/THllv.png

</details>


# 答案1
**得分**: 1

这是绘制`subplots`的我的版本需要预先定义两个变量

```python
df_list = [df_1, df_2, df_3]
key_list = ['Salary_base', 'Children_base', 'Expenditure_base']

def subplot_func(df_base, df, key_list, axs, id_):
    axs = axs.ravel()
    for i in range(len(axs)):
        key = key_list[i % 3]
        group_id  = int(i/3+1)
        
        axs[i].plot(df_base.groupby(by="id").get_group(group_id)['cycle_base'], df_base.groupby(by="id").get_group(group_id)[key], 'b', linewidth='1', label=key.replace('_base', '') + ': df_base')
        axs[i].plot(df.groupby(by="id").get_group(group_id)['cycle'], df.groupby(by="id").get_group(group_id)[key.replace('base', str(id_+1))], 'r', linewidth='1', label=key.replace('_base', '') + ': df_' + str(id_+1))
        axs[i].set_xlabel('cycle')
        if i % 3 == 0: 
            axs[i].set_ylabel('wrt to id' + str(group_id))
        axs[i].legend()

for id_ in range(3):
    fig, axs = plt.subplots(3,3, figsize=(10,8))
    subplot_func(df_base, df_list[id_], key_list, axs, id_)
英文:

Here is my version for drawing the subplots, need to pre-define two variables :

df_list = [df_1, df_2, df_3]
key_list = [&#39;Salary_base&#39;, &#39;Children_base&#39;, &#39;Expenditure_base&#39;]
def subplot_func(df_base, df, key_list, axs, id_):
axs = axs.ravel()
for i in range(len(axs)):
key = key_list[i % 3]
group_id  = int(i/3+1)
axs[i].plot(df_base.groupby(by=&quot;id&quot;).get_group(group_id)[&#39;cycle_base&#39;], df_base.groupby(by=&quot;id&quot;).get_group(group_id)[key], &#39;b&#39;,  linewidth = &#39;1&#39;, label =key.replace(&#39;_base&#39;, &#39;&#39;) + &#39;: df_base&#39;)
axs[i].plot(df.groupby(by=&quot;id&quot;).get_group(group_id)[&#39;cycle&#39;], df.groupby(by=&quot;id&quot;).get_group(group_id)[key.replace(&#39;base&#39;, str(id_+1))], &#39;r&#39;,  linewidth = &#39;1&#39;, label =key.replace(&#39;_base&#39;, &#39;&#39;)+&#39;: df_&#39; + str(id_+1))
axs[i].set_xlabel(&#39;cycle&#39;)
if i % 3 ==0: 
axs[i].set_ylabel(&#39;wrt to id&#39; + str(group_id))
axs[i].legend()
for id_ in range(3):
fig, axs = plt.subplots(3,3, figsize=(10,8))
subplot_func(df_base, df_list[id_], key_list, axs, id_)

huangapple
  • 本文由 发表于 2023年6月22日 14:24:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定