How to change the font size on a matplotlib plot
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
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.
Related reading
- How to change the font size on a matplotlib plot
- How to change the order of DataFrame columns?
- How to change the order of DataFrame columns?
- How to change the threshold on decision tree classifier model?
- How to change the name of a Django app?
- How to check a string for specific characters?
- How to change tick label font size
- How to check dataset if valid for some classify in WEKA api?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.