Matplotlib and seaborn 3 cheatsheet

Matplotlib and seaborn 3: seaborn

8 common Matplotlib and seaborn 3 tasks.

Set a theme

Apply seaborn's default look to all charts.

import seaborn as sns

sns.set_theme(style="whitegrid")

Histogram with density

A histogram with a smooth density curve on top.

df = sns.load_dataset("penguins")
sns.histplot(data=df, x="flipper_length_mm", kde=True)

Scatter plot by group

Color points by category with hue.

sns.scatterplot(data=df, x="bill_length_mm", y="bill_depth_mm", hue="species")

Box plot by category

Compare a variable across groups.

sns.boxplot(data=df, x="species", y="body_mass_g")

Count categories

Show how many rows fall in each category.

sns.countplot(data=df, x="island", hue="species")

Correlation heatmap

Show how numeric columns relate, with values written in the cells.

sns.heatmap(df.select_dtypes("number").corr(), annot=True, cmap="coolwarm", vmin=-1, vmax=1)

Plot every pair of variables

A grid of scatter plots and histograms for exploring a dataset.

sns.pairplot(df, hue="species")

Line chart with confidence interval

Plot a trend with an interval around the mean.

tips = sns.load_dataset("flights")
sns.lineplot(data=tips, x="year", y="passengers")

Was this page helpful?