Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Visualizations With Seaborn

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

load_dataset()

Seaborn comes with a handy way to quickly get some datasets to play with, but please note this is NOT the normal way of loading a CSV file. Usually we’d use pandas.read_csv() as we’ve seen so far.

tips = sns.load_dataset("tips")
tips
Loading...
penguins = sns.load_dataset("penguins")

Seaborn Scatterplots

sns.set_theme()
sns.scatterplot(data=tips, x="total_bill", y="tip")
<Axes: xlabel='total_bill', ylabel='tip'>
<Figure size 640x480 with 1 Axes>
tips.plot(kind="scatter", x="total_bill", y="tip")
<Axes: xlabel='total_bill', ylabel='tip'>
<Figure size 640x480 with 1 Axes>
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="smoker")
<Axes: xlabel='total_bill', ylabel='tip'>
<Figure size 640x480 with 1 Axes>
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="sex")
<Axes: xlabel='total_bill', ylabel='tip'>
<Figure size 640x480 with 1 Axes>
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="day")
<Axes: xlabel='total_bill', ylabel='tip'>
<Figure size 640x480 with 1 Axes>
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="sex", style="time")
<Axes: xlabel='total_bill', ylabel='tip'>
<Figure size 640x480 with 1 Axes>
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="sex", style="sex")
<Axes: xlabel='total_bill', ylabel='tip'>
<Figure size 640x480 with 1 Axes>
sns.scatterplot(data=tips, x="total_bill", y="tip", size="size")
<Axes: xlabel='total_bill', ylabel='tip'>
<Figure size 640x480 with 1 Axes>
sns.scatterplot(data=tips, x="total_bill", y="tip", size="size", hue="sex")
<Axes: xlabel='total_bill', ylabel='tip'>
<Figure size 640x480 with 1 Axes>

Seaborn Line Plots

flights = sns.load_dataset("flights")
sns.lineplot(data=flights, x="year", y="passengers")
<Axes: xlabel='year', ylabel='passengers'>
<Figure size 640x480 with 1 Axes>
flights.groupby("year")["passengers"].mean().plot()
<Axes: xlabel='year'>
<Figure size 640x480 with 1 Axes>
sns.lineplot(data=flights, x="year", y="passengers", estimator="sum")
<Axes: xlabel='year', ylabel='passengers'>
<Figure size 640x480 with 1 Axes>
sns.lineplot(data=flights, x="year", y="passengers", hue="month")
<Axes: xlabel='year', ylabel='passengers'>
<Figure size 640x480 with 1 Axes>
trips= sns.load_dataset("taxis", parse_dates=["pickup", "dropoff"])
trips["hour"] = trips["pickup"].dt.hour
trips
Loading...
sns.lineplot(data=trips, x="hour", y="total", hue="payment", style="color", ci=None)
/tmp/ipykernel_51313/3291997563.py:1: FutureWarning: 

The `ci` parameter is deprecated. Use `errorbar=None` for the same effect.

  sns.lineplot(data=trips, x="hour", y="total", hue="payment", style="color", ci=None)
<Axes: xlabel='hour', ylabel='total'>
<Figure size 640x480 with 1 Axes>

The Figure-Level Relplot( ) Method

tips
Loading...
sns.relplot(data=tips, x="total_bill", y="tip", kind="scatter", col="sex")
<seaborn.axisgrid.FacetGrid at 0x7fd9209eda90>
<Figure size 1000x500 with 2 Axes>
sns.relplot(data=tips, x="total_bill", y="tip", kind="scatter", hue="smoker", col="sex")
<seaborn.axisgrid.FacetGrid at 0x7fd91e772c10>
<Figure size 1080x500 with 2 Axes>
sns.relplot(data=tips, x="total_bill", y="tip", kind="scatter", hue="smoker", col="sex", row="time")
<seaborn.axisgrid.FacetGrid at 0x7fd91e4942d0>
<Figure size 1080x1000 with 4 Axes>
sns.relplot(data=trips, x="hour", y="total", kind="line", col="pickup_borough", hue="payment")
<seaborn.axisgrid.FacetGrid at 0x7fd91e432350>
<Figure size 2126.25x500 with 4 Axes>
sns.relplot(
    data=trips, 
    x="hour",
    y="total", 
    kind="line", 
    col="pickup_borough", 
    hue="payment", 
    row="dropoff_borough"
)
<seaborn.axisgrid.FacetGrid at 0x7fd91e047110>
<Figure size 2126.25x2500 with 20 Axes>

Changing Plot Sizes

