Tick Locators & Formatters
Tick locators decide where the marks on an axis go, while tick formatters decide how each mark's value is written — together they give you complete control over axis labels.
Learn Tick Locators & Formatters in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll set ticks by hand, place them with MaxNLocator and MultipleLocator , reformat numbers with FuncFormatter and PercentFormatter , rotate crowded labels, and add minor ticks.
The most direct approach: call ax.set_xticks() with the positions where ticks should sit, then ax.set_xticklabels() with the text for each. This is ideal when you plot against numeric positions but want friendly labels like month names underneath.
What you'll see: a five-point line of monthly sales rising overall, with the x-axis marked Jan through May instead of the raw numbers 0 to 4.
When the data range varies, hard-coding ticks is fragile. Locators place them by a rule instead. MultipleLocator(2) puts a tick at every multiple of 2, while MaxNLocator(5) picks at most five nicely rounded values. Both live in matplotlib.ticker and attach via ax.xaxis.set_major_locator() .
What you'll see: two sine waves. The top one has evenly stepped gridlike ticks every 2 units on x and 0.5 on y; the bottom one shows just a handful of clean, rounded x-ticks chosen automatically.
A formatter rewrites each tick's number. PercentFormatter(xmax=1.0) turns 0.25 into 25%. FuncFormatter runs your own function on every value — perfect for currency. StrMethodFormatter("{x:,.0f}") applies a format string with thousands separators. Attach any of them with ax.yaxis.set_major_formatter() .
What you'll see: two panels. The left bars have y-axis labels like $1,200 and $2,400; the right line has y-axis labels like 10% and 30% — the same numbers dressed up for their audience.
Replace each ___ to rotate the long day labels so they stop overlapping.
Hint: 45 degrees with ha="right" is the classic fix for crowded labels.
You set labels without matching positions. Call set_xticks(positions) first, then set_xticklabels(labels) with the same count.
Your formatter function must accept both value and position : lambda v, pos: ... , even if you ignore pos .
Import from the right module: from matplotlib.ticker import MultipleLocator, FuncFormatter, PercentFormatter .
Plot exponential growth with major ticks every 2 and minor ticks every 0.5, format the y-axis with thousands separators, and add a two-level grid.
Lesson complete — your axes now say exactly what you mean!
You set ticks by hand, placed them with MultipleLocator and MaxNLocator , reformatted numbers with FuncFormatter and PercentFormatter , rotated long labels, and added minor ticks.
🚀 Up next: Text, Math & LaTeX in Plots — annotate your charts with words and equations.
Practice quiz
What does a tick LOCATOR control?
- The chart's colormap
- The figure size
- How each tick number is written
- WHERE the ticks are placed on an axis
Answer: WHERE the ticks are placed on an axis. A locator decides where ticks go; a formatter decides how each is written.
Which pair sets tick positions and their text labels by hand?
- ax.ticks() and ax.labels()
- ax.set_locator() and ax.set_text()
- ax.set_xticks() and ax.set_xticklabels()
- ax.xticks() and ax.xlabels()
Answer: ax.set_xticks() and ax.set_xticklabels(). set_xticks(positions) then set_xticklabels(labels) places labels manually.
Which locator puts a tick at every multiple of a value?
- MaxNLocator(n)
- MultipleLocator(n)
- AutoLocator(n)
- FixedLocator(n)
Answer: MultipleLocator(n). MultipleLocator(2) places a tick at every multiple of 2.
What does MaxNLocator(5) do?
- Picks at most 5 nicely-rounded tick values
- Places exactly 5 random ticks
- Removes all but 5 data points
- Limits the axis to 5 units
Answer: Picks at most 5 nicely-rounded tick values. MaxNLocator(5) chooses at most five tidy, rounded tick values.
Which formatter turns 0.25 into 25%?
- FuncFormatter(xmax=1.0)
- StrMethodFormatter('%')
- ScalarFormatter()
- PercentFormatter(xmax=1.0)
Answer: PercentFormatter(xmax=1.0). PercentFormatter(xmax=1.0) renders fractions as percentages.
How many arguments must a FuncFormatter function accept?
- Zero
- Exactly one
- Two — the value and its position
- Three
Answer: Two — the value and its position. A FuncFormatter receives the value and its position; both must be in the signature.
Which module holds MultipleLocator and FuncFormatter?
- matplotlib.pyplot
- matplotlib.ticker
- matplotlib.axes
- matplotlib.scale
Answer: matplotlib.ticker. Locators and formatters live in matplotlib.ticker.
Which call attaches a locator to the x-axis?
- ax.xaxis.set_major_locator(loc)
- ax.set_locator(loc)
- ax.xticks(loc)
- ax.locator_x(loc)
Answer: ax.xaxis.set_major_locator(loc). ax.xaxis.set_major_locator() attaches the locator to the x-axis.
How do you stop long category labels from overlapping?
- Hide the x-axis entirely
- Make the figure smaller
- Use a logarithmic scale
- Rotate them, e.g. rotation=45, ha='right'
Answer: Rotate them, e.g. rotation=45, ha='right'. set_xticklabels(labels, rotation=45, ha='right') keeps crowded labels readable.
What does StrMethodFormatter('{x:,.0f}') add to numbers?
- Percent signs
- Thousands separators
- Scientific notation
- Currency symbols
Answer: Thousands separators. The format string {x:,.0f} adds thousands separators to each tick value.