Update plt.rcParams (such as plt.rcParams["font.size"] = 14) to change font sizes globally across all figures in a script, or pass fontsize / labelsize directly to specific text and tick methods (ax.set_title(..., fontsize=16), ax.set_xlabel(..., fontsize=12), ax.tick_params(labelsize=10)) to customize individual elements on a single plot.
Basic Example
import matplotlib.pyplot as plt
# 1. GLOBAL: Apply font sizes across all subsequent plots in the script
plt.rcParams.update({
"font.size": 12, # Default base font size (text, annotations, legends)
"axes.titlesize": 16, # Subplot titles (ax.set_title / plt.title)
"axes.labelsize": 13, # X and Y axis labels (ax.set_xlabel / ax.set_ylabel)
"xtick.labelsize": 11, # X-axis tick numbers
"ytick.labelsize": 11, # Y-axis tick numbers
"legend.fontsize": 11, # Legend entries
"figure.titlesize": 18, # Overall figure suptitle (fig.suptitle)
})
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [10, 20, 15, 25], label="Trend A")
# 2. INDIVIDUAL: Override specific elements on this Axes without changing globals
ax.set_title("Custom Individual Title", fontsize=20, fontweight="bold")
ax.set_xlabel("Time (s)", fontsize=14)
ax.set_ylabel("Value", fontsize=14)
ax.tick_params(axis="both", labelsize=10)
ax.legend(fontsize=12)
plt.tight_layout()
plt.show()
Key Details & Edge Cases
- Order of execution matters for
plt.rcParams: Matplotlib readsrcParamsat the moment each figure or text object is created. Always configureplt.rcParamsbefore callingplt.subplots()orplt.figure(), and note that callingplt.style.use("seaborn-v0_8")later will overwrite yourrcParamscustomizations—so applyplt.style.use()first, then updateplt.rcParams. - Temporary scoped overrides (
plt.rc_context): If you want global-style settings for a single figure without mutating state for the rest of your notebook or application, wrap the plot inside a context manager:with plt.rc_context({"font.size": 14, "axes.titlesize": 18}): fig, ax = plt.subplots()
You must log in or register to comment.
