Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 15

Themes, Contexts, and Palettes

Change the whole look of your charts with sns.set_theme styles and contexts, and choose qualitative, sequential, or diverging colour palettes that fit the data.

Intermediate25 min
Themes, Contexts, and Palettes hero Module 26 Lesson 15Themes, Contexts, and Palettes hero Module 26 Lesson 15

Seaborn's look comes from three settings. The style controls the background, grid, and spines. The context scales fonts and lines for where the chart will be seen, from a paper to a poster. The palette picks the colours. sns.set_theme sets all three at once.

Five rows of colour swatches for the deep, colorblind, rocket, mako, and vlag palettesFive rows of colour swatches for the deep, colorblind, rocket, mako, and vlag palettes

Five styles

style is one of "darkgrid", "whitegrid", "dark", "white", or "ticks". sns.axes_style used in a with block applies a style to the charts inside it only.

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

rng = np.random.default_rng(26)
study = pd.DataFrame({
    "track": rng.choice(["Python", "Data", "Web"], 120),
    "mode": rng.choice(["online", "classroom"], 120),
    "week": rng.integers(1, 7, 120),
})
study["hours"] = (rng.uniform(1, 6, 120) + study["week"] * 0.8).round(1)
study["score"] = (35 + study["hours"] * 4.5 + rng.normal(0, 7, 120)).clip(0, 100).round()

styles = ["darkgrid", "whitegrid", "dark", "white", "ticks"]
fig = plt.figure(figsize=(14, 3))
for index, name in enumerate(styles, start=1):
    with sns.axes_style(name):
        ax = fig.add_subplot(1, 5, index)
        sns.histplot(data=study, x="score", binwidth=5, ax=ax)
        ax.set_title(name)
plt.tight_layout()
plt.show()

print(sns.axes_style("darkgrid")["axes.facecolor"])

Output:

text
#EAEAF2

Contexts scale everything

context is "paper", "notebook" (the default), "talk", or "poster". It makes text and lines larger without changing the figure's design, so a slide stays readable from the back of a room.

python
import seaborn as sns

for name in ["paper", "notebook", "talk", "poster"]:
    settings = sns.plotting_context(name)
    print(f"{name:<9} font {settings['font.size']:>5.1f}  lines {settings['lines.linewidth']:>5.2f}")

Output:

text
paper     font   9.6  lines  1.20
notebook  font  12.0  lines  1.50
talk      font  18.0  lines  2.25
poster    font  24.0  lines  3.00

Three kinds of palette

Qualitative palettes such as "deep" and "colorblind" give unrelated categories clearly different colours. Sequential palettes such as "rocket" and "mako" run light to dark for values that go one way. Diverging palettes such as "vlag" have two ends and a quiet middle for values around a centre.

python
import seaborn as sns

for name in ["colorblind", "rocket", "vlag"]:
    print(name, sns.color_palette(name, 4).as_hex())

Output:

text
colorblind ['#0173b2', '#de8f05', '#029e73', '#d55e00']
rocket ['#4c1d4b', '#a11a5b', '#e83f3f', '#f69c73']
vlag ['#87a0c4', '#d9dce5', '#ecd3d2', '#cc8582']

"colorblind" is a safe default for categories: its colours stay distinct for the most common kinds of colour blindness.

Your own colours

palette also accepts a list or a dictionary. A dictionary pins each category to a colour, so Python is always violet in every chart. sns.despine() removes the top and right lines for a cleaner frame.

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

rng = np.random.default_rng(26)
study = pd.DataFrame({
    "track": rng.choice(["Python", "Data", "Web"], 120),
    "mode": rng.choice(["online", "classroom"], 120),
    "week": rng.integers(1, 7, 120),
})
study["hours"] = (rng.uniform(1, 6, 120) + study["week"] * 0.8).round(1)
study["score"] = (35 + study["hours"] * 4.5 + rng.normal(0, 7, 120)).clip(0, 100).round()

sns.set_theme(style="ticks", context="talk")
brand = {"Python": "#7C3AED", "Data": "#2563EB", "Web": "#EC4899"}
sns.boxplot(data=study, x="track", y="score", hue="track", palette=brand, legend=False)
sns.despine()
plt.show()

print(brand["Python"])

Output:

text
#7C3AED

Key takeaways

  • style sets the background and grid: darkgrid, whitegrid, dark, white, ticks

  • context scales fonts and lines: paper, notebook, talk, poster

  • Qualitative for categories, sequential for one-way values, diverging around a centre

  • Pass a dict as palette to pin colours; sns.despine() cleans the frame

  • Next: Customising and Saving Seaborn Charts

  • Previous: Pair Plots and Joint Plots

Loading playground…

Check your understanding

Themes, Contexts, and Palettes Quiz

What three things does set_theme control?

1 / 10