Seaborn is a fantastic package for data visualisation.
Here we’ll look at two uses of it:
- Visualising all possible pairs of multidimensional data to help spot correlations.
- Easily adding legends to categorical data.
Use pip install seaborn to install Seaborn.
Visualising Correlations with Seaborn
We can easily use Seaborn to plot pairs of values from a Pandas data frame. Here we’ll use it to plot pairs of values from the iris flower dataset. The size of the plot can be controlled using the height parameter.
import sklearn.datasets as ds
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
iris = ds.load_iris(as_frame=True)
df = iris['data']
df['species_index'] = iris['target']
df['species'] = np.choose(iris['target'], iris['target_names'])
sns.pairplot(df, height=1.5)
plt.show()

It would be nice if we could colour these charts differently depending on the iris species. We can do this simply by specifying which column contains the categorical data we want to use to colour the graph. We can also specify a colour scheme. The ‘husl’ palette (hue/saturation/lightness) tries to make the colours harmonious.
import sklearn.datasets as ds
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
iris = ds.load_iris(as_frame=True)
df = iris['data']
df['species_index'] = iris['target']
df['species'] = np.choose(iris['target'], iris['target_names'])
sns.pairplot(df, height=1.5, hue='species', palette="husl")
plt.show()

Adding Legends to Categorical Data with Seaborn
Seaborn makes it really easy to add legends to categorical data.
Here we plot two dimensions of the iris data using Seaborn’s scatterplot function instead of using Matplotlib directly.
Notice how we specify what data to plot here: we pass the entire data frame to Seaborn, then specify the names of the columns to use for x, y and hue. These column names need to be exactly correct, so we have to include the “(cm)” which in this case is part of the column name.
import sklearn.datasets as ds
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
iris = ds.load_iris(as_frame=True)
df = iris['data']
df['species_index'] = iris['target']
df['species'] = np.choose(iris['target'], iris['target_names'])
sn.scatterplot(data=df, x='petal length (cm)', y='petal width (cm)', hue='species', palette='husl')
plt.show()

Using Subplots with Seaborn
One very handy feature of Seaborn is that we can specify the axes to use for drawing, allowing us to use Matplotlib subplots with Seaborn.
import sklearn.datasets as ds
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
iris = ds.load_iris(as_frame=True)
df = iris['data']
df['species_index'] = iris['target']
df['species'] = np.choose(iris['target'], iris['target_names'])
fig, axes = plt.subplots(ncols=2, nrows=1, figsize=(16,9))
axes[0].set_title("Petals")
axes[1].set_title("Sepals")
sn.scatterplot(data=df, x='petal length (cm)', y='petal width (cm)', hue='species', palette='husl', ax=axes[0])
sn.scatterplot(data=df, x='sepal length (cm)', y='sepal width (cm)', hue='species', palette='husl', ax=axes[1])
plt.show()

Leave a Reply