Unit 09.04: One function that produces your house style
House style belongs in one function, not repeated across thirty figures.
One entry point for every chart in the deck
A function that applies the style settings and returns a figure and axes ready to plot on.
The code defines one and inspects the result.
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
HOUSE = {"figure.figsize": (6, 3.5), "axes.spines.top": False,
"axes.spines.right": False, "axes.grid": True,
"grid.alpha": 0.3, "font.size": 11}
def house_figure(title, subtitle=""):
"""Every chart in the deck starts here, so the deck is consistent."""
with plt.rc_context(HOUSE):
fig, ax = plt.subplots()
ax.set_title(title, loc="left", fontweight="bold")
if subtitle:
ax.text(0, 1.02, subtitle, transform=ax.transAxes, fontsize=9)
return fig, ax
fig, ax = house_figure("Defect rate above target in weeks 12-13",
"UK line 2, weekly, Jan-Mar 2026")
ax.plot([1, 2, 3], [2, 3, 2])
print(f"top spine visible : {ax.spines['top'].get_visible()}")
print(f"grid on : {ax.xaxis._major_tick_kw.get('gridOn', True)}")
print(f"title : {ax.get_title(loc='left')!r}")
plt.close(fig)
print("\nThe title carries the finding and the subtitle carries the scope.")
print("Both are set once, in one place, for every chart in the deck.")
Every chart in the deck now starts from the same call, so the deck is consistent by construction rather than by discipline. Changing the style is one edit.
The signature is the useful part: it takes a title and a subtitle, which encodes the Module 3 rule - the title carries the finding, the subtitle carries the scope and period - into the structure of the code.
The mistake this prevents
The mistake is putting the style in a global configuration and the titles elsewhere. Bundling them in one function means the person making the chart is prompted for the finding and the scope at the moment they create the figure, which is when they still know both.
Takeaway
Put house style in one function that also asks for the title and subtitle. Consistency becomes structural, and the scope gets captured while it is still known.
