Seaborn¶
Seaborn is a high-level interface to matplotlib. It is great for rapidly creating standard yet pretty data visualizations.
Pros:
- Easy-to-use, high-level
- Pretty statistical data visualizations by default
- Theming and color palettes
- Extendable or modifable with matplotlib code
Cons:
- Limited support for complex visualizations (in which case, consider matplotlib)
To install, run:
pip install seaborn
Details For use in Jupyter notebooks, make sure you enable matplotlib mode to see the plots. Alternatively, call:
import matplotlib.pyplot as plt
plt.show()
Code
import seaborn as sns
import seaborn.objects as so
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
sns.set_theme()
Theming¶
If no theme is set, the default matplotlib theme is used.
| Action | Code | Details |
|---|---|---|
| Set default theme |
|
Plots¶
Distribution plots¶
See https://seaborn.pydata.org/generated/seaborn.displot.html
Boxplots¶
| Action | Code | Details |
|---|---|---|
| Boxplot |
|
|
| Boxplot by group |
|
|
| Facetted boxplot by two grouping factors |
|
|
| Boxplot by two grouping factors |
|
Histograms¶
| Action | Code | Details |
|---|---|---|
| Plot histogram |
|
|
| Plot histogram with bins centered at discrete values |
|
For example, at integer values |
| Plot histogram with bin-width |
|
|
| Plot histogram with a given number of bins |
|
|
| Plot histogram and kernel density |
|
|
| Plot normalized histogram |
|
|
| Plot normalized histogram as percentage |
|
|
| Facetted histogram chart |
|
Kernel density plots¶
| Action | Code | Details |
|---|---|---|
| Plot kernel density |
|
|
| Plot kernel density by group |
|
|
| Facetted kernel density chart |
|
|
| Violin plot |
|
|
| Violin plot with grouping factor |
|
Empirical cumulative density plots¶
| Action | Code | Details |
|---|---|---|
| Plot stepped cumulative density |
|
|
| Plot stepped inverse cumulative density |
|
|
| Plot stepped cumulative density by group |
|
|
| Facetted plot of stepped cumulative density |
|
|
| Plot cumulative density as barplot |
|
|
| Plot cumulative density by group as barplot |
|
Each group reaches 1.0 |
| Facetted plot of cumulative density by group as barplot |
|
Each group reaches 1.0 |
| Plot step-wise cumulative density with density rug |
|
Bivariate plots¶
Plots involving two continuous variables.
Scatter plots¶
| Action | Code | Details |
|---|---|---|
| Scatter plot |
|
|
| Scatter plot with grouping factor |
|
|
| Facetted scatter plot |
|
Joint plots¶
| Action | Code | Details |
|---|---|---|
| Scatter plot with KDE axes |
|
Scatter and KDE |
| Scatter plot with KDE axes, with grouping factor |
|
|
| Plot heatmap with histograms along axes |
|
Line plots¶
| Action | Code | Details |
|---|---|---|
| Line plot |
|
|
| Line plot, with separate lines per group |
|
|
| Line plot involving duplicate observations per x, as confidence region |
|
|
| Facetted line plot |
|
Multivariate plots¶
Plots involving more than two continuous variables.
| Action | Code | Details |
|---|---|---|
| Pairs plot |
|
Plots pairwise scatter, and KDE along the diagonal |
Facetted plotting options¶
The function for creating facetted plots differs between the kinds of plots, so only options are described here.
See, for example, sns.displot(), sns.relplot() and sns.catplot().
| Action | Code | Details |
|---|---|---|
| Horizontally stacked facets |
|
|
| Horizontally stacked facets, wrap after n columns |
|
|
| Vertically stacked facets |
|
|
| Vertically stacked facets, wrap after n columns |
|
|
| Grid-based facets along two grouping factors |
|
Plot configuration¶
Arguments should be used inside the sns.relplot function.
| Action | Code | Details |
|---|---|---|
| Flip axes |
|
|
| Log-scale with base b |
|
|
| Format axis with comma as thousands separator |
|
Very tedious |
| Format axis with percentage labels |
|
PercentFormatter assumes percent input, not proportion, so 1.0 = '1.0%' |
| 1:1 aspect ratio |
|
|
| Hide legend |
|