Appendix E — Seaborn tutorial#

See outline here:
https://docs.google.com/document/d/1fwep23-95U-w1QMPU31nOvUnUXE2X3s_Dbk5JuLlKAY/edit#bookmark=id.3i7cktuf1u3i

In this tutorial, we’ll learn about Seaborn data visualizations. We’ll discuss Seaborn plot functions We’ll also describe the various options for customize plots’ the appearance, add annotations, and export plots as publication-quality images.

If you want to pursue a career in a data-related field, I highly recommend you get to know Seaborn by reading this tutorial and the other resources in the links section.

Seaborn overview#

Basic plots#

Line plot#

import seaborn as sns
import pandas as pd
days = [1, 2, 3, 4]
cakes = [2, 5, 3, 4]
sns.lineplot(x=days, y=cakes);
../_images/bde28be8021979fa3538547a822a43da8fc7fbe043b966114782a2749489c631.png
# # (optional) use Matplotlib axis methods to add labels
# ax = sns.lineplot(x=days, y=cakes)
# ax.set_xlabel("days")
# ax.set_ylabel("cakes")
df = pd.DataFrame({"days":days, "cakes":cakes})
df
days cakes
0 1 2
1 2 5
2 3 3
3 4 4
df.columns
Index(['days', 'cakes'], dtype='object')
sns.lineplot(x="days", y="cakes", data=df);
../_images/fb39abeb3c7d0e4a8c2974f8fdb017741557c19a3069b9cd024919094a6fb8f0.png
# # ALT. hybrid approach
# sns.lineplot(x=df["days"], y=df["cakes"])

Plotting function graphs#

def g(x):
    return 0.5 * x**2
import numpy as np
xs = np.linspace(0, 10, 1000)
gxs = g(xs)
sns.lineplot(x=xs, y=gxs, label="Graph of g(x)");
../_images/900cb6e223cb4451bd70ce4e7e87969d12aa5d50debe24e1c1ba5475a2fa7f9a.png
# # FIGURES ONLY
# from ministats.utils import savefigure
# ax = sns.lineplot(x=xs, y=gxs, label="Graph of g(x)");
# filename = "figures/tutorials/seaborn/graph_of_function_g_eq_halfx2.pdf"
# savefigure(ax, filename)

Distribution plots#

Strip plots#

Scatter plots#

Density plots#

Histograms#

Box plots#

Violin plots#

Categorical plots#

Bar plots#

Linear model plots#

Linear model plots using seaborn#

Linear model plots from scratch#

Linear model plots using statsmodels#

Other plots#

Stem plot for discrete random variables#

Customizing plots#

Bonus topics#

Data visualization tips#