我如何在 pandas 中绘制分面图

How do i plot facet plots in pandas

这是我现在拥有的:

np.random.seed(1234)
test = pd.DataFrame({'week': [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2],
                     'score': np.random.uniform(0, 1, 12),
                     'type': [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
                     'type2': [3, 3, 4, 4, 5, 5, 3, 3, 4, 4, 5, 5]})

test.groupby(['week', 'type', 'type2']).agg('sum').unstack().plot(kind='bar')

如何根据 'type' 绘制分面?我想要两个不同的地块,一个用于 type = 1,另一个用于 type = 2.

您需要取消堆叠以便 type 成为列,然后使用 subplots 参数:

test.groupby(['week', 'type', 
              'type2']).agg('sum').unstack(1).plot(kind='bar', subplots=True)