Pass the fontsize (or size) keyword argument directly to title and label functions—such as ax.set_title(..., fontsize=16), ax.set_xlabel(..., fontsize=12), ax.set_ylabel(..., fontsize=12), and fig.suptitle(..., fontsize=18)—or configure global defaults across an entire script using plt.rcParams.
Basic Example
import matplotlib.pyplot as plt
# 1. Create a figure and axes
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [10, 25, 18, 32])
# 2. Customize font sizes individually on the Axes and Figure
fig.suptitle("Overall Figure Suptitle", fontsize=18, fontweight="bold")
ax.set_title("Subplot Title", fontsize=14, pad=10)
ax.set_xlabel("Time (Seconds)", fontsize=12)
ax.set_ylabel("Amplitude (mV)", fontsize=12)
# 3. Also resize tick labels and legend text to match
ax.tick_params(axis="both", labelsize=10)
ax.legend(["Signal A"], fontsize=11)
plt.tight_layout()
plt.show()
Key Details & Global Styling
fontsizevs.size: Bothfontsize=14andsize=14are valid aliases in Matplotlib text methods (set_title,set_xlabel,set_ylabel,suptitle, andtext).- Relative string sizes: Instead of fixed point numbers, you can pass relative CSS-style keywords (
"xx-small","x-small","small","medium","large","x-large","xx-large") that scale automatically relative tofont.size. - Setting global font sizes once with
plt.rcParams: Rather than passingfontsize=to every single plot in a multi-chart script or notebook, updateplt.rcParamsat the top of your file:import matplotlib.pyplot as plt plt.rcParams.update({ "font.size": 12, # Base font size for text and legends "figure.titlesize": 18, # fig.suptitle() "axes.titlesize": 15, # ax.set_title() / plt.title() "axes.labelsize": 13, # ax.set_xlabel() and ax.set_ylabel() "xtick.labelsize": 11, # X-axis tick numbers "ytick.labelsize": 11, # Y-axis tick numbers "legend.fontsize": 11, # Legend entries }) - Scoped temporary styling (
plt.rc_context): Wrap a specific plot insidewith plt.rc_context({"axes.titlesize": 16, "axes.labelsize": 13}):to apply custom sizes to just one figure without mutating global defaults for subsequent plots. - Preventing clipped large titles: Increasing
suptitleoraxes.labelsizecan push text outside the canvas boundary—callfig.tight_layout()(or usesubplots(..., layout="constrained")) and passbbox_inches="tight"when saving withsavefig().
Matplotlib Text Elements & Font Size Reference
| Plot Element | Object-Oriented API (ax / fig) |
Pyplot API (plt) |
Global rcParams Key |
|---|---|---|---|
| Figure Suptitle | fig.suptitle("...", fontsize=18) |
plt.suptitle("...", fontsize=18) |
figure.titlesize |
| Axes / Subplot Title | ax.set_title("...", fontsize=14) |
plt.title("...", fontsize=14) |
axes.titlesize |
| X-Axis Label | ax.set_xlabel("...", fontsize=12) |
plt.xlabel("...", fontsize=12) |
axes.labelsize |
| Y-Axis Label | ax.set_ylabel("...", fontsize=12) |
plt.ylabel("...", fontsize=12) |
axes.labelsize |
| Axis Tick Labels | ax.tick_params(axis="both", labelsize=10) |
plt.xticks(fontsize=10) / plt.yticks(...) |
xtick.labelsize / ytick.labelsize |
| Legend Text | ax.legend(fontsize=11) |
plt.legend(fontsize=11) |
legend.fontsize |
You must log in or register to comment.
