如何在pandas中绘制分面图

13

这是我现在拥有的:

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。


你可以看一下 seaborn(基于 pandas 和 matplotlib)的教程:http://stanford.edu/~mwaskom/software/seaborn/tutorial/axis_grids.html - JohnE
1个回答

16

您需要将 type 转化为列,然后使用 subplots 参数:

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

结果图


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接