英文:
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 = {'id': [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,],
'cycle_base': [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,],
'Salary_base': [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,],
'Children_base': [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,],
'Expenditure_base': [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("\n df_base = \n",df_base)
########################################################################################
#Create dataframe_1
data_set_1 = {'id': [1, 1, 1,1, 1, 1, 1, 1, 1,
2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3,3,],
'cycle': [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,],
'Salary_1': [6, 7, 7, 7,8,9,10,11,12,
30, 10, 20, 4,
2, 1,19,0,11,2,28,5,],
'Children_1': [1, 2, 1, 0, 1, 1, 0,2, 1,
0, 1, 1, 2,
1, 2,0, 1, 2,0, 1,0,],
'Expenditure_1': [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("\n df_1 = \n",df_1)
########################################################################################
#Create dataframe_2
data_set_2 = {'id': [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,],
'cycle': [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,],
'Salary_2': [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,],
'Children_2': [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,],
'Expenditure_2': [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("\n df_2 = \n",df_2)
########################################################################################
#Create dataframe_3
data_set_3 = {'id': [1, 1, 1,1, 1, 1,
2, 2, 2,
3, 3, 3, 3, 3, 3, 3,],
'cycle': [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,],
'Salary_3': [2, 5, 2, 7,2,1,
3, 7, 4,
2, 8,6,10,11,4,13,],
'Children_3': [1, 0, 1, 0, 1, 0,
0, 1, 0,
1, 0,0, 1, 1,0,2,],
'Expenditure_3': [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("\n df_3 = \n",df_3)
The features are: 'Salary', 'Children', and 'Expenditure'.
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="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_1.groupby(by="id").get_group(3)['cycle'], df_1.groupby(by="id").get_group(3)['Children_1'], 'r', linewidth = '1', label ='Children: df_1')
plt.xlabel('cycle')
plt.legend()
## Expenditure plot
plt.subplot(3,3,9)
plt.plot(df_base.groupby(by="id").get_group(3)['cycle_base'], df_base.groupby(by="id").get_group(3)['Expenditure_base'], 'b', linewidth = '1', label ='Expenditure: df_base')
plt.plot(df_1.groupby(by="id").get_group(3)['cycle'], df_1.groupby(by="id").get_group(3)['Expenditure_1'], 'r', linewidth = '1', label ='Expenditure: df_1')
plt.xlabel('cycle')
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="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_2.groupby(by="id").get_group(1)['cycle'], df_2.groupby(by="id").get_group(1)['Salary_2'], 'r', linewidth = '1', label ='Salary: df_2')
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_2.groupby(by="id").get_group(1)['cycle'], df_2.groupby(by="id").get_group(1)['Children_2'], 'r', linewidth = '1', label ='Children: df_2')
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_2.groupby(by="id").get_group(1)['cycle'], df_2.groupby(by="id").get_group(1)['Expenditure_2'], 'r', linewidth = '1', label ='Expenditure: df_2')
plt.xlabel('cycle')
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="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_2.groupby(by="id").get_group(2)['cycle'], df_2.groupby(by="id").get_group(2)['Salary_2'], 'r', linewidth = '1', label ='Salary: df_2')
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_2.groupby(by="id").get_group(2)['cycle'], df_2.groupby(by="id").get_group(2)['Children_2'], 'r', linewidth = '1', label ='Children: df_2')
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_2.groupby(by="id").get_group(2)['cycle'], df_2.groupby(by="id").get_group(2)['Expenditure_2'], 'r', linewidth = '1', label ='Expenditure: df_2')
plt.xlabel('cycle')
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="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_2.groupby(by="id").get_group(3)['cycle'], df_2.groupby(by="id").get_group(3)['Salary_2'], 'r', linewidth = '1', label ='Salary: df_2')
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_2.groupby(by="id").get_group(3)['cycle'], df_2.groupby(by="id").get_group(3)['Children_2'], 'r', linewidth = '1', label ='Children: df_2')
plt.xlabel('cycle')
plt.legend()
## Expenditure plot
plt.subplot(3,3,9)
plt.plot(df_base.groupby(by="id").get_group(3)['cycle_base'], df_base.groupby(by="id").get_group(3)['Expenditure_base'], 'b', linewidth = '1', label ='Expenditure: df_base')
plt.plot(df_2.groupby(by="id").get_group(3)['cycle'], df_2.groupby(by="id").get_group(3)['Expenditure_2'], 'r', linewidth = '1', label ='Expenditure: df_2')
plt.xlabel('cycle')
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="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_3.groupby(by="id").get_group(1)['cycle'], df_3.groupby(by="id").get_group(1)['Salary_3'], 'r', linewidth = '1', label ='Salary: df_3')
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_3.groupby(by="id").get_group(1)['cycle'], df_3.groupby(by="id").get_group(1)['Children_3'], 'r', linewidth = '1', label ='Children: df_3')
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_3.groupby(by="id").get_group(1)['cycle'], df_3.groupby(by="id").get_group(1)['Expenditure_3'], 'r', linewidth = '1', label ='Expenditure: df_3')
plt.xlabel('cycle')
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="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_3.groupby(by="id").get_group(2)['cycle'], df_3.groupby(by="id").get_group(2)['Salary_3'], 'r', linewidth = '1', label ='Salary: df_3')
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_3.groupby(by="id").get_group(2)['cycle'], df_3.groupby(by="id").get_group(2)['Children_3'], 'r', linewidth = '1', label ='Children: df_3')
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_3.groupby(by="id").get_group(2)['cycle'], df_3.groupby(by="id").get_group(2)['Expenditure_3'], 'r', linewidth = '1', label ='Expenditure: df_3')
plt.xlabel('cycle')
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="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_3.groupby(by="id").get_group(3)['cycle'], df_3.groupby(by="id").get_group(3)['Salary_3'], 'r', linewidth = '1', label ='Salary: df_3')
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_3.groupby(by="id").get_group(3)['cycle'], df_3.groupby(by="id").get_group(3)['Children_3'], 'r', linewidth = '1', label ='Children: df_3')
plt.xlabel('cycle')
plt.legend()
## Expenditure plot
plt.subplot(3,3,9)
plt.plot(df_base.groupby(by="id").get_group(3)['cycle_base'], df_base.groupby(by="id").get_group(3)['Expenditure_base'], 'b', linewidth = '1', label ='Expenditure: df_base')
plt.plot(df_3.groupby(by="id").get_group(3)['cycle'], df_3.groupby(by="id").get_group(3)['Expenditure_3'], 'r', linewidth = '1', label ='Expenditure: df_3')
plt.xlabel('cycle')
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 = ['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_)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论