Python
Matplotlib
Data Visualization
Font Size
Plot Customization

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 is not a single setting. Titles, axis labels, tick labels, legends, and annotations can each have their own font size, and the best approach depends on whether you want to style one plot or set a default for many plots.

Change Font Size Per Plot Element

The most direct approach is to set font size where you create each element.

python
1import matplotlib.pyplot as plt
2
3x = [1, 2, 3]
4y = [2, 4, 3]
5
6fig, ax = plt.subplots()
7ax.plot(x, y, label="Series A")
8ax.set_title("Example Plot", fontsize=18)
9ax.set_xlabel("X axis", fontsize=14)
10ax.set_ylabel("Y axis", fontsize=14)
11ax.legend(fontsize=12)
12plt.show()

This is the most explicit solution and is usually best when only one figure needs customization.

Change Tick Label Font Size

Tick labels have their own setting.

python
ax.tick_params(axis="both", labelsize=12)

Or more completely:

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots()
4ax.plot([1, 2, 3], [3, 1, 4])
5ax.tick_params(axis="both", which="major", labelsize=12)
6plt.show()

This is a common detail people miss when they change title and axis label sizes but forget the tick labels.

Set Global Defaults With rcParams

If you want all plots in a script or notebook to use larger fonts, change Matplotlib defaults.

python
1import matplotlib.pyplot as plt
2
3plt.rcParams["axes.titlesize"] = 18
4plt.rcParams["axes.labelsize"] = 14
5plt.rcParams["xtick.labelsize"] = 12
6plt.rcParams["ytick.labelsize"] = 12
7plt.rcParams["legend.fontsize"] = 12

Now every later plot in that session uses those defaults unless overridden.

This is the right answer when consistent styling matters across many figures.

Use plt.rc_context For Temporary Defaults

Sometimes you want global-looking defaults, but only for one block of code.

python
1import matplotlib.pyplot as plt
2
3with plt.rc_context({
4    "axes.titlesize": 20,
5    "axes.labelsize": 16,
6    "xtick.labelsize": 12,
7    "ytick.labelsize": 12,
8}):
9    fig, ax = plt.subplots()
10    ax.plot([1, 2, 3], [3, 2, 5])
11    ax.set_title("Styled Only Here")
12    ax.set_xlabel("X")
13    ax.set_ylabel("Y")
14    plt.show()

This is useful in shared notebooks where you do not want to permanently change the plotting environment.

Legends, Text, And Annotations

Legends and free text have their own font controls too.

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots()
4ax.plot([1, 2, 3], [2, 3, 5], label="data")
5ax.legend(fontsize=11)
6ax.text(2, 4, "peak", fontsize=13)
7plt.show()

So if a plot still looks inconsistent after changing the obvious settings, check whether annotations or legends are still using defaults.

Object-Oriented API Is Easier To Maintain

You can set font sizes through plt.title(...) and similar stateful helpers, but the object-oriented style is clearer for nontrivial plots.

python
fig, ax = plt.subplots()
ax.set_title("Title", fontsize=18)

This becomes especially helpful once you have multiple axes in one figure.

Apply A Consistent Style Across Subplots

When a figure has several subplots, repeat the same font policy on each axis instead of styling them one by one in an ad hoc way.

python
1import matplotlib.pyplot as plt
2
3fig, axes = plt.subplots(1, 2, figsize=(8, 3))
4
5for ax in axes:
6    ax.plot([1, 2, 3], [1, 4, 2])
7    ax.set_xlabel("Input", fontsize=12)
8    ax.set_ylabel("Output", fontsize=12)
9    ax.tick_params(axis="both", labelsize=10)
10
11axes[0].set_title("Left", fontsize=14)
12axes[1].set_title("Right", fontsize=14)
13plt.tight_layout()
14plt.show()

This keeps dashboards and comparison figures readable without relying on hidden global state.

Common Pitfalls

The most common mistake is changing only the title font size and forgetting tick labels, legend text, or annotations.

Another mistake is mixing global rcParams tweaks with local overrides and then losing track of why different plots look inconsistent.

Developers also sometimes call plt.xticks(fontsize=...) in one place and tick_params in another without a clear style policy.

Finally, if you are working in notebooks, remember that rcParams changes persist in the session until you reset them or restart the kernel.

Summary

  • Set fontsize directly on titles, labels, legends, and text for per-plot control.
  • Use tick_params(labelsize=...) for tick labels.
  • Use rcParams when you want consistent defaults across many plots.
  • Use plt.rc_context for temporary styling scopes.
  • Check every text element, not just the title, when adjusting readability.

Course illustration
Course illustration

All Rights Reserved.