matplotlib:每个类别的箱形图 [英] matplotlib: box plot for each category

查看:245
本文介绍了matplotlib:每个类别的箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的熊猫数据框有两列:categoryduration.和 我使用以下代码绘制所有数据点的箱形图.

My pandas data frame has two columns: category and duration. And I use the following code to make a box plot of all data points.

import matplotlib.pyplot as plt
plt.boxplot(df.duration)
plt.show()

但是,如果每个category都需要一个框,如何修改上面的代码?谢谢!

However, if I want one box fore each category, how do I modify the above code? Thanks!

推荐答案

我们可以使用熊猫

#df=pd.DataFrame({'category':list('aacde'),'duration':[1,3,2,3,4]}) sample data
df.assign(index=df.groupby('category').cumcount()).pivot('index','category','duration').plot(kind='box')

这篇关于matplotlib:每个类别的箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