Seaborn Cheat Sheet: Plot Functions and Common Commands

A one-page Seaborn cheat sheet to download, with code for twelve plot functions and the commands for figure-level plots, themes, palettes, legends, layout, and saving.

Most Seaborn charts are one call: pass a DataFrame as data= and name its columns, as in sns.scatterplot(data=df, x="temp", y="sales", hue="cafe"). Functions like histplot and boxplot draw on a Matplotlib Axes, while displot, catplot, and relplot build a whole figure of facets. This cheat sheet collects both kinds on one page, with copyable tables below.

Quick Example

A scatter plot with a color per group, a theme, labels, and the legend moved outside. The code needs only the imports and the data it creates.

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

rng = np.random.default_rng(21)
cafe = rng.choice(["North", "South", "East"], 240)
df = pd.DataFrame({"cafe": cafe, "temp": rng.normal(18, 6, 240)})
df["sales"] = 40 + 3 * df["temp"] + 25 * (cafe == "North") + rng.normal(0, 12, 240)

sns.set_theme(style="whitegrid")
fig, ax = plt.subplots(figsize=(8, 4.5), layout="constrained")
sns.scatterplot(data=df, x="temp", y="sales", hue="cafe", ax=ax)
ax.set(title="Daily sales vs temperature", xlabel="Temperature (C)", ylabel="Sales")
sns.move_legend(ax, "upper left", bbox_to_anchor=(1, 1))
plt.show()

Seaborn scatter plot of sales against temperature with points colored by cafe, a whitegrid theme and the legend outside the plot

Sales rise with temperature (a correlation of 0.73 in this data), and the North cafe averages 117 against 93 for East and 98 for South, so its points sit higher.

Download the Seaborn Cheat Sheet

Download the full-size cheat sheet (PNG). Every chart on it is drawn by the code printed beside it, with Seaborn 0.13.

One page Seaborn cheat sheet with setup code, twelve plot functions each shown with its code, and reference panels for figure-level functions, themes and colors, labels and legends, and layout and saving

Setup Code for the Examples

The snippets in the tables below use these variables. The data is made up (seeded, so you get the same numbers): 240 days at three cafes.

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

rng = np.random.default_rng(8)
cafe = rng.choice(["North", "South", "East"], 240)
df = pd.DataFrame({"cafe": cafe,
                   "temp": rng.normal(18, 6, 240),
                   "week": rng.integers(1, 13, 240)})
df["sales"] = (40 + 3 * df["temp"]
               + 25 * (cafe == "North")
               + rng.normal(0, 12, 240))
df["visits"] = df["sales"] / 4 + rng.normal(0, 5, 240)
sns.set_theme()
fig, ax = plt.subplots()

Seaborn Plot Types

These are axes-level functions: each draws on ax (or the current Axes) and returns it. Add ax= to choose where.

Chart (links to guide) Code
Histogram sns.histplot(data=df, x="sales", bins=20)
Density sns.kdeplot(data=df, x="sales", hue="cafe")
Scatter sns.scatterplot(data=df, x="temp", y="sales", hue="cafe")
Line with confidence band sns.lineplot(data=df, x="week", y="sales")
Bar (mean) sns.barplot(data=df, x="cafe", y="sales")
Count sns.countplot(data=df, x="cafe")
Box sns.boxplot(data=df, x="cafe", y="sales")
Violin sns.violinplot(data=df, x="cafe", y="sales")
Strip (jittered points) sns.stripplot(data=df, x="cafe", y="sales")
Regression sns.regplot(data=df, x="temp", y="sales")
Heatmap sns.heatmap(df.corr(numeric_only=True), annot=True, fmt=".2f")
ECDF sns.ecdfplot(data=df, x="sales", hue="cafe")

barplot shows the mean with a bootstrapped 95% confidence interval by default, and lineplot does the same for each x value. Use errorbar="sd" for the standard deviation instead.

Figure-Level Functions: displot, catplot, relplot

Figure-level functions create their own figure, split it by col= or row=, and return a FacetGrid (or PairGrid / JointGrid). They don't take ax=.

Task Code
Distribution per group g = sns.displot(data=df, x="sales", col="cafe")
Category plot per group g = sns.catplot(data=df, x="cafe", y="sales", kind="box")
Relationship per group g = sns.relplot(data=df, x="temp", y="sales", col="cafe")
Regression per group g = sns.lmplot(data=df, x="temp", y="sales", hue="cafe")
Every pair of columns g = sns.pairplot(df, hue="cafe")
Scatter plus marginals g = sns.jointplot(data=df, x="temp", y="sales")

catplot takes kind="strip", "swarm", "box", "violin", "boxen", "point", "bar", or "count". Which family a function belongs to is covered in figure-level vs axes-level, and the extras in pairplot and jointplot.

Themes and Colors

Task Code
Theme sns.set_theme(style="whitegrid")
Size preset sns.set_context("talk")
Default palette sns.set_palette("Set2")
Palette for one plot sns.scatterplot(data=df, x="temp", y="sales", hue="cafe", palette="Set2")
List of colors colors = sns.color_palette("viridis", 4)
Remove top and right spines sns.despine()

style takes "darkgrid" (the default), "whitegrid", "dark", "white", or "ticks". set_context takes "paper", "notebook" (the default), "talk", or "poster", and scales fonts and line widths. See themes and custom color palettes.

Labels, Category Order, and Legends

Task Code
Axis labels ax.set(xlabel="Temp (C)", ylabel="Sales")
Title ax.set_title("Title")
Rotate tick labels ax.tick_params(axis="x", rotation=45)
Category order sns.boxplot(data=df, x="cafe", y="sales", order=["South", "North", "East"])
Move the legend sns.move_legend(ax, "upper left", bbox_to_anchor=(1, 1))
Facet titles g.set_titles("{col_name}")

On a figure-level plot, call these on the returned grid: g.set_axis_labels("Sales", "Count"), g.set_titles(...), g.figure.suptitle(...). More in category order, move_legend, and FacetGrid titles.

Layout, Size, and Saving

Task Code
Several plots in one figure fig, axs = plt.subplots(1, 2)
sns.histplot(data=df, x="sales", ax=axs[0])
Size (axes-level) fig, ax = plt.subplots(figsize=(8, 5))
Size (figure-level) sns.displot(data=df, x="sales", height=3, aspect=1.5)
Standard deviation bars sns.lineplot(data=df, x="week", y="sales", errorbar="sd")
Save (axes-level) plt.savefig("plot.png", dpi=200, bbox_inches="tight")
Save (figure-level) g.savefig("plot.png", dpi=200)

For axes-level functions you set the size on the Matplotlib figure. For figure-level ones, height is per facet and aspect is width divided by height, so a col= grid of three is about three times as wide as height * aspect. See subplots and saving high-resolution figures.

Which Seaborn Function Should I Use?

You have Use
One numeric column histplot, kdeplot, ecdfplot
A number per category boxplot, violinplot, stripplot, barplot
Counts of categories countplot
Two numeric columns scatterplot, regplot, lineplot (if x is ordered)
A matrix or correlations heatmap, clustermap
Any of the above split by a column the figure-level version: displot, catplot, relplot

Practical Tips

  • Pass data= and column names, not arrays. Seaborn then labels the axes for you.
  • hue needs a column, and in 0.13 a palette given without hue is deprecated.
  • Axes-level functions accept ax=; figure-level ones don't. If an ax= call fails with TypeError, you probably have a figure-level function.
  • sns.set_theme() changes Matplotlib's global defaults, so call it once at the top of a script.
  • df.corr() needs numeric_only=True when the DataFrame has text columns.

Similar Topics