小编luc*_*usm的帖子

在多个图上绘制多列分组

我有这样的数据

ID    value_y   date_x      end_cutoff
1      75     2020-7-1      2021-01-17
1      73     2020-7-2      2021-01-17
1      74     2020-7-1      2021-06-05
1      71     2020-7-2      2021-06-05
2      111    2020-7-1      2021-01-17
2      112    2020-7-2      2021-01-17
2      113    2020-7-1      2021-06-05
2      115    2020-7-2      2021-06-05
   
Run Code Online (Sandbox Code Playgroud)

我想绘制以下数据以满足以下条件:

  1. 每个ID有1张图
  2. 每个图表都绘制了 n 条线(本例中为 2 条;每个 end_cutoff 为 1 条)

因此,理想情况下,在这个示例中,我将有两个单独的图,每个图都有两条线。

目前,这是我拥有的代码,但它将它们全部绘制在同一个图上,而不是为每个 ID 绘制一个新图。

 grouped = df_fit.groupby(['ID','end_cutoff'])
 fig, ax = plt.subplots()
 for (ID, end_cutoff), df_fit in grouped:
     ax.plot(df_fit['date_x'], df_fit['value_y'], label=ID+' '+str(end_cutoff.date()))
 plt.show()
Run Code Online (Sandbox Code Playgroud)

python matplotlib python-3.x pandas subplot

2
推荐指数
1
解决办法
627
查看次数

标签 统计

matplotlib ×1

pandas ×1

python ×1

python-3.x ×1

subplot ×1