Show Menu

Seaborn Cheat Sheet by

python     science     analytics     data     business     visualization     seaborn

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

sns.barplot(
 data=df ,
 x="x value column" ,
 y="y value column",

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

plt.show()

# 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)

Median
np.m­edi­an­(d­f.c­olu­mn_­name)

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

Boxplots

sns.boxplot(
 data=df,
 x='label',
 y='value',
 
# optional
 width=0.45
)
plt.show()

# 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

sns.violinplot(
 data=df,
 x="label",
 y="value"
)
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)
sns.set_style("")
->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:
   sns.palplot(palette)

# Select a palette in Seaborn:
  sns.set_palette("Paired")

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

# More Palettes using Color Brewer:
http://colorbrewer2.org

Download the Seaborn Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/justin1209_seaborn.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

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

          NumPy