Edamame
Edamame

Reputation: 25366

matplotlib: box plot for each category

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

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

Upvotes: 7

Views: 17292

Answers (2)

Diziet Asahi
Diziet Asahi

Reputation: 40707

In addition to Wen's answer, which is spot on, you might want to check out the seaborn library. It was made to do this kind of plot.

Seaborn is a Python visualization library based on matplotlib. It provides a high-level interface for drawing attractive statistical graphics.

Check the documentation for boxplots

Draw a box plot to show distributions with respect to categories.

sns.boxplot(data=df, x='category', y='duration')

enter image description here

Upvotes: 9

BENY
BENY

Reputation: 323306

We can do it with 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')

enter image description here

Upvotes: 3

Related Questions