typestar

Styling in Python

Style sheets set the defaults; rcParams override them per figure.

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt

print([name for name in plt.style.available if "white" in name][:3])

with plt.rc_context({"axes.grid": True, "font.size": 9,
                     "axes.spines.top": False, "axes.spines.right": False}):
    figure, axes = plt.subplots()
    axes.plot([1, 2, 3], [1, 4, 2])
    axes.set_title("scoped styling")
    figure.savefig("styled.png")

print(matplotlib.rcParams["axes.grid"])

How it works

  1. plt.style.use applies a named sheet.
  2. rc_context scopes changes to a block.
  3. available lists what is installed.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
420
Tokens
128
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 77 seconds.

Type this snippet

Step 3 of 4 in Figures & axes, step 3 of 14 in Plotting with matplotlib.

← Previous Next →