Control Matplotlib Axis Ticks with Custom Locators and Formatters
Control Matplotlib tick placement and label formatting with Locators and Formatters. Use MultipleLocator for fixed intervals, FuncFormatter for custom text, and MaxNLocator with FormatStrFormatter to cap tick count and precision.
17 Jan 2026, 20:48 UTC

Problem: default ticks crowd or mislabel your data
Matplotlib chooses tick positions automatically. On a wide range or a small figure that choice can produce overlapping labels, uneven spacing, or numbers that are not readable for your domain. The useful takeaway is that tick placement and tick text are separate concerns, both controlled through matplotlib.ticker. A Locator decides where ticks go, a Formatter decides how those values are rendered.
Desired outcome
You want predictable tick intervals, a maximum number of ticks, and human readable labels such as currency or percentages, without changing the plotted data.
Prerequisites
Matplotlib installed in the Python environment where you run the script. A working Axes object from plt.subplots or a figure. Familiarity with ax.xaxis and ax.yaxis. No special permissions are required; changes are in-memory until you save or show the figure.
Core concepts
A Locator is an object that returns a sequence of tick positions for a given view limits. A Formatter receives a numeric value and a position index and must return a string.
Entry points are Axis.set_major_locator and Axis.set_major_formatter. Minor ticks use set_minor_locator and set_minor_formatter. Changing a locator or formatter does not alter data, only its presentation.
Focused procedure
Fixed interval spacing with MultipleLocator
Use MultipleLocator when you need ticks every N units. This is useful for consistent visual grids.
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig, ax = plt.subplots()
ax.plot([0, 20], [0, 20])
ax.xaxis.set_major_locator(MultipleLocator(base=5))Expected check: the x axis shows major ticks at multiples of 5 within the current limits. If the limits change, the locator recomputes on draw.
Custom label text with FuncFormatter
FuncFormatter wraps a Python callable that maps value to string. The callable signature is value, position.
from matplotlib.ticker import FuncFormatter
def currency_fmt(x, pos):
return f'{x:,.0f} USD'
ax.xaxis.set_major_formatter(FuncFormatter(currency_fmt))Expected check: tick labels are transformed by the function and remain strings. Returning a non-string can cause rendering errors in some backends.
Cap tick count and control precision
MaxNLocator limits the number of major ticks and chooses nice locations. Pair it with FormatStrFormatter for fixed decimal precision.
from matplotlib.ticker import MaxNLocator, FormatStrFormatter
ax.yaxis.set_major_locator(MaxNLocator(nbins=6))
ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))Expected check: no more than about six y ticks are drawn and each label shows two decimal places.
Combined example
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FuncFormatter, MaxNLocator, FormatStrFormatter
def currency_fmt(x, pos):
return f'{x:,.0f} USD'
fig, ax = plt.subplots()
ax.plot([0, 20], [0, 20])
ax.xaxis.set_major_locator(MultipleLocator(base=5))
ax.xaxis.set_major_formatter(FuncFormatter(currency_fmt))
ax.yaxis.set_major_locator(MaxNLocator(nbins=5))
ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
plt.show()Risk: too many ticks for the figure DPI and size cause overlapping labels. Reduce nbins or increase figure size.
Limitations and cautions
Logarithmic scales require LogLocator and LogFormatter. Applying a linear MultipleLocator or FuncFormatter to a log axis produces incorrect spacing.
Custom formatters must return a string for every input. Edge cases such as NaN or very large values should be handled explicitly.
Changing locators after the view limits are set is fine, but the change only becomes visible after a draw or show.
Verification
- Instantiate a figure and apply MultipleLocator(base=5). Verify ticks appear at regular 5 unit intervals across the visible range.
- Implement a FuncFormatter that appends a unit symbol. Verify the label transformation logic is applied to each tick.
- Set MaxNLocator with nbins and compare the drawn tick count against the requested maximum.
Recovery options
If a custom locator or formatter produces undesirable results, reset to defaults:
ax.xaxis.set_major_locator(plt.AutoLocator())
ax.xaxis.set_major_formatter(plt.ScalarFormatter())For a full reset you can also call ax.relim and ax.autoscale_view. These operations are safe and do not modify data.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.