Cheat Sheet Seaborn Reference Guide

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 1

DownloadCheat Sheet Seaborn Reference Guide
Open PDF In BrowserView PDF
Python For Data Science Cheat Sheet 3
Seaborn

Learn Data Science Interactively at www.DataCamp.com

Statistical Data Visualization With Seaborn
The Python visualization library Seaborn is based on
matplotlib and provides a high-level interface for drawing
attractive statistical graphics.

The basic steps to creating plots with Seaborn are:
1. Prepare some data
2. Control figure aesthetics
3. Plot with Seaborn
4. Further customize your plot
>>>
>>>
>>>
>>>
>>>

import matplotlib.pyplot as plt
import seaborn as sns
Step 1
tips = sns.load_dataset("tips")
Step 2
sns.set_style("whitegrid")
Step 3
g = sns.lmplot(x="tip",
y="total_bill",
data=tips,
aspect=2)
>>> g = (g.set_axis_labels("Tip","Total bill(USD)").
set(xlim=(0,10),ylim=(0,100)))
Step 4
>>> plt.title("title")
>>> plt.show(g)
Step 5

1

Data
>>>
>>>
>>>
>>>

Also see Lists, NumPy & Pandas

import pandas as pd
import numpy as np
uniform_data = np.random.rand(10, 12)
data = pd.DataFrame({'x':np.arange(1,101),
'y':np.random.normal(0,4,100)})

Seaborn also offers built-in data sets:
>>> titanic = sns.load_dataset("titanic")
>>> iris = sns.load_dataset("iris")

2

Axis Grids

>>> g = sns.FacetGrid(titanic,
col="survived",
row="sex")
>>> g = g.map(plt.hist,"age")
>>> sns.factorplot(x="pclass",
y="survived",
hue="sex",
data=titanic)
>>> sns.lmplot(x="sepal_width",
y="sepal_length",
hue="species",
data=iris)

Scatterplot

Scatterplot with one
categorical variable

>>> sns.barplot(x="sex",
y="survived",
hue="class",
data=titanic)

Show point estimates and
confidence intervals with
scatterplot glyphs

>>> sns.countplot(x="deck",
data=titanic,
palette="Greens_d")

Show count of observations

>>> sns.pointplot(x="class",
y="survived",
hue="sex",
data=titanic,
palette={"male":"g",
"female":"m"},
markers=["^","o"],
linestyles=["-","--"])

Show point estimates and
confidence intervals as
rectangular bars

>>> sns.boxplot(x="alive",
y="age",
hue="adult_male",
data=titanic)
>>> sns.boxplot(data=iris,orient="h")

Boxplot

>>> sns.violinplot(x="age",
y="sex",
hue="survived",
data=titanic)

Violin plot

Categorical scatterplot with
non-overlapping points

Bar Chart

Count Plot

>>> sns.regplot(x="sepal_width",
y="sepal_length",
data=iris,
ax=ax)

Plot data and a linear regression
model fit

>>> plot = sns.distplot(data.y,
kde=False,
color="b")

Plot univariate distribution

Distribution Plots
Matrix Plots

>>> sns.heatmap(uniform_data,vmin=0,vmax=1)

4

Boxplot

Boxplot with wide-form data

Violinplot

(Re)set the seaborn default
Set the matplotlib parameters
Set the matplotlib parameters
Return a dict of params or use with

Context Functions
>>> sns.set_context("talk")
>>> sns.set_context("notebook",
font_scale=1.5,

Set context to "talk"
Set context to "notebook",
Scale font elements and
rc={"lines.linewidth":2.5}) override param mapping

Color Palette
>>>
>>>
>>>
>>>

Further Customizations

Heatmap

Also see Matplotlib

Axisgrid Objects

Also see Matplotlib

with to temporarily set the style

h = sns.PairGrid(iris)
Subplot grid for plotting pairwise
h = h.map(plt.scatter)
relationships
sns.pairplot(iris)
Plot pairwise bivariate distributions
i = sns.JointGrid(x="x",
Grid for bivariate plot with marginal
y="y",
univariate plots
data=data)
>>> i = i.plot(sns.regplot,
sns.distplot)
>>> sns.jointplot("sepal_length", Plot bivariate distribution
"sepal_width",
data=iris,
kind='kde')

Point Plot

Seaborn styles

>>> sns.axes_style("whitegrid")

Plot data and regression model fits
across a FacetGrid