plt.figure(figsize=(8,5))
sns.lineplot(data=flights, x="year", y="passengers", hue="month")
<Axes: xlabel='year', ylabel='passengers'>
<Figure size 800x500 with 1 Axes>
sns.relplot(
    data=tips, 
    x="total_bill", 
    y="tip", 
    kind="scatter", 
    hue="smoker", 
    col="day",
    row="sex",
    height=2,
    aspect=2
)
<seaborn.axisgrid.FacetGrid at 0x7fd91cc7afd0>
<Figure size 1680x400 with 8 Axes>

Seaborn Histograms

sns.histplot(data=tips, x="tip")
<Axes: xlabel='tip', ylabel='Count'>
<Figure size 640x480 with 1 Axes>
sns.histplot(data=tips, x="tip", hue="time")
<Axes: xlabel='tip', ylabel='Count'>
<Figure size 640x480 with 1 Axes>
sns.histplot(data=tips, x="tip", hue="smoker", multiple="stack")
<Axes: xlabel='tip', ylabel='Count'>
<Figure size 640x480 with 1 Axes>
sns.histplot(data=tips, x="tip", hue="smoker", multiple="dodge")
<Axes: xlabel='tip', ylabel='Count'>
<Figure size 640x480 with 1 Axes>
sns.histplot(data=penguins, x="body_mass_g", binwidth=500)
<Axes: xlabel='body_mass_g', ylabel='Count'>
<Figure size 640x480 with 1 Axes>
sns.histplot(data=penguins, x="body_mass_g", bins=20)
<Axes: xlabel='body_mass_g', ylabel='Count'>
<Figure size 640x480 with 1 Axes>
sns.histplot(data=penguins, x="body_mass_g", bins=20, hue="species", multiple="stack")
<Axes: xlabel='body_mass_g', ylabel='Count'>
<Figure size 640x480 with 1 Axes>
sns.histplot(data=penguins, x="body_mass_g", bins=20, hue="species", multiple="stack", element="step")
<Axes: xlabel='body_mass_g', ylabel='Count'>
<Figure size 640x480 with 1 Axes>
sns.histplot(data=tips, x="tip", kde=True)
<Axes: xlabel='tip', ylabel='Count'>
<Figure size 640x480 with 1 Axes>
sns.set_theme()
plt.figure(dpi=200)
sns.histplot(
    data=penguins, 
    x="body_mass_g", 
    hue="species", 
    multiple="stack", 
    palette="muted",
    bins=20,
    kde=True
)
plt.title("Penguin Body Mass")
<Figure size 1280x960 with 1 Axes>
sns.set_theme()
plt.figure(dpi=200)
sns.histplot(
    data=penguins, 
    x="body_mass_g", 
    bins=20, 
    hue="species", 
    multiple="stack", 
    kde=True
)
plt.title("Penguin Body Mass")
<Figure size 1280x960 with 1 Axes>

Seaborn KDE Plots

sns.kdeplot(data=penguins, x="body_mass_g")
<Axes: xlabel='body_mass_g', ylabel='Density'>
<Figure size 640x480 with 1 Axes>
sns.kdeplot(data=penguins, x="body_mass_g", hue="species")
<Axes: xlabel='body_mass_g', ylabel='Density'>
<Figure size 640x480 with 1 Axes>
sns.kdeplot(data=penguins, x="body_mass_g", hue="species", bw_adjust=0.4)
<Axes: xlabel='body_mass_g', ylabel='Density'>
<Figure size 640x480 with 1 Axes>
sns.kdeplot(data=penguins, x="body_mass_g", hue="species", multiple="stack")
<Axes: xlabel='body_mass_g', ylabel='Density'>
<Figure size 640x480 with 1 Axes>
penguins
Loading...
sns.histplot(data=penguins, x="flipper_length_mm")
<Axes: xlabel='flipper_length_mm', ylabel='Count'>
<Figure size 640x480 with 1 Axes>
sns.histplot(data=penguins, x="body_mass_g")
<Axes: xlabel='body_mass_g', ylabel='Count'>
<Figure size 640x480 with 1 Axes>
sns.histplot(data=penguins, x="body_mass_g", y="flipper_length_mm")
<Axes: xlabel='body_mass_g', ylabel='flipper_length_mm'>
<Figure size 640x480 with 1 Axes>
sns.kdeplot(data=penguins, x="body_mass_g", y="flipper_length_mm")
<Axes: xlabel='body_mass_g', ylabel='flipper_length_mm'>
<Figure size 640x480 with 1 Axes>
sns.kdeplot(data=penguins, x="bill_length_mm", y="flipper_length_mm", hue="species")
<Axes: xlabel='bill_length_mm', ylabel='flipper_length_mm'>
<Figure size 640x480 with 1 Axes>
sns.histplot(data=penguins, x="bill_length_mm", y="flipper_length_mm", hue="species")
<Axes: xlabel='bill_length_mm', ylabel='flipper_length_mm'>
<Figure size 640x480 with 1 Axes>

