Call ax.tick_params(axis="both", labelsize=12) (or plt.xticks(fontsize=12) / plt.yticks(fontsize=12) in pyplot state-machine code) to resize tick labels on a specific plot, or set plt.rcParams.update({"xtick.labelsize": 12, "ytick.labelsize": 12}) once at the top of your script to apply a consistent tick label size across all subplots and figures.
Basic Example
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6, 4))
ax.plot([1, 2, 3, 4], [10, 25, 18, 32])
# 1. Recommended (Object-Oriented API): Resize both X and Y tick labels on `ax`
ax.tick_params(axis="both", which="major", labelsize=12)
# 2. Customize X and Y axes independently (e.g., smaller minor ticks or rotated X labels):
ax.tick_params(axis="x", labelsize=13, labelrotation=30)
ax.tick_params(axis="y", labelsize=11)
plt.tight_layout()
plt.show()
Key Details & Edge Cases
which="major"vs.which="minor"vs.which="both"intick_params():- By default,
ax.tick_params(labelsize=12)modifies onlywhich="major"ticks. On logarithmic plots (ax.set_yscale("log")) or axes withAutoMinorLocator, minor tick labels remain at their old size unless you explicitly passwhich="both"(or configure minor ticks separately withwhich="minor").
- By default,
- Resizing colorbar tick labels (
cbar.ax.tick_params(labelsize=...)):- Calling
ax.tick_params(...)on the main plot axes does not resize the tick labels on afig.colorbar(...). Always callcbar.ax.tick_params(labelsize=11)on the colorbar’s own.axattribute.
- Calling
- Avoid calling
ax.set_xticklabels(ax.get_xticklabels(), fontsize=12):- Calling
ax.set_xticklabels(...)converts dynamic tick locators into aFixedLocatorand triggersUserWarning: set_ticklabels() should only be used with a fixed number of tickswhenever you zoom, pan, or autoscale. Useax.tick_params(axis="x", labelsize=12)instead—it changes only the font size without locking tick positions.
- Calling
- Setting global defaults for an entire notebook or multi-subplot figure (
plt.rcParams):- To avoid repeating
ax.tick_params(...)on every subplot in a grid (fig, axes = plt.subplots(2, 2)), configurercParamsbefore creating figures (or scope it temporarily withwith plt.rc_context(...):):plt.rcParams.update({ "xtick.labelsize": 11, "ytick.labelsize": 11, })
- To avoid repeating
- Scientific notation offset text (
1e6at the corner of the axis):tick_params(labelsize=...)resizes the tick numbers themselves, not the axis multiplier/offset label (1e6) rendered at the end of an axis. Resize that label separately viaax.xaxis.get_offset_text().set_fontsize(11)(orax.yaxis.get_offset_text().set_fontsize(11)).
Comparison of Methods for Setting Tick Label Font Sizes in Matplotlib
| Method | Scope | Safe with Dynamic Tick Locators? | Supports Major & Minor Ticks? | Best Used When |
|---|---|---|---|---|
ax.tick_params(axis="both", which="both", labelsize=12) (Recommended) |
Single Axes (or cbar.ax) |
Yes | Yes ("major", "minor", "both") |
Standard Object-Oriented Matplotlib plots, multi-subplot dashboards, and colorbars. |
plt.rcParams.update({"xtick.labelsize": 12, "ytick.labelsize": 12}) |
All figures created afterward | Yes | Major ticks (rcParams global) |
Establishing uniform publication/presentation styling across an entire script or notebook. |
plt.xticks(fontsize=12) / plt.yticks(fontsize=12) |
Current active Axes (pyplot) |
Yes (when passed with no tick labels) | Major ticks only | Quick single-plot scripts using the plt.* state-machine interface. |
ax.set_xticklabels(..., fontsize=12) (Avoid for sizing) |
Single Axes |
No (locks ticks to FixedLocator & emits UserWarning) |
Major ticks only | Only when simultaneously replacing tick text strings at fixed positions. |
You must log in or register to comment.
