Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 57 ยท Line Plots and Basic Styling

Ticks, Locators and Formatters

Take control of Matplotlib tick marks: set_xticks, tick labels, MultipleLocator, MaxNLocator, FuncFormatter, percent and currency labels, minor ticks.

Ticks are the small marks along an axis, and tick labels are the numbers or words beside them. Matplotlib chooses both automatically, and it usually does a good job. Real charts, though, need things like ticks every 5 units, labels in percent, a dollar sign on money or month names instead of numbers.

Behind every axis there are two cooperating objects. A locator decides where the ticks go, and a formatter decides what text each tick shows. Once you know that split, every tick tweak becomes a small, predictable change. This lesson comes after Limits, Scales and Aspect.

Two objects run each axis

Locator Chooses where ticks sit.
Formatter Chooses the text of each label.

Change one without touching the other.

1 / 2

Setting ticks by hand

The fastest way is to list the positions yourself with ax.set_xticks([...]). Add labels with ax.set_xticklabels([...]), or give both at once with ax.set_xticks(positions, labels=names). When you set labels, always set the ticks first so each label is attached to a known position.

Python
ax.set_xticks([0, 1, 2, 3])
ax.set_xticklabels(["Q1", "Q2", "Q3", "Q4"])
# or in one call
ax.set_xticks([0, 1, 2, 3], labels=["Q1", "Q2", "Q3", "Q4"])

Hand-set ticks are fixed. If the data or limits change later, they do not adapt, which is perfect for categories but wrong for continuous data. For a continuous axis, use a locator instead.

Locators: where ticks go

Locators live in matplotlib.ticker. You attach one with ax.xaxis.set_major_locator(...) (or ax.yaxis).

  • MultipleLocator(base) places a tick at every multiple of base: 0, 5, 10, 15 for base=5.
  • MaxNLocator(nbins, integer=True) allows at most nbins intervals and picks "nice" values. With integer=True it never places a tick on a fraction, which is ideal for counts and years.
  • FixedLocator(list) is what set_xticks uses under the hood.
  • LogLocator places ticks on powers of ten for log axes, and AutoMinorLocator(n) divides each major gap into n equal minor intervals.
Python Locators in action

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On the right, a day is divided into quarters with major ticks, and each quarter gets six minor ticks, so a reader can find an hour at a glance. AutoMinorLocator(6) splits each major gap into 6 equal parts; note that the number is the count of intervals, not of minor ticks.

Formatters: what labels say

Formatters decide how a tick position becomes text. Common ones:

  • StrMethodFormatter("{x:.1f}") uses Python's format mini-language: "${x:,.0f}" gives $1,500.
  • FuncFormatter(func) calls your own function func(value, position) and uses its returned string. It can do anything.
  • PercentFormatter(xmax=1) turns 0.25 into 25%. With the default xmax=100 it treats 25 as 25%.
  • FormatStrFormatter("%.2f") uses the older percent-style format string.
  • NullFormatter() hides labels but keeps the ticks.
Python Currency, percent and custom labels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The data underneath never changes: the bars are still 1250000 and so on. Only the text of the labels is built by the formatter. That matters when you read values back, for example with ax.get_yticks().

Dates and months

Showing month names for positions 1 to 12 is a one-liner with FuncFormatter, a list lookup, or set_xticks(range(1, 13), labels=[...]). For real dates there are dedicated locators and formatters, covered in Dates and Time Series Charts.

tick_params: style, direction and visibility

ax.tick_params(axis="x", ...) styles ticks and labels in bulk. Useful options: which="major", "minor" or "both"; length and width of the marks; direction="in", "out" or "inout"; labelsize, labelcolor and labelrotation; top=True and right=True to also draw ticks on the opposite sides.

To hide ticks, pass bottom=False, left=False for the marks and labelbottom=False, labelleft=False for the labels, or call ax.set_xticks([]). A clean "no axes" look for pictures is ax.axis("off").

Python Minor ticks, tick_params and hidden ticks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A common reason to hide y ticks is when bars are labelled directly with values, using ax.bar_label, so the axis would only repeat information. See Designing Clear Charts for when less decoration is better.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which object decides WHERE ticks appear?

    Locators choose tick positions; formatters turn each position into label text.
  2. 2. How do you make a y axis show $1,500 for the value 1500?

    A formatter controls the text. StrMethodFormatter uses Python's format syntax, and :, adds thousands separators.
  3. 3. You plot a count of events per year. Which locator avoids ticks like 2020.5?

    MaxNLocator with integer=True only picks whole numbers.
  4. 4. What does AutoMinorLocator(4) do?

    The argument is the number of intervals per major gap, so there are n - 1 minor ticks between two majors.
  5. 5. How can you hide the tick labels on the y axis while keeping the axis line?

    tick_params can switch off labels and tick marks independently of the spines.

Summary

  • A locator decides where ticks go, a formatter decides what text they show.
  • set_xticks (with labels=) fixes positions by hand, ideal for categories; use locators for continuous data.
  • MultipleLocator, MaxNLocator(integer=True) and AutoMinorLocator(n) are the everyday locators.
  • StrMethodFormatter, FuncFormatter and PercentFormatter produce currency, custom text and percent labels without changing the data.
  • tick_params styles or hides ticks and labels; set which="minor" for minor ticks.

Next, explain what each line means in Legends and Grids.