How can I create a boxplot of multiple columns using Seaborn? 2

How can I create a boxplot of multiple columns using Seaborn?

Creating a boxplot of multiple columns using Seaborn involves first importing the Seaborn library and loading the desired dataset. Then, the boxplot function can be used, specifying the columns to be plotted and the dataset as parameters. This will generate a visual representation of the distribution of the selected columns, making it easier to compare and analyze the data. Additional customization options are available, such as changing the color or adding labels. Overall, using Seaborn to create a boxplot of multiple columns is a simple and efficient way to visualize data and gain insights.

Seaborn: Create a Boxplot of Multiple Columns


You can use the following basic syntax in seaborn to create a boxplot of multiple columns of a pandas DataFrame:

sns.boxplot(x='variable', y='value', data=df)

The following example shows how to use this syntax in practice.

Example: Boxplot of Multiple Columns Using Seaborn

Suppose we have the following pandas DataFrame that shows the points scored by players on three different basketball teams:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'A': [5, 7, 7, 9, 12, 12],
                   'B': [8, 8, 9, 13, 15, 17],
                   'C': [1, 2, 2, 4, 5, 7]})

#view DataFrame
df

        A	B	C
0	5	8	1
1	7	8	2
2	7	9	2
3	9	13	4
4	12	15	5
5	12	17	7

Suppose we’d like to create three boxplots that show the distribution of points scored by each team.

To create multiple boxplots in seaborn, we must first melt the pandas DataFrame into a :

#melt data frame into long format
df_melted = pd.melt(df)

#view first 10 rows of melted data frame
df_melted.head(10)

	variable value
0	A	 5
1	A	 7
2	A	 7
3	A	 9
4	A	 12
5	A	 12
6	B	 8
7	B	 8
8	B	 9
9	B	 13

Now we can create multiple boxplots using seaborn:

import matplotlib.pyplotas plt
import seaborn as sns

#create seaborn boxplots by group
sns.boxplot(x='variable', y='value', data=df_melted)

seaborn boxplot of multiple columns

The x-axis displays the teams and the y-axis displays the distribution of points scored.

Note that we can use the following syntax to also and modify the :

import matplotlib.pyplotas plt
import seaborn as sns

#create seaborn boxplots by group
sns.boxplot(x='variable', y='value', data=df_melted).set(title='Points by Team')

#modify axis labels
plt.xlabel('Team')
plt.ylabel('Points')

Additional Resources

Cite this article

stats writer (2024). How can I create a boxplot of multiple columns using Seaborn?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-boxplot-of-multiple-columns-using-seaborn/

stats writer. "How can I create a boxplot of multiple columns using Seaborn?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-boxplot-of-multiple-columns-using-seaborn/.

stats writer. "How can I create a boxplot of multiple columns using Seaborn?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-boxplot-of-multiple-columns-using-seaborn/.

stats writer (2024) 'How can I create a boxplot of multiple columns using Seaborn?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-boxplot-of-multiple-columns-using-seaborn/.

[1] stats writer, "How can I create a boxplot of multiple columns using Seaborn?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I create a boxplot of multiple columns using Seaborn?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top