The Stats Files

Table of Contents

Boxplot using Seaborn

This is a very basic boxplot which can be used to compare the distributions of two groups. The boxplot plot is created with the boxplot() method. The example below loads the Iris flower data set. Then the presented boxplot shows the minimum, maximum, 1st quartile and 3rd quartile. I prefer to work with Jupyter Notebooks in the Google Colab environment. In this first plot, I have not used a categorical variable to create multiple plots. Rather this is the distribution of the sepal length of all iris species in the data set.
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset(‘iris’)
df.head()
sns.boxplot( y=df["sepal_length"] );
plt.show()
Vertical Boxplot

Multiple Boxplot

Again using the Iris dataset. Note the categorical variable "species" is mapped to the x-axis and the quantitative variable "sepal_length" is mapped to the y-axis.
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset(‘iris’)
df.head()
sns.boxplot( y=df["sepal_length"], x=df["species"] );
plt.show()
Multiple Boxplot Vertical

Horizontal Boxplot

By mapping the categorical variable "species" to the y-axis and the quantitative to the x-axis, we have a multiple horizontal plot.
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset(‘iris’)
df.head()
sns.boxplot( y=df["species"], x=df["sepal_length"] );
plt.show()
Multiple Horizonal Boxplot

Colors

Seaborn boxplot colors can be changed using the "pallette" parameters.
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset(‘iris’)
df.head()
sns.boxplot( y=df["sepal_length"],x=df["species"] , palette= "Blues");
plt.show()
Blue Boxplot

Plot Size

In order to change the figure size of the pyplot/seaborn image use pyplot.figure. The values in "figsize" are for a final image in inches of 16×10 approx.
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset(‘iris’)
df.head()
plt.figure(figsize=(16, 10))
sns.boxplot( y=df["species"], x=df["sepal_length"] , palette= "Blues", width= 0.5);
plt.show()
Horizontal Boxplot
Dr. Dawn Wright

Welcome

Meet Dr. Dawn

I’ll help you find easy solutions to those statistics and analytics problems you love to hate. I show easy ways to use technology to solve them.