Themes and context

set_theme, set_style, set_context, and palettes.

sns.set_theme() sets paper-friendly defaults: a white grid, a colorblind-friendly palette, and readable fonts. The workbench already called it once at startup.

Goal

Switch style, context, and palette, then see how later plots pick up the change.

Style

df = pd.DataFrame(
    {
        "city": ["Nairobi", "Mombasa", "Kisumu"],
        "units": [19, 13, 14],
    }
)
sns.set_style("whitegrid")
sns.barplot(data=df, x="city", y="units")
plt.title("whitegrid")
plt.show()

Other styles: "darkgrid", "white", "dark", "ticks".

Context

df = pd.DataFrame(
    {
        "city": ["Nairobi", "Mombasa", "Kisumu"],
        "units": [19, 13, 14],
    }
)
sns.set_context("talk")
sns.barplot(data=df, x="city", y="units")
plt.title("talk context")
plt.show()
sns.set_context("notebook")

paper is smaller; talk and poster are larger. Reset to "notebook" so later chapters match the rest of the course.

Palette

df = pd.DataFrame(
    {
        "city": ["Nairobi", "Nairobi", "Mombasa", "Mombasa", "Kisumu", "Kisumu"],
        "product": ["A", "B", "A", "B", "A", "B"],
        "units": [12, 7, 9, 4, 11, 3],
    }
)
sns.set_palette("deep")
sns.barplot(data=df, x="city", y="units", hue="product")
plt.title("deep palette")
plt.show()

Try "muted", "colorblind", or "Set2". Themes persist for the rest of the session until you change them again.

sns.set_theme()
print("reset to workbench defaults")
Tip

If a later block looks “off”, paste sns.set_theme() at the top of the next script, or Reset the editor and re-run.