Seaborn Rugplots

sns.rugplot(data=tips, x="tip", height=0.2)
<Axes: xlabel='tip'>
<Figure size 640x480 with 1 Axes>
sns.rugplot(data=tips, y="tip", height=0.2)
<Axes: ylabel='tip'>
<Figure size 640x480 with 1 Axes>
sns.kdeplot(data=tips, x="total_bill")
sns.rugplot(data=tips, x="total_bill", height=0.07)
<Axes: xlabel='total_bill', ylabel='Density'>
<Figure size 640x480 with 1 Axes>
sns.scatterplot(data=tips, x="total_bill", y="tip")
sns.rugplot(data=tips,x="total_bill", y="tip")
<Axes: xlabel='total_bill', ylabel='tip'>
<Figure size 640x480 with 1 Axes>
diamonds = sns.load_dataset("diamonds")
sns.scatterplot(data=diamonds, x="carat", y="price", s=5)
sns.rugplot(data=diamonds, x="carat", y="price", lw=1, alpha=.005)
<Axes: xlabel='carat', ylabel='price'>
<Figure size 640x480 with 1 Axes>

The Figure-Level hisplot( ) Method

sns.displot(kind="hist", data=penguins, x="body_mass_g", height=3, aspect=2)
<seaborn.axisgrid.FacetGrid at 0x7fd915d7d6d0>
<Figure size 600x300 with 1 Axes>
sns.displot(
    kind="hist", 
    data=penguins, 
    hue="sex",
    x="body_mass_g", 
    col="species"
)
<seaborn.axisgrid.FacetGrid at 0x7fd91015c190>
<Figure size 1606x500 with 3 Axes>
sns.displot(data=tips, kind="kde", x="tip", col="time", rug=True)
<seaborn.axisgrid.FacetGrid at 0x7fd90be32c10>
<Figure size 1000x500 with 2 Axes>
sns.displot(data=tips, kind="kde", x="total_bill", y="tip", rug=True)
<seaborn.axisgrid.FacetGrid at 0x7fd90bd3ead0>
<Figure size 500x500 with 1 Axes>

Seaborn Countplots

sns.countplot(data=penguins, x="species")
<Axes: xlabel='species', ylabel='count'>
<Figure size 640x480 with 1 Axes>
sns.countplot(data=penguins, x="species", hue="sex")
<Axes: xlabel='species', ylabel='count'>
<Figure size 640x480 with 1 Axes>
titanic = pd.read_csv("data/titanic.csv")
sns.countplot(data=titanic, x="pclass", hue="sex")
<Axes: xlabel='pclass', ylabel='count'>
<Figure size 640x480 with 1 Axes>
sns.countplot(data=titanic, y="pclass", hue="sex")
<Axes: xlabel='count', ylabel='pclass'>
<Figure size 640x480 with 1 Axes>

Seaborn Stripplot & Swarmplot

sns.scatterplot(data=trips, x="pickup_borough", y="distance")
<Axes: xlabel='pickup_borough', ylabel='distance'>
<Figure size 640x480 with 1 Axes>
plt.figure(dpi=100)
sns.stripplot(data=trips, x="pickup_borough", y="distance")
plt.title("Taxi Trip Distance By Burough")
<Figure size 640x480 with 1 Axes>
trips_sample = trips.nlargest(600, "total")
plt.figure(figsize=(12,5))
sns.swarmplot(data=trips_sample, x="pickup_borough", y="distance")
<Axes: xlabel='pickup_borough', ylabel='distance'>
<Figure size 1200x500 with 1 Axes>
plt.figure(figsize=(12,5))
sns.stripplot(data=trips_sample, x="pickup_borough", y="distance")
plt.title("Taxi Trips By Borough")
<Figure size 1200x500 with 1 Axes>
titanic = sns.load_dataset("titanic")
plt.figure(figsize=(12,5))

sns.swarmplot(data=titanic, x="pclass", y="age", hue="sex")
<Axes: xlabel='pclass', ylabel='age'>
<Figure size 1200x500 with 1 Axes>

Seaborn Boxplots

sns.boxplot(data=titanic, x="age")
<Axes: xlabel='age'>
<Figure size 640x480 with 1 Axes>
sns.boxplot(data=trips, x="pickup_borough", y="total")
<Axes: xlabel='pickup_borough', ylabel='total'>
<Figure size 640x480 with 1 Axes>
sns.boxplot(data=trips, x="pickup_borough", y="total", whis=2.5, fliersize=2)
<Axes: xlabel='pickup_borough', ylabel='total'>
<Figure size 640x480 with 1 Axes>
sns.boxplot(data=titanic, x="pclass", y="age", hue="sex", fliersize=5)
<Axes: xlabel='pclass', ylabel='age'>
<Figure size 640x480 with 1 Axes>
sns.boxplot(data=penguins, x="species", y="body_mass_g")
sns.swarmplot(data=penguins, x="species", y="body_mass_g", color="0.3")
<Axes: xlabel='species', ylabel='body_mass_g'>
<Figure size 640x480 with 1 Axes>