>>> sns.stripplot(x="species",
y="petal_length",
data=iris)
>>> sns.swarmplot(x="species",
y="petal_length",
data=iris)

>>> f, ax = plt.subplots(figsize=(5,6)) Create a figure and one subplot

{"xtick.major.size":8,
"ytick.major.size":8})

Draw a categorical plot onto a
Facetgrid

>>>
>>>
>>>
>>>

Regression Plots

Figure Aesthetics

>>> sns.set()
>>> sns.set_style("whitegrid")
>>> sns.set_style("ticks",

Subplot grid for plotting conditional
relationships

Categorical Plots

Make use of the following aliases to import the libraries:
>>> import matplotlib.pyplot as plt
>>> import seaborn as sns

Plotting With Seaborn

sns.set_palette("husl",3)
Define the color palette
sns.color_palette("husl")
Use with with to temporarily set palette
flatui = ["#9b59b6","#3498db","#95a5a6","#e74c3c","#34495e","#2ecc71"]
sns.set_palette(flatui)
Set your own color palette

>>>
>>>
>>>
>>>

g.despine(left=True)
g.set_ylabels("Survived")
g.set_xticklabels(rotation=45)
g.set_axis_labels("Survived",
"Sex")
>>> h.set(xlim=(0,5),
ylim=(0,5),
xticks=[0,2.5,5],
yticks=[0,2.5,5])

Remove left spine
Set the labels of the y-axis
Set the tick labels for x
Set the axis labels
Set the limit and ticks of the
x-and y-axis

Plot
>>>
>>>
>>>
>>>
>>>
>>>
>>>

5

plt.title("A Title")
plt.ylabel("Survived")
plt.xlabel("Sex")
plt.ylim(0,100)
plt.xlim(0,10)
plt.setp(ax,yticks=[0,5])
plt.tight_layout()

Add plot title
Adjust the label of the y-axis
Adjust the label of the x-axis
Adjust the limits of the y-axis
Adjust the limits of the x-axis
Adjust a plot property
Adjust subplot params

Show or Save Plot
>>> plt.show()
>>> plt.savefig("foo.png")
>>> plt.savefig("foo.png",
transparent=True)

Also see Matplotlib
Show the plot
Save the plot as a figure
Save transparent figure

Close & Clear

Also see Matplotlib

>>> plt.cla()
>>> plt.clf()
>>> plt.close()

Clear an axis
Clear an entire figure
Close a window

DataCamp

Learn Python for Data Science Interactively



Source Exif Data:
File Type                       : PDF
File Type Extension             : pdf
MIME Type                       : application/pdf
PDF Version                     : 1.6
Linearized                      : Yes
Language                        : en-US
Tagged PDF                      : Yes
XMP Toolkit                     : Adobe XMP Core 5.6-c123 79.158978, 2016/02/13-01:11:19
Create Date                     : 2017:08:29 10:40:55-04:00
Metadata Date                   : 2017:08:29 10:40:56-04:00
Modify Date                     : 2017:08:29 10:40:56-04:00
Creator Tool                    : Adobe InDesign CC 2015 (Macintosh)
Instance ID                     : uuid:2d5b36ef-183c-af41-a3b4-4aa96a168beb
Original Document ID            : xmp.did:6e99caaf-3078-794a-b789-6191a95ace2d
Document ID                     : xmp.id:4974a08e-a757-406d-b9f3-0e6efc6cdcc3
Rendition Class                 : proof:pdf
Version ID                      : 1
Derived From Instance ID        : xmp.iid:386c2fbd-6dfa-4feb-a64b-06cc2ae158b3
Derived From Document ID        : xmp.did:986116fb-901e-4c6d-aefc-b91d70a533b6
Derived From Original Document ID: xmp.did:6e99caaf-3078-794a-b789-6191a95ace2d
Derived From Rendition Class    : default
History Action                  : converted
History Parameters              : from application/x-indesign to application/pdf
History Software Agent          : Adobe InDesign CC 2015 (Macintosh)
History Changed                 : /
History When                    : 2017:08:29 10:40:55-04:00
Format                          : application/pdf
Title                           : Cheat sheet Seaborn.indd
Producer                        : Adobe PDF Library 15.0
Trapped                         : False
GTS PDFX Version                : PDF/X-4
Page Count                      : 1
Creator                         : Adobe InDesign CC 2015 (Macintosh)
EXIF Metadata provided by EXIF.tools

Navigation menu