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
plt.style.useapplies a named sheet.rc_contextscopes changes to a block.availablelists what is installed.
Keywords and builtins used here
asforifprintwith
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.
Step 3 of 4 in Figures & axes, step 3 of 14 in Plotting with matplotlib.