Seaborn Boxenplots

sns.boxplot(data=trips, x="pickup_borough", y="total")
<Axes: xlabel='pickup_borough', ylabel='total'>
<Figure size 640x480 with 1 Axes>
plt.figure(figsize=(10,6))
sns.boxenplot(data=trips, x="pickup_borough", y="total")
<Axes: xlabel='pickup_borough', ylabel='total'>
<Figure size 1000x600 with 1 Axes>

Seaborn Violinplots

sns.violinplot(data=titanic, x="age")
# sns.boxplot(data=titanic, x="age")
<Axes: xlabel='age'>
<Figure size 640x480 with 1 Axes>
sns.violinplot(data=titanic, x="pclass", y="age")
<Axes: xlabel='pclass', ylabel='age'>
<Figure size 640x480 with 1 Axes>
sns.violinplot(data=titanic, x="pclass", y="age", hue="sex")
<Axes: xlabel='pclass', ylabel='age'>
<Figure size 640x480 with 1 Axes>
plt.figure(figsize=(10,4))
sns.violinplot(data=titanic, x="pclass", y="age", hue="sex", split=True, palette="muted")
<Axes: xlabel='pclass', ylabel='age'>
<Figure size 1000x400 with 1 Axes>

Seaborn Barplots

sns.barplot(data=trips, x="pickup_borough", y="distance")
<Axes: xlabel='pickup_borough', ylabel='distance'>
<Figure size 640x480 with 1 Axes>
sns.barplot(data=trips, x="pickup_borough", y="total")
<Axes: xlabel='pickup_borough', ylabel='total'>
<Figure size 640x480 with 1 Axes>
sns.barplot(data=trips, x="pickup_borough", y="total", estimator=sum)
<Axes: xlabel='pickup_borough', ylabel='total'>
<Figure size 640x480 with 1 Axes>
trips.groupby("pickup_borough")["total"].sum().plot(kind="bar")
<Axes: xlabel='pickup_borough'>
<Figure size 640x480 with 1 Axes>
sns.barplot(data=trips, x="pickup_borough", y="distance", hue="color")
<Axes: xlabel='pickup_borough', ylabel='distance'>
<Figure size 640x480 with 1 Axes>
sns.barplot(data=trips, y="pickup_borough", x="distance", hue="color", dodge=False)
<Axes: xlabel='distance', ylabel='pickup_borough'>
<Figure size 640x480 with 1 Axes>
sns.barplot(data=titanic, y="pclass", x="survived", orient="h")
<Axes: xlabel='survived', ylabel='pclass'>
<Figure size 640x480 with 1 Axes>

The Figure-Level catplot( ) Method

sns.catplot(data=titanic, x="sex", y="survived", kind="bar")
<seaborn.axisgrid.FacetGrid at 0x7fd90afe3390>
<Figure size 512.222x500 with 1 Axes>
sns.catplot(data=titanic, x="sex", y="survived", kind="bar", col="pclass")
<seaborn.axisgrid.FacetGrid at 0x7fd90ae656d0>
<Figure size 1512.22x500 with 3 Axes>
sns.catplot(data=titanic, x="pclass", y="survived", kind="bar", hue="sex")
<seaborn.axisgrid.FacetGrid at 0x7fd90ad7bc50>
<Figure size 601.125x500 with 1 Axes>
sns.catplot(
    data=trips, 
    kind="strip", 
    x="pickup_borough", 
    y="distance", 
    col="color",
    aspect=0.8
)
<seaborn.axisgrid.FacetGrid at 0x7fd90ac0b250>
<Figure size 812.222x500 with 2 Axes>
sns.catplot(data=trips, kind="violin", x="pickup_borough", y="distance", hue="color", split=True)
<seaborn.axisgrid.FacetGrid at 0x7fd90ab134d0>
<Figure size 597.625x500 with 1 Axes>
sns.catplot(
    data=titanic, 
    kind="bar",
    x="who",
    y="survived",
    col="pclass",
    ci=None,
)
/tmp/ipykernel_51313/1873499506.py:1: FutureWarning: 

The `ci` parameter is deprecated. Use `errorbar=None` for the same effect.

  sns.catplot(
<seaborn.axisgrid.FacetGrid at 0x7fd90abf4550>
<Figure size 1512.22x500 with 3 Axes>