Style sheets

plt.style.use, ggplot, seaborn-v0_8, and resetting.

plt.style.use changes defaults for every new Axes: colors, grid, fonts. Reset with "default" when a snippet should look like the rest of the course.

Goal

List a few styles, apply ggplot, and return to default.

What is installed

print([s for s in plt.style.available if "ggplot" in s or "seaborn" in s or s == "default"][:12])

ggplot

x = np.linspace(0, 2 * np.pi, 80)
plt.style.use("ggplot")
fig, ax = plt.subplots()
ax.plot(x, np.sin(x), label="sin")
ax.plot(x, np.cos(x), label="cos")
ax.legend()
ax.set_title("ggplot")
plt.show()
plt.style.use("default")

The last line matters: styles persist for the rest of the session until you reset.

seaborn-v0_8 (if present)

name = "seaborn-v0_8" if "seaborn-v0_8" in plt.style.available else "ggplot"
plt.style.use(name)
fig, ax = plt.subplots()
ax.bar(["A", "B", "C"], [12, 7, 2])
ax.set_title(name)
plt.show()
plt.style.use("default")

These are style sheets, not the seaborn library. For seaborn itself, use the Seaborn workbench and Learn seaborn.

Pitfall

If a later block looks “off”, you probably left a style on. Paste plt.style.use("default") at the top of the next script, or Reset the editor and re-run.