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

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

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:

  1. plt_fig_verify = plt.figure(figsize=(10,8))
  2. ##########################################################################
  3. ## Plots for id1, overlap for df_1 and df_base
  4. ## Salary plot
  5. plt.subplot(3,3,1)
  6. 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')
  7. 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')
  8. plt.xlabel('cycle')
  9. plt.ylabel('wrt to id1')
  10. plt.legend()
  11. ## Children plot
  12. plt.subplot(3,3,2)
  13. 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')
  14. 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')
  15. plt.xlabel('cycle')
  16. plt.legend()
  17. ## Expenditure plot
  18. plt.subplot(3,3,3)
  19. 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')
  20. 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')
  21. plt.xlabel('cycle')
  22. plt.legend()
  23. ##########################################################################
  24. ## Plots for id2, overlap for df_1 and df_base
  25. ## Salary plot
  26. plt.subplot(3,3,4)
  27. 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')
  28. 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')
  29. plt.xlabel('cycle')
  30. plt.ylabel('wrt to id2')
  31. plt.legend()
  32. ## Children plot
  33. plt.subplot(3,3,5)
  34. 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')
  35. 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')
  36. plt.xlabel('cycle')
  37. plt.legend()
  38. ## Expenditure plot
  39. plt.subplot(3,3,6)
  40. 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')
  41. 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')
  42. plt.xlabel('cycle')
  43. plt.legend()
  44. ##########################################################################
  45. ## Plots for id3, overlap for df_1 and df_base
  46. ## Salary plot
  47. plt.subplot(3,3,7)
  48. 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')
  49. 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')
  50. plt.xlabel('cycle')
  51. plt.ylabel('wrt to id3')
  52. plt.legend()
  53. ## Children plot
  54. plt.subplot(3,3,8)
  55. 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')
  56. plt.plot(df_
  57. <details>
  58. <summary>英文:</summary>
  59. I have a baseline dataframe (df_base) and 3 other dataframes (df_1/df_2/df_3) as such:
  60. #Load the required libraries
  61. import pandas as pd
  62. import matplotlib.pyplot as plt
  63. ########################################################################################
  64. #Create dataframe_Baseline
  65. data_set_base = {&#39;id&#39;: [1, 1, 1,1, 1, 1, 1, 1, 1,1,1,
  66. 2, 2, 2, 2,2,2,
  67. 3, 3, 3, 3, 3, 3, 3,3,],
  68. &#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,
  69. 0.0, 0.2,0.4, 0.6,0.8,1,
  70. 0.0, 0.2,0.4, 0.6, 0.8,1.0,1.2,1.4,],
  71. &#39;Salary_base&#39;: [1, 4, 3, 8,2,9,10,5,1,6,1,
  72. 1, 0, 1, 5,3,8,
  73. 12, 3,29,10,11,1,0,3,],
  74. &#39;Children_base&#39;: [2, 0, 1, 0, 2, 0, 0,1, 0,0,1,
  75. 2, 1, 0, 0,1,1,
  76. 0, 1,2, 1, 1,0, 0,1,],
  77. &#39;Expenditure_base&#39;: [20, 10, 128, 76, 30, 40, 85, 60, 27,45,84,
  78. 25, 100, 120,82,100,110,
  79. 28, 15,90, 80, 66, 120, 35, 67,],
  80. }
  81. #Convert to dataframe_Baseline
  82. df_base = pd.DataFrame(data_set_base)
  83. print(&quot;\n df_base = \n&quot;,df_base)
  84. ########################################################################################
  85. #Create dataframe_1
  86. data_set_1 = {&#39;id&#39;: [1, 1, 1,1, 1, 1, 1, 1, 1,
  87. 2, 2, 2, 2,
  88. 3, 3, 3, 3, 3, 3, 3,3,],
  89. &#39;cycle&#39;: [0.0, 0.2,0.4, 0.6, 0.8, 1,1.2,1.4,1.6,
  90. 0.0, 0.2,0.4, 0.6,
  91. 0.0, 0.2,0.4, 0.6, 0.8,1.0,1.2,1.4,],
  92. &#39;Salary_1&#39;: [6, 7, 7, 7,8,9,10,11,12,
  93. 30, 10, 20, 4,
  94. 2, 1,19,0,11,2,28,5,],
  95. &#39;Children_1&#39;: [1, 2, 1, 0, 1, 1, 0,2, 1,
  96. 0, 1, 1, 2,
  97. 1, 2,0, 1, 2,0, 1,0,],
  98. &#39;Expenditure_1&#39;: [141, 123, 128, 66, 66, 120, 141, 52, 52,
  99. 141, 96, 120,120,
  100. 141, 15,123, 128, 66, 120, 141, 141,],
  101. }
  102. #Convert to dataframe_1
  103. df_1 = pd.DataFrame(data_set_1)
  104. print(&quot;\n df_1 = \n&quot;,df_1)
  105. ########################################################################################
  106. #Create dataframe_2
  107. data_set_2 = {&#39;id&#39;: [1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1,
  108. 2, 2, 2, 2, 2, 2, 2,
  109. 3, 3, 3, 3, 3, 3, 3,3,],
  110. &#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,
  111. 0.0, 0.2,0.4, 0.6,0.8,1.0,1.2,
  112. 0.0, 0.2,0.4, 0.6, 0.8,1.0,1.2,1.4,],
  113. &#39;Salary_2&#39;: [7, 8, 8, 8,8,9,14,21,12,19,14,20,
  114. 1, 6, 3, 8,4,9,8,
  115. 6, 4,9,10,4,12,13,6,],
  116. &#39;Children_2&#39;: [1, 0, 1, 1, 0, 1, 2,2, 0, 1, 0, 2,
  117. 1, 0, 1, 2, 1, 1, 1,
  118. 1, 2,1, 0, 2,0, 1,0,],
  119. &#39;Expenditure_2&#39;: [79, 82, 128, 66, 42, 120, 141, 52,96, 120, 141, 52,
  120. 30, 96, 86,120, 91, 52,96,
  121. 28, 15,51, 128, 76, 120, 87, 141,],
  122. }
  123. #Convert to dataframe_2
  124. df_2 = pd.DataFrame(data_set_2)
  125. print(&quot;\n df_2 = \n&quot;,df_2)
  126. ########################################################################################
  127. #Create dataframe_3
  128. data_set_3 = {&#39;id&#39;: [1, 1, 1,1, 1, 1,
  129. 2, 2, 2,
  130. 3, 3, 3, 3, 3, 3, 3,],
  131. &#39;cycle&#39;: [0.0, 0.2,0.4, 0.6, 0.8, 1,
  132. 0.0, 0.2,0.4,
  133. 0.0, 0.2,0.4, 0.6, 0.8,1.0,1.2,],
  134. &#39;Salary_3&#39;: [2, 5, 2, 7,2,1,
  135. 3, 7, 4,
  136. 2, 8,6,10,11,4,13,],
  137. &#39;Children_3&#39;: [1, 0, 1, 0, 1, 0,
  138. 0, 1, 0,
  139. 1, 0,0, 1, 1,0,2,],
  140. &#39;Expenditure_3&#39;: [100, 50, 10, 66, 66, 80,
  141. 10, 5, 80,
  142. 70, 15,40, 85, 66, 93, 60, ],
  143. }
  144. #Convert to dataframe_1
  145. df_3 = pd.DataFrame(data_set_3)
  146. print(&quot;\n df_3 = \n&quot;,df_3)
  147. The features are: &#39;Salary&#39;, &#39;Children&#39;, and &#39;Expenditure&#39;.
  148. Every dataframe (df_base/df_1/df_2/df_3) has ids from 1 to 4.
  149. 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.
  150. For df_1 overlapping with df_base these are the codes:
  151. plt_fig_verify = plt.figure(figsize=(10,8))
  152. ##########################################################################
  153. ## Plots for id1, overlap for df_1 and df_base
  154. ## Salary plot
  155. plt.subplot(3,3,1)
  156. 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;)
  157. 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;)
  158. plt.xlabel(&#39;cycle&#39;)
  159. plt.ylabel(&#39;wrt to id1&#39;)
  160. plt.legend()
  161. ## Children plot
  162. plt.subplot(3,3,2)
  163. 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;)
  164. 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;)
  165. plt.xlabel(&#39;cycle&#39;)
  166. plt.legend()
  167. ## Expenditure plot
  168. plt.subplot(3,3,3)
  169. 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;)
  170. 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;)
  171. plt.xlabel(&#39;cycle&#39;)
  172. plt.legend()
  173. ##########################################################################
  174. ## Plots for id2, overlap for df_1 and df_base
  175. ## Salary plot
  176. plt.subplot(3,3,4)
  177. 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;)
  178. 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;)
  179. plt.xlabel(&#39;cycle&#39;)
  180. plt.ylabel(&#39;wrt to id2&#39;)
  181. plt.legend()
  182. ## Children plot
  183. plt.subplot(3,3,5)
  184. 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;)
  185. 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;)
  186. plt.xlabel(&#39;cycle&#39;)
  187. plt.legend()
  188. ## Expenditure plot
  189. plt.subplot(3,3,6)
  190. 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;)
  191. 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;)
  192. plt.xlabel(&#39;cycle&#39;)
  193. plt.legend()
  194. ##########################################################################
  195. ## Plots for id3, overlap for df_1 and df_base
  196. ## Salary plot
  197. plt.subplot(3,3,7)
  198. 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;)
  199. 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;)
  200. plt.xlabel(&#39;cycle&#39;)
  201. plt.ylabel(&#39;wrt to id3&#39;)
  202. plt.legend()
  203. ## Children plot
  204. plt.subplot(3,3,8)
  205. 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;)
  206. 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;)
  207. plt.xlabel(&#39;cycle&#39;)
  208. plt.legend()
  209. ## Expenditure plot
  210. plt.subplot(3,3,9)
  211. 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;)
  212. 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;)
  213. plt.xlabel(&#39;cycle&#39;)
  214. plt.legend()
  215. plt.show()
  216. The plot looks as such:
  217. [![enter image description here][1]][1]
  218. Likewise for df_2 overlapped with df_base:
  219. plt_fig_verify = plt.figure(figsize=(10,8))
  220. ##########################################################################
  221. ## Plots for id1, overlap for df_2 and df_base
  222. ## Salary plot
  223. plt.subplot(3,3,1)
  224. 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;)
  225. 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;)
  226. plt.xlabel(&#39;cycle&#39;)
  227. plt.ylabel(&#39;wrt to id1&#39;)
  228. plt.legend()
  229. ## Children plot
  230. plt.subplot(3,3,2)
  231. 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;)
  232. 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;)
  233. plt.xlabel(&#39;cycle&#39;)
  234. plt.legend()
  235. ## Expenditure plot
  236. plt.subplot(3,3,3)
  237. 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;)
  238. 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;)
  239. plt.xlabel(&#39;cycle&#39;)
  240. plt.legend()
  241. ##########################################################################
  242. ## Plots for id2, overlap for df_2 and df_base
  243. ## Salary plot
  244. plt.subplot(3,3,4)
  245. 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;)
  246. 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;)
  247. plt.xlabel(&#39;cycle&#39;)
  248. plt.ylabel(&#39;wrt to id2&#39;)
  249. plt.legend()
  250. ## Children plot
  251. plt.subplot(3,3,5)
  252. 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;)
  253. 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;)
  254. plt.xlabel(&#39;cycle&#39;)
  255. plt.legend()
  256. ## Expenditure plot
  257. plt.subplot(3,3,6)
  258. 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;)
  259. 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;)
  260. plt.xlabel(&#39;cycle&#39;)
  261. plt.legend()
  262. ##########################################################################
  263. ## Plots for id3, overlap for df_2 and df_base
  264. ## Salary plot
  265. plt.subplot(3,3,7)
  266. 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;)
  267. 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;)
  268. plt.xlabel(&#39;cycle&#39;)
  269. plt.ylabel(&#39;wrt to id3&#39;)
  270. plt.legend()
  271. ## Children plot
  272. plt.subplot(3,3,8)
  273. 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;)
  274. 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;)
  275. plt.xlabel(&#39;cycle&#39;)
  276. plt.legend()
  277. ## Expenditure plot
  278. plt.subplot(3,3,9)
  279. 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;)
  280. 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;)
  281. plt.xlabel(&#39;cycle&#39;)
  282. plt.legend()
  283. plt.show()
  284. [![enter image description here][2]][2]
  285. And df_3 overlapped with df_base:
  286. plt_fig_verify = plt.figure(figsize=(10,8))
  287. ##########################################################################
  288. ## Plots for id1, overlap for df_3 and df_base
  289. ## Salary plot
  290. plt.subplot(3,3,1)
  291. 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;)
  292. 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;)
  293. plt.xlabel(&#39;cycle&#39;)
  294. plt.ylabel(&#39;wrt to id1&#39;)
  295. plt.legend()
  296. ## Children plot
  297. plt.subplot(3,3,2)
  298. 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;)
  299. 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;)
  300. plt.xlabel(&#39;cycle&#39;)
  301. plt.legend()
  302. ## Expenditure plot
  303. plt.subplot(3,3,3)
  304. 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;)
  305. 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;)
  306. plt.xlabel(&#39;cycle&#39;)
  307. plt.legend()
  308. ##########################################################################
  309. ## Plots for id2, overlap for df_3 and df_base
  310. ## Salary plot
  311. plt.subplot(3,3,4)
  312. 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;)
  313. 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;)
  314. plt.xlabel(&#39;cycle&#39;)
  315. plt.ylabel(&#39;wrt to id2&#39;)
  316. plt.legend()
  317. ## Children plot
  318. plt.subplot(3,3,5)
  319. 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;)
  320. 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;)
  321. plt.xlabel(&#39;cycle&#39;)
  322. plt.legend()
  323. ## Expenditure plot
  324. plt.subplot(3,3,6)
  325. 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;)
  326. 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;)
  327. plt.xlabel(&#39;cycle&#39;)
  328. plt.legend()
  329. ##########################################################################
  330. ## Plots for id3, overlap for df_3 and df_base
  331. ## Salary plot
  332. plt.subplot(3,3,7)
  333. 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;)
  334. 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;)
  335. plt.xlabel(&#39;cycle&#39;)
  336. plt.ylabel(&#39;wrt to id3&#39;)
  337. plt.legend()
  338. ## Children plot
  339. plt.subplot(3,3,8)
  340. 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;)
  341. 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;)
  342. plt.xlabel(&#39;cycle&#39;)
  343. plt.legend()
  344. ## Expenditure plot
  345. plt.subplot(3,3,9)
  346. 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;)
  347. 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;)
  348. plt.xlabel(&#39;cycle&#39;)
  349. plt.legend()
  350. plt.show()
  351. [![enter image description here][3]][3]
  352. 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.
  353. SO a **total of 27 subplot function needs to be written.**
  354. 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?
  355. Can somebody please help me out with this ?
  356. [1]: https://i.stack.imgur.com/XF2Yy.png
  357. [2]: https://i.stack.imgur.com/dyH0k.png
  358. [3]: https://i.stack.imgur.com/THllv.png
  359. </details>
  360. # 答案1
  361. **得分**: 1
  362. 这是绘制`subplots`的我的版本需要预先定义两个变量
  363. ```python
  364. df_list = [df_1, df_2, df_3]
  365. key_list = ['Salary_base', 'Children_base', 'Expenditure_base']
  366. def subplot_func(df_base, df, key_list, axs, id_):
  367. axs = axs.ravel()
  368. for i in range(len(axs)):
  369. key = key_list[i % 3]
  370. group_id = int(i/3+1)
  371. 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')
  372. 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))
  373. axs[i].set_xlabel('cycle')
  374. if i % 3 == 0:
  375. axs[i].set_ylabel('wrt to id' + str(group_id))
  376. axs[i].legend()
  377. for id_ in range(3):
  378. fig, axs = plt.subplots(3,3, figsize=(10,8))
  379. 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 :

  1. df_list = [df_1, df_2, df_3]
  2. key_list = [&#39;Salary_base&#39;, &#39;Children_base&#39;, &#39;Expenditure_base&#39;]
  3. def subplot_func(df_base, df, key_list, axs, id_):
  4. axs = axs.ravel()
  5. for i in range(len(axs)):
  6. key = key_list[i % 3]
  7. group_id = int(i/3+1)
  8. 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;)
  9. 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))
  10. axs[i].set_xlabel(&#39;cycle&#39;)
  11. if i % 3 ==0:
  12. axs[i].set_ylabel(&#39;wrt to id&#39; + str(group_id))
  13. axs[i].legend()
  14. for id_ in range(3):
  15. fig, axs = plt.subplots(3,3, figsize=(10,8))
  16. 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:

确定