10 Must-know Seaborn Visualization Plots for Multivariate Data Analysis in Python _ by Susan Maina _ Towards Data Science
10 Must-know Seaborn Visualization Plots for Multivariate Data Analysis in Python _ by Susan Maina _ Towards Data Science
Seaborn is an interface built on top of Matplotlib that uses short lines of code to
create and style statistical plots from Pandas datafames. It utilizes Matplotlib under
the hood, and it is best to have a basic understanding of the figure, axes, and axis
objects.
We will use the vehicles dataset from Kaggle that is under the Open database
license. The code below imports the required libraries, sets the style, and loads the
dataset.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('darkgrid')
sns.set(font_scale=1.3)
cars = pd.read_csv('edited_cars.csv')
Before we continue, note that seaborn plots belong to one of two groups.
Axes-level plots — These mimic Matplotlib plots and can be bundled into
subplots using the ax parameter. They return an axes object and use normal
Matplotlib functions to style.
Figure-level plots — These provide a wrapper around axes plots and can only
create meaningful and related subplots because they control the entire figure.
They return either FacetGrid, PairGrid, or JointGrid objects and do not support
the ax parameter. They use different styling and customization inputs.
For each plot, I will mention which group it falls in.
The first two plots will be matrix plots, where you pass the whole dataframe to
visualize all the pairwise distributions in one plot.
1. Pair plot
A pair plot creates a grid of scatter plots to compare the distribution of pairs of
numeric variables. It also features a histogram for each feature in the diagonal
boxes.
Functions to use:
The kind parameter changes the type of bivariate plots created with kind=
sns.pairplot(cars);
In the pair plot below, the circled plots show an apparent linear relationship. The
diagonal line points out the histograms for each feature, and the pair plot’s top
triangle is a mirror image of the bottom.
Pairplot by author
We can add a third variable that segments the scatter plots by color using the
parameter hue=’cat_col’ .
sns.pairplot(
data=cars,
aspect=.85,
hue='transmission');
2. Heat map
A heat map is a color-coded graphical representation of values in a grid. It’s an ideal
plot to follow a pair plot because the plotted values represent the correlation
coefficients of the pairs that show the measure of the linear relationships.
In short, a pair plot shows the intuitive trends of the data, while a heat map plots the
actual correlation values using color.
Functions to use:
First, we run df.corr() to get a table with the correlation coefficients. This table is
also known as a correlation matrix.
cars.corr()
sns.heatmap() — Since the table above is not very intuitive, we’ll create a heatmap.
sns.set(font_scale=1.15)
plt.figure(figsize=(8,4))
sns.heatmap(
cars.corr(),
cmap='RdBu_r',
annot=True,
vmin=-1, vmax=1);
cmap=’RdBu_r’ sets the color scheme, annot=True draws the values inside the cells,
and vmin and vmax ensures the color codes start at -1 to 1.
Heatmap by author
Highly correlated features. These are the dark-red and dark-blue cells. Values
close to 1 mean a high positive linear relationship, while close to -1 show a high
negative relationship.
3. Scatter plot
A scatter plot shows the relationship between two numeric features by using dots to
visualize how these variables move together.
Functions to use:
sns.scatterplot() — axes-level plot
sns.relplot(kind=’line’) — figure-level
sns.regplot() — axes-level
sns.lmplot() — figure-level
sns.scatterplot(x='num_col1', y='num_col2', data=df) — Let us visualize the engine size with the
mileage (efficiency) of the vehicle.
sns.set(font_scale=1.3)
sns.scatterplot(
x='engine_cc',
y='mileage_kmpl',
data=cars)
plt.xlabel(
'Engine size in CC')
plt.ylabel(
'Fuel efficiency')
sns.regplot(x, y, data)
A reg plot draws a scatter plot with a regression line showing the trend of the data.
sns.regplot(
x='engine_cc',
y='mileage_kmpl',
data=cars)
plt.xlabel(
'Engine size in CC')
plt.ylabel(
'Fuel efficiency');
sns.scatterplot(
x='mileage_kmpl',
y='engine_cc',
data=cars,
palette='bright',
hue='fuel');
Scatter plot with hue by author
A rel plot, or relational plot, is used to create a scatter plot using kind=’scatter’
sns.relplot(
x='mileage_kmpl',
y='engine_cc',
data=cars,
palette='bright',
kind='scatter',
hue='fuel');
Relplot by author
sns.relplot(
x='year',
y='selling_price',
data=cars,
kind='scatter',
col='transmission');
Relplot by author
sns.relplot(
x='year',
y='selling_price',
data=cars,
palette='bright',
height=3, aspect=1.3,
kind='scatter',
hue='transmission',
col='fuel',
col_wrap=2);
Relational scatterplots by author
The lmplot is the figure-level version of a regplot that draws a scatter plot with a
regression line onto a Facet grid. It does not have a kind parameter.
sns.lmplot(
x="seats",
y="engine_cc",
data=cars,
palette='bright',
col="transmission",
hue="fuel");
lmplot by author
4. line plot
A line plot comprises dots connected by a line that shows the relationship between
the x and y variables. The x-axis usually contains time intervals, while the y-axis
holds a numeric variable whose changes we want to track over time.
Functions to use:
sns.lineplot(
x="year",
y="selling_price",
data=cars)
Line plot by author
sns.lineplot(
x="year",
y="selling_price",
data=cars,
palette='bright',
hue='fuel');
parameter.
sns.relplot(
x="year",
y="selling_price",
data=cars,
color='blue', height=4
kind='line',
col='transmission');
sns.relplot(
x="year",
y="selling_price",
data=cars,
palette='bright',
height=4,
kind='line',
col='transmission',
hue="fuel");
5. Joint plot
A joint plot comprises three charts in one. The center contains the bivariate
relationship between the x and y variables. The top and right-side plots show the
univariate distribution of the x-axis and y-axis variables, respectively.
Functions to use:
sns.jointplot(
x='max_power_bhp',
y='selling_price',
data=cars);
Joint plot by author
The joint plots in the image below utilize different kind parameters ( ‘kde’ , ‘hist’ ,
sns.jointplot(
x='selling_price',
y='max_power_bhp',
data=cars,
palette='bright',
hue='transmission');
Joint plot with hue parameter by author
Part two: Exploring the relationships between categorical and numeric relationships
In the following charts, the x-axis will hold a categorical variable and the y-axis a
numeric variable.
6. Bar plot
The bar chart uses bars of different heights to compare the distribution of a
numeric variable between groups of a categorical variable.
By default, bar heights are estimated using the “mean”. The estimator parameter
changes this aggregation function by using python’s inbuilt functions such as
estimator=max or len , or NumPy functions like np.max and np.median .
Functions to use:
Barplot by author
sns.barplot(
x='fuel',
y='selling_price',
data=cars,
palette='bright'
hue='transmission');
Barplot with hue by author
A catplot or categorical plot, uses the kind parameter to specify what categorical
plot to draw with options being ‘strip’ (default), ’swarm’, ‘box’, ‘violin’,
The plot below uses catplot to create a similar plot to the one above.
sns.catplot(
x='fuel',
y='selling_price',
data=cars,
palette='bright',
kind='bar',
hue='transmission');
Barplot with hue parameter by author
g = sns.catplot(
x='fuel',
y='selling_price',
data=cars,
palette='bright',
height=3, aspect=1.3,
kind='bar',
hue='transmission',
col ='seller_type',
col_wrap=2)
g.set_titles(
'Seller: {col_name}');
Categorical barplot by author
7. Point plot
Instead of bars like in a bar plot, a point plot draws dots to represent the mean (or
another estimate) of each category group. A line then joins the dots, making it easy
to compare how the y variable’s central tendency changes for the groups.
Functions to use:
sns.pointplot(
x='seller_type',
y='mileage_kmpl',
data=cars);
Point plot by author
When you add a third category using hue , a point plot is more informative than a
bar plot because a line is drawn through each “hue” class, making it easy to
compare how that class changes across the x variable’s groups.
sns.catplot(
x='transmission',
y='selling_price',
data=cars,
palette='bright',
kind='point',
hue='seller_type');
Categorical point plot by author
sns.catplot(
x='fuel',
y='year',
data=cars,
ci=None,
height=5, #default
aspect=.8,
kind='point',
hue='owner',
col='owner',
col_wrap=3);
Point plots using hue and col by author
8. Box plot
A box plot visualizes the distribution between numeric and categorical variables by
displaying the information about the quartiles.
From the plots, you can see the minimum value, median, maximum value, and
outliers for every category class.
Functions to use:
sns.boxplot(
x='owner',
y='engine_cc',
data=cars,
color='blue')
plt.xticks(rotation=45,
ha='right');
Boxplot by author
sns.catplot(
x='fuel',
y='max_power_bhp',
data=cars,
palette='bright',
kind = 'box',
col='transmission');
Categorical boxplots by author
g = sns.catplot(
x='owner',
y='year',
data=cars,
palette='bright',
height=3, aspect=1.5,
kind='box',
hue='transmission',
col='fuel',
col_wrap=2)
g.set_titles(
'Fuel: {col_name}');
g.set_xticklabels(
rotation=45, ha='right')
Categorical boxplots by author
9. Violin plot
In addition to the quartiles displayed by a box plot, a violin plot draws a Kernel
density estimate curve that shows probabilities of observations at different areas.
Functions to use:
sns.violinplot(
x='transmission',
y='engine_cc',
data=cars,
color='blue');
g = sns.catplot(
x='owner',
y='year',
data=cars,
palette='bright',
height=3,
aspect=2
split=False,
# split=True
kind='violin',
hue='transmission')
g.set_xticklabels(
rotation=45,
ha='right')
The violin plot supports the split parameter, which draws half of the violin plot for
each categorical class. Note that this works when the hue variable has only two
classes.
my_df = cars[cars['fuel'].isin(['Diesel','Petrol'])]
g = sns.catplot(
x="owner",
y="engine_cc",
data=my_df,
palette='bright',
kind = 'violin',
hue="transmission",
col = 'fuel')
g.set_xticklabels(
rotation=90);
Functions to use:
sns.stripplot() — axes-level plot
plt.figure(
figsize=(12, 6))
sns.stripplot(
x='year',
y='km_driven',
data=cars,
linewidth=.5,
color='blue')
plt.xticks(rotation=90);
Stripplot by author
sns.catplot(
x='seats',
y='km_driven',
data=cars,
palette='bright',
height=3,
aspect=2.5,
# dodge=True,
kind='strip',
hue='transmission');
g = sns.catplot(
x="seller_type",
y="year",
data=cars,
palette='bright',
height=3, aspect=1.6,
kind='strip',
hue='owner',
col='fuel',
col_wrap=2)
g.set_xticklabels(
rotation=45,
ha='right');
A strip plot can be used together with a violin plot or box plot to show the position
of gaps or outliers in the data.
g = sns.catplot(
x='seats',
y='mileage_kmpl',
data=cars,
palette='bright',
aspect=2,
inner=None,
kind='violin')
sns.stripplot(
x='seats',
y='mileage_kmpl',
data=cars,
color='k',
linewidth=0.2,
edgecolor='white',
ax=g.ax);
Additional remarks
For categorical plots such as bar plots and box plots, the bar direction can be re-
oriented to horizontal bars by switching up the x and y variables.
The row and col parameters of the FacetGrid figure-level objects used together
can add another dimension to the subplots. However, col_wrap cannot be with
the row parameter.
Conclusion
In this article, we performed bivariate and multivariate analyses on a dataset.
We first created matrix plots that visualized relationships in a grid to identify
numeric variables with high correlations. We then used different axes-level and
figure-level functions to create charts that explored the relationships between the
numeric and categorical columns. Find the code here on GitHub.
I hope you enjoyed the article. To receive more like this whenever I publish,
subscribe here. If you are not yet a medium member and would like to support me
as a writer, follow this link and I will earn a small commission. Thank you for
reading!
Editors Pick
Following
Google Colab: How to Upload Large Image Datasets from Github, Kaggle
and Local Machine
Learn how to upload large deep-learning datasets to a Google Colab Jupyter notebook
82 2