Show Menu
Cheatography

Python - Seaborn Cheat Sheet (DRAFT) by

Seaborn for visualization library

This is a draft cheat sheet. It is a work in progress and is not finished yet.

TO START

import seaborn as sns
# If working on a notebook
%matplotlib inline

DISTRI­BUTION PLOTS

sns.di­stp­lot­(df­['c­ol'])
distri­bution plot
- bin = x
number of bins
- kde = False
remove the line
sns.jo­int­plo­t(x­,y,df)
plot 2 variables
- kind = ''
kind of plot*
sns.pa­irp­lot(df)
plot all vars combin
- hue='categ var'
distin­guish per var
- palette=''
set a color palette
sns.ru­gpl­ot(­df[­'col'])
idea of distri­bution
sns.kd­epl­ot(­df[­'col'])
kde plot
"­kin­d=" E.g.: hex, reg, kde.

CATEGO­RICAL PLOTS

sns.ba­rpl­ot(­x,y,df)
bar plot
- estima­tor=''*
bar values
sns.co­unt­plo­t(x,df)
bars = count
sns.bo­xpl­ot(­x,y­,,df)
box plot
- hue='categ var'
divide per var
- palette=''
set palette
- orient='h'
horiz. plot
sns.vi­oli­npl­ot(­x,y,df)
violin plot*
- hue='categ var'
divide per var
- palette=''
set palette
sns.st­rip­plo­t(x­,y,df)
bars = scatter
- jitter = True
add noise
- hue='categ var'
divide per var
- palette=''
set palette
- split = True
split by hue
sns.sw­arm­plo­t(x­,y,df)
swarm plot
- hue='categ var'
divide per var
- palette=''
set palette
- split = True
split by hue
** You can alo combine more plots by calling them one after each other.
sns.factorplot(x,y,df,kind)*
general categorical form of graph
TIP: when you call a plot function, press
"­shift + tab" to show the parameters needed.
estimator= can be, mean, std, or whatever function. It will display the bars or whatever you choose.
General form, kind=: e.g., point, bar, violin, etc.
 

ON CATEGO­RICAL PLOTS...

What is a violin plot?
It has a similar role of a box and whisker plots. It shows the distri­bution of quanti­tative data across several levels of one (or more) catego­rical variables. The violin plot features a kernel density estimation of the underlying distri­bution.
What is a strip plot?
It will draw a scatte­rplot where one variable is catego­rical. It is also a good complement to a box or violin plot in cases where you want to show all observ­ations along with some repres­ent­ation of the underlying distri­bution.
What is a swarm plot
It is similar to a stripp­lot(), but the points are adjusted (only along the catego­rical axis) so that they don’t overlap. This gives a better repres­ent­ation of the distri­bution of values, although it does not scale as well to large numbers of observ­ations.

MATRIX PLOTS

sns.he­atm­ap(­df.c­or­r())*
heat map plot
- annot = True
add actual values
- cmap=''
set a color palette
- lineco­lor=''
set borders
- linewi­dths=x
set border width
sns.cl­ust­erm­ap(­matrix)
hierarc. clustering
- cmap=''
set a color palette
- standa­rd_­scale = 1
normalise data
Heat map plot needs a correl­ation matrix, or more generally, a matrix. You can use the pivot_­tab­le(­ind­ex,­col­umn­s,v­alues) function to convert a dataframe.

GRIDS

sns.pa­irp­lot(df)
plot all vars combin­ation
- hue='categ var'
divide per var
- palette=''
set palette
g = sns.Pa­irG­rid(df)
set (empty) axis of pairplot
-g.map­(pl­t.s­catter)
populate axis with some plot
-g.map­_di­ag(­plt.hist)
set diag plots
-g.map­_up­per­(pl­t.s­catter)
set upper plots
-g.map_lower(sns.kdeplot)
set lower plots
g = sns.Fa­cet­Gri­d(d­f,c,r)
empty axis
-g = g.map(plt.hist, "­c")
populate axis histograms
-..g.m­ap(­sns.di­stplot, "­c")
populate axis with distplots
now some more complex stuff
- hue='categ var'
divide per var
-g = g.map(­plt.sc­atter, "­c", "­c").a­dd­_le­gend()
g = sns.Jo­int­Gri­d(x­,y,df)
general form of jointp­lot()
g = g.plot­(sn­s.r­egplot, sns.di­stplot)
join two plots
 

REGRESSION PLOTS

sns.lm­plo­t(x­,y,df)
creat reg plot
- hue='categ var'
divide per var
- palette=''
set palette
- markers='' *
set mark shape
- scatte­r_k­ws=­'dict' *
set marker size
sns.lm­plo­t(x­,y,df,col)
create a grid plot
sns.lmplot(x,y,df,row,col)
X*X grid
sns.lmplot(x,y,df,row,col,hue)
X*X*X grid
- aspect = x
choose ratio
- size = x
set size
marker­s='': e.g., o,v,etc.
scatte­r_k­ws='' e.g.: {'s':100}, it is a call to matplo­tlib. It will be hard to remember how to use these special cases, so no worries, you will have a look online.

STYLE and COLOR

sns.se­t_s­tyl­e('­dar­kgrid')
apply darkgrid style
sns.se­t_s­tyl­e('­ticks')
apply ticks style
sns.de­spine()
remove borders
sns.de­spi­ne(­lef­t=True)
remove left border
plt.fi­gur­e(f­igs­ize­=(x,x))
choose fig size
sns.se­t_c­ont­ext­('t­alk') *
set context
sns.set_context
(font_scale)
set font size
.set_c­ont­ext­(''): e.g.: paper, poster, talk, notebook, etc.