Back to Cheatsheets

seaborn

Table of Contents

1. ABOUT

2. FORMATTING

3. BARPLOT

4. DISTRIBUTIONS

ABOUT

Import

import seaborn as sns

Show

plt.show()

Setup

f, ax = plt.subplots(figsize=(width,height))

FORMATTING

Labels

ax.set_title() : same as plt.title()

Modify Ticks

import matplotlib.ticker as mtick
fmt = '${x:,.0f}'
tick = mtick.StrMethodFormatter(fmt)
ax.yaxis.set_major_formatter(tick)

Style

sns.set_style('style')` 

Despine

sns.despine()

Scale

sns.set_context('scale_type', font_scale=text_size, rc={"grid.linewidth": 5})

Palettes

sns.set_palette('palette_name')
Palette When to use About
qualitative distinct but non-ordered categories set of distinct olours, easy to distinguish categories, no particular ordering/meaning
sequential a variable exists as ordered categories , or as continuous values that can be put into groups move sequentially from a lighter to a darker color, only when high values need to be emphasised
diverging both the low and high values might be of equal interest dark to light to dark, dark more important as attract attention

BARPLOT

sns.barplot(data=df, x='x_col_name', y='y_col_name')

Error Bars

sns.barplot(...., ci='error_measure')

Aggregate

sns.barplot(...., estimator=aggregate_func)

Aggregate by multiple

hue='other_x_col_name'

DISTRIBUTIIONS

KDE plots

sns.kdeplot(dataset1, shade=True)
sns.kdeplot(dataset2, shade=True)

Box plots

sns.boxplot(data=df, x='df_x_col', y='df_y_col')

Violin plots

sns.violinplot(data=df, x='df_x_col', y='df_y_col, hue='3rd_variable', split=True)

STRIP PLOTS

sns.stripplot(x='x', y='y', data=df)