Show Menu

Import the Seaborn Library

from matplotlib import pyplot as plt
import seaborn as sns

import numpy as np
Seab­orn is a extension to Matp­lot­lib with more visually appealing syntax and additional Chart Types. That's why Matp­lot­lib should also be imported.

If we want to calculate aggr­ega­tes we need to import numpy aswell.

Bar Plot

 data=df ,
 x="x value column" ,
 y="y value column",

# everything specified below is optional
estimator=np.median | len
hue="column to compare"

# ci="sd" changes the error bar to standard deviation
# estimator is used to specifiy the aggregation and takes any argument that works on a list. (examples provided in code)
# hue adds a nested categorical variable to compare to the "y value column"
If the specified columns need to be aggr­ega­ted first, Seab­orn will perform that aggreg­ation automa­tic­ally. (mean by default)

Seab­orn will, by default, provide an error bar displaying the boot­str­apped confidence interv­al­(95%).

Aggregates (with numpy)


KDE (Key Densitiy Estimator) Plots

sns.k­de­plo­t­(d­ata­set1, shade­=True)
sns.k­de­plo­t­(d­ata­set2, shade­=True)

KDE Plots show the dist­rib­ution of an univ­ari­ate dataset.
univ­ari­ate datasets have only one variable. (e.g.: Temper­ature)

shade defines if the are under the line is shaded


# optional

# In Seaborn it's also possible to plot multiple Boxplots in one viz
The box represents the inte­rqu­artile range
The line in the middle of the box is the median
The end lines are the first and third quarti­les
The diamonds show outl­iers

Violin Plots

Two KDE plots that are symmet­rical along the center line. (Just for visual effect)

A white dot represents the median.

The thick black line in the center of each violin represents the interq­uartile range.

The lines that extend from the center are the confidence intervals (95%)

Seaborn Styling (Figure Style and Scale)

# Themes: (called prior to plot)
->darkgrid, whitegrid, dark, white, ticks

#Removes Plot Borders (called after plot)
sns.despine() (default: top=True, right=True)
-> bottom, left

#Adjust font- and label size
sns.set_context(context="paper", font_scale=1.4, rc={"grid.linewidth": 0.6} )
-> paper, notebook (default), talk, poster
   In order of relative size
-> Arguments for rc parameter:
 'axes.labelsize': 17.6,
 'axes.titlesize': 19.0,
 'font.size': 19.2,
 'grid.linewidth': 1.6,
 'legend.fontsize': 16.0,
 'lines.linewidth': 2.8,
 'lines.markeredgewidth': 0.0,
 'lines.markersize': 11.2,
 'patch.linewidth': 0.48,
 'xtick.labelsize': 16.0,
 'xtick.major.pad': 11.2,
 'xtick.major.width': 1.6,
 'xtick.minor.width': 0.8,
 'ytick.labelsize': 16.0,
 'ytick.major.pad': 11.2,
 'ytick.major.width': 1.6,
 'ytick.minor.width': 0.8

Seaborn Styling (Color)

# If you want to quickly see what a palette looks like
 # Save a palette to a variable:
   palette = sns.color_palette("bright")

 # Use palplot and pass in the variable:

# Select a palette in Seaborn:

# Default Palettes
-> deep, muted, pastel, bright, dark, colorblind

# More Palettes using Color Brewer:

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          More Cheat Sheets by Justin1209