How to change the font size on a matplotlib plot
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Matplotlib, font size can be controlled at several levels: individual labels, axis ticks, legends, or the global plotting style. The right approach depends on whether you want to tune one plot element or establish consistent typography across many figures.
Change Font Size For Specific Elements
The most direct method is to pass fontsize= when creating titles, axis labels, legends, or annotations.
This is the best option when only one or two text elements need adjustment.
Change Tick Label Size
Axis labels and tick labels are separate. If you change the title size but leave ticks alone, the plot can still look cramped.
tick_params is the cleanest way to resize tick labels on one axes object.
Set A Global Default With rcParams
If you want consistent font sizing across multiple plots, set Matplotlib defaults before creating figures.
This is a better choice when you produce many figures in the same script or notebook.
Use The Object-Oriented API For Control
The object-oriented Matplotlib API is usually easier to manage in larger scripts because every font change is attached to a specific axes.
This style becomes especially useful when a figure contains multiple subplots with different needs.
Changing Font Family And Weight
If the problem is not only size but also emphasis, use fontdict or FontProperties.
For most tasks, though, fontsize= is enough.
Pick Sizes Relative To Figure Size
Font size cannot be chosen in isolation. A 14-point label looks very different on a small dashboard tile versus a full-page report plot.
A practical rule is:
- use larger title fonts than axis labels
- use readable tick labels, but do not let them dominate
- increase font size when the figure will be projected or printed
Typography should support the plot, not fight with it.
Common Pitfalls
A common mistake is changing only plt.rcParams["font.size"] and expecting titles, ticks, legends, and labels all to look balanced automatically. Some plot elements have their own more specific defaults.
Another issue is forgetting tick labels. Developers often enlarge the axis labels and title but leave the ticks too small to read.
Mixing plt.* stateful code and the object-oriented API can also become confusing in larger scripts. If a figure has several axes, it is safer to set font sizes on each ax explicitly.
Finally, avoid choosing font sizes without considering the final output medium. A chart that looks fine in a notebook may be unreadable in a slide deck or printed report.
Summary
- Use
fontsize=for individual text elements such as titles, labels, legends, and annotations. - Use
tick_params(labelsize=...)for tick labels. - Use
rcParamswhen you want consistent font sizing across multiple figures. - Prefer the object-oriented API for more complex plotting code.
- Choose sizes in relation to the figure size and where the plot will actually be viewed.

