Better Visualization : Seaborn
After discussing basic visualization with Matplotlib, now let’s try another but more attractive visualization library called Seaborn. Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics.
There are 5 main plots that really useful for your visualization.
- Relational Plot
- Distribution Plot
- Categorical Plot
- Regression Plot
- Matrix Plot
and 3 multi-plot grids which are
- Facet grids
- Pair grids
- Joint grids
But first, let’s use Seaborn own dataset sample called tips
tips = sns.load_dataset('tips')
Relational Plot
This function provides access to several different axes-level functions that show the relationship between two variables or features with semantic mappings of subsets. The kind
parameter selects the underlying axes-level function like scatter
and line
Example
sns.relplot(data=tips, x='total_bill', y='tip', kind='scatter')
sns.relplot(data=tips, x='total_bill', y='tip', kind='line')
When thinking about how to assign variables to different facets, a general rule is that it makes sense to use hue
for the most important comparison, followed by col
and row
. However, always think about your particular dataset and the goals of the visualization you are creating.
You can visualize comparison between 2 features with another categorical features with hue
(by color), column
, and row
(by another plot).
Example
sns.relplot(data=tips, x="total_bill", y="tip", hue="day", col="time", row="sex")
Distribution Plot
This function provides access to several approaches for visualizing the univariate (single feature) or bivariate (double features) distribution of data, including subsets of data defined by semantic mapping and faceting across multiple subplots. The kind
parameter selects the approach to use are hist
(Histogram), kde
(A kernel density estimate (KDE) plot is a method for visualizing the density of dataset), and ecdf
(stair-like, univariate only).
Example
sns.displot(data=tips, x='total_bill', kind='hist')
sns.displot(data=tips, x='total_bill', kind='kde')
sns.displot(data=tips, x='total_bill', kind='ecdf')
You can also showing histogram or ecdf with kde.
sns.displot(data=tips, x='total_bill', kind='hist', kde=True)
Then, if you want to visualize between 2 features
sns.displot(data=tips, x='total_bill', y='tip', kind='kde')
Categorical Plot
This function provides access to several axes-level functions that show the relationship between a numerical and one or more categorical variables using one of several visual representations. The kind
parameter selects the underlying axes-level function to use:
Categorical scatterplots:
-
stripplot()
(withkind="strip"
; the default)sns.catplot(data=tips, x='sex', y='total_bill', kind='strip')
-
swarmplot()
(withkind="swarm"
)
sns.catplot(data=tips, x='sex', y='total_bill', kind='swarm')
Categorical distribution plots:
-
boxplot()
(withkind="box"
)sns.catplot(data=tips, x='sex', y='total_bill', kind='box')
-
violinplot()
(withkind="violin"
)sns.catplot(data=tips, x='sex', y='total_bill', kind='violin')
-
boxenplot()
(withkind="boxen"
)sns.catplot(data=tips, x='sex', y='total_bill', kind='boxen')
Categorical estimate plots:
-
pointplot()
(withkind="point"
)sns.catplot(data=tips, x='day', y='total_bill', kind='point')
-
barplot()
(withkind="bar"
)sns.catplot(data=tips, x='day', y='total_bill', kind='bar')
-
countplot()
(withkind="count"
)sns.catplot(data=tips, x='sex', hue='day', kind='count')
Regression Plot
To plot data with regression model fits across a FacetGrid.
sns.lmplot(data=tips, x='tip', y='total_bill', hue='sex', col='smoker')
Matrix Plot
Plot rectangular data as a color-encoded matrix. This is an Axes-level function and will draw the heatmap into the currently-active Axes if none is provided to the ax
argument. Part of this Axes space will be taken and used to plot a colormap, unless cbar
is False or a separate Axes is provided to cbar_ax
.
Example
sns.heatmap(tips.corr())
Leave a comment