pandas 我如何在熊猫中绘制刻面图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29786227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:14:43  来源:igfitidea点击:

How do i plot facet plots in pandas

pythonpandasplotvisualizationfacet

提问by I am not George

This is what I have right now:

这就是我现在所拥有的:

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')

enter image description here

在此处输入图片说明

How do I plot facet based on 'type'? I want two different plots, one for type = 1 and another type = 2.

如何根据“类型”绘制构面?我想要两个不同的图,一个类型 = 1,另一个类型 = 2。

回答by JAB

You need to unstack so typeare columns, and then use the subplotsparameter:

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

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

Resulting plot

结果图