Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 504
Edamame Avatar asked Jul 22 '26 11:07

Edamame


1 Answers

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

like image 81
Diziet Asahi Avatar answered Jul 25 '26 08:07

Diziet Asahi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!