matplotlib:每个类别的箱线图
matplotlib: box plot for each category
我的 pandas 数据框有两列:category
和 duration
。和
我使用以下代码绘制所有数据点的箱线图。
import matplotlib.pyplot as plt
plt.boxplot(df.duration)
plt.show()
但是,如果我想要每个 category
一个盒子,我该如何修改上面的代码?谢谢!
我们可以用 pandas
#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')
除了, which is spot on, you might want to check out the seaborn library。就是为了做这种剧情。
Seaborn is a Python visualization library based on matplotlib. It
provides a high-level interface for drawing attractive statistical
graphics.
Draw a box plot to show distributions with respect to categories.
sns.boxplot(data=df, x='category', y='duration')
我的 pandas 数据框有两列:category
和 duration
。和
我使用以下代码绘制所有数据点的箱线图。
import matplotlib.pyplot as plt
plt.boxplot(df.duration)
plt.show()
但是,如果我想要每个 category
一个盒子,我该如何修改上面的代码?谢谢!
我们可以用 pandas
#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')
除了
Seaborn is a Python visualization library based on matplotlib. It provides a high-level interface for drawing attractive statistical graphics.
Draw a box plot to show distributions with respect to categories.
sns.boxplot(data=df, x='category', y='duration')