#30 Basic Boxplot with Seaborn

This page aims to explain how to plot a basic boxplot with seaborn. Boxplot are made using the … boxplot() function! Three types of input can be used to make a boxplot:

1 - One numerical variable only

If you have only one numerical variable, you can use this code to get a boxplot with only one group (left chart).

# library & dataset
import seaborn as sns
df = sns.load_dataset('iris')

# Make boxplot for one group only
sns.boxplot( y=df["sepal_length"] )
#sns.plt.show()
2 - One numerical variable, and several groups

Let’s say we want to study the distribution of a numerical variable, but for each group separately. Here we study the sepal length of 3 species of flower.


# library & dataset
import seaborn as sns
df = sns.load_dataset('iris')

sns.boxplot( x=df["species"], y=df["sepal_length"] )
#sns.plt.show()

3 - Several numerical variable

Finally we can study the distribution of several numerical variables, let’s say sepal length and width:


# library & dataset
import seaborn as sns
df = sns.load_dataset('iris')

sns.boxplot(data=df.ix[:,0:2])
#sns.plt.show()

 

Leave a Reply

Your email address will not be published.