Create Boxplots by Group in seaborn

You can also plot grouped box plots using long-form and wide form data using yet another library called seaborn which is built on top matplotlib library.

Syntax: sns.boxplot(data, x, y)

Parameters:

  • data – specifies the dataframe to be used for the box plots
  • x –  specifies the column to be used in the x-axis
  • y – specifies the column to be used in y-axis

Grouped Box plots for long-form data:

Python3




# import the necessary python packages
import pandas as pd
import numpy as np
import seaborn as sns
 
# create long-form data
data = pd.DataFrame({'season': np.repeat(['Summer', 'Winter',
                                          'Spring'], 5),
                     'rainfall_amount': [17, 18, 19, 21, 27,
                                         33, 37, 33, 36, 12,
                                         14, 15, 16, 21, 22],
                     })
# print the data
print(data)
 
# use seaborn plot and specify the x and y
# columns and specify the dataframe
sns.boxplot(x='season', y='rainfall_amount', data=data)


Output:

Grouped Box plots for wide form data:

Python3




# import the necessary python packages
import pandas as pd
import numpy as np
import seaborn as sns
 
# create wide-form data
data = pd.DataFrame({'Summer': [17, 18, 19, 21, 27],
                     'Winter': [33, 37, 33, 36, 12],
                     'Spring': [14, 15, 16, 21, 22]})
# print the data
print(data)
# use melt to convert wide form to long form data
# use seaborn plot and specify the x and y columns
# and specify the dataframe
sns.boxplot(x='variable', y='value', data=pd.melt(data)).set(
    xlabel='Season',
    ylabel='Rainfall amount')


Output:

Code Explanation:

  • Import the necessary packages
  • Create a sample dataframe that lists seasonal rainfall amounts in wide form format as shown.
  • To plot the grouped box plot, the data has to be in a long format, so use pandas.melt() function to melt the data from the wide form to long-form.
  • When the wide form data is converted to long-form data, the two columns will be named as ‘variable’ and ‘value’ by default.
  • Use seaborn plot and pass the ‘variable’  as x and ‘value’ as y column of the boxplot and the corresponding dataframe.
  • Use the set() function to set the x and y-axis labels of the boxplot.


How to Create Boxplots by Group in Matplotlib?

Boxplots by groups can be created using the matplotlib package, but, however, if you wish to make more customizations to your grouped box plot, then the seaborn package provides a go-to function that supports a wide variety of customizations to the grouped box plots. Matplotlib doesn’t provide an explicit function to create a grouped box plot. We have to construct the plot as per the required format. This article discusses how to create grouped boxplots in matplotlib. 

Similar Reads

Create Boxplots by Group in Matplotlib

matplotlib.pyplot.boxplot() & matplotlib.pyplot.setp() are the two useful functions to create grouped boxplots...

Create Boxplots by Group in seaborn

...

Contact Us