plot ticks
data visualization
axis customization
plotting techniques
chart aesthetics

reducing number of plot ticks

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Too many axis ticks make a chart look busy before the reader has even looked at the data. Reducing the number of ticks is usually not about cosmetics alone; it improves legibility, prevents overlapping labels, and makes the scale easier to scan.

Let the Locator Do the Work

In Matplotlib, tick placement is controlled by locators. For most linear axes, MaxNLocator is one of the most useful tools when you want fewer ticks without hard-coding every label.

python
1import matplotlib.pyplot as plt
2from matplotlib.ticker import MaxNLocator
3
4x = list(range(20))
5y = [value * value for value in x]
6
7fig, ax = plt.subplots()
8ax.plot(x, y)
9ax.xaxis.set_major_locator(MaxNLocator(nbins=5))
10ax.yaxis.set_major_locator(MaxNLocator(nbins=4))
11
12plt.show()

nbins means "at most about this many intervals," not "guarantee exactly this many printed labels." That distinction matters because Matplotlib still tries to choose nice round values.

Use Fixed Spacing When the Scale Has a Natural Step

Sometimes the data has a natural increment such as every 10 units or every hour. In those cases, a fixed locator can be clearer than asking Matplotlib to guess.

python
1import matplotlib.pyplot as plt
2from matplotlib.ticker import MultipleLocator
3
4fig, ax = plt.subplots()
5ax.plot([0, 10, 20, 30, 40], [1, 3, 2, 5, 4])
6ax.xaxis.set_major_locator(MultipleLocator(10))
7
8plt.show()

This is a good fit when you want predictable tick spacing rather than adaptive spacing.

Manual Tick Selection

For presentation charts, manual ticks are sometimes the simplest answer:

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots()
4ax.plot([0, 5, 10, 15, 20], [0, 1, 4, 9, 16])
5ax.set_xticks([0, 10, 20])
6ax.set_yticks([0, 5, 10, 15])
7
8plt.show()

This gives full control, but it is best only when the axis range is stable. If the data limits change a lot, manual ticks become brittle and may stop matching the plotted range well.

Reduce Labels Without Removing All Reference Points

You do not always need to remove every extra visual marker. Another option is to keep major ticks sparse and use minor ticks or grid lines for context.

This works well when you want a clean label set but still want the eye to follow the scale. The chart remains readable without becoming empty or overly simplified.

Match the Tick Strategy to the Chart

A small sparkline and a large analytical figure should not use the same tick density. Good practice depends on:

  • figure size
  • font size
  • label length
  • data range
  • whether the chart is exploratory or presentation-ready

If the x-axis contains dates or long category names, reducing count matters even more because label overlap becomes the real problem.

Avoid Fighting Autoscaling Blindly

Matplotlib's default locators already try to avoid extreme clutter. If the output still looks crowded, the issue may be that:

  • the figure is too small
  • the labels are too long
  • the axis range is too wide for the chosen layout

Reducing tick count helps, but rotating labels, enlarging the figure, or changing the formatter may be equally important.

Common Pitfalls

One common mistake is expecting MaxNLocator(nbins=5) to always print exactly five labels. It does not promise that. It chooses a reasonable set up to that limit based on the axis range.

Another issue is hard-coding ticks for data that changes between runs. Manual values look fine once, then become misleading later when the dataset grows or shrinks.

People also remove so many ticks that the scale becomes hard to interpret. The goal is fewer ticks, not no scale information.

Summary

  • Reducing tick count improves readability and often prevents label collisions.
  • 'MaxNLocator is a strong default when you want fewer ticks but still want automatic placement.'
  • 'MultipleLocator works well when the axis has a natural fixed interval.'
  • Manual ticks are useful for stable presentation charts, but fragile for dynamic data.
  • If a chart still looks crowded, consider figure size, label formatting, and layout as well as tick count.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.