tick frequency
x axis
y axis
data visualization
graph customization

Changing the tick frequency on the x or y axis

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Changing tick frequency means controlling how often axis tick marks appear, not changing the data itself. The right frequency improves readability by showing enough reference points without overcrowding the chart. Too many ticks make the plot noisy. Too few make it hard to read values.

The Core Idea

Tick frequency is usually controlled by one of these strategies:

  • set a fixed interval such as every 5 units
  • request a maximum number of ticks
  • use specialized locators for dates or logarithmic axes

The exact API depends on the plotting library, but the design goal is always the same: match tick density to the scale of the chart.

Fixed Intervals in Matplotlib

For Matplotlib, MultipleLocator is a common tool.

python
1import matplotlib.pyplot as plt
2from matplotlib.ticker import MultipleLocator
3
4x = [0, 1, 2, 3, 4, 5, 6, 7, 8]
5y = [0, 1, 4, 9, 16, 25, 36, 49, 64]
6
7fig, ax = plt.subplots()
8ax.plot(x, y)
9
10ax.xaxis.set_major_locator(MultipleLocator(2))
11ax.yaxis.set_major_locator(MultipleLocator(10))
12
13plt.show()

This produces x-axis ticks every 2 units and y-axis ticks every 10 units.

Control Major and Minor Ticks Separately

Sometimes you want a clean major grid and lighter minor guidance.

python
1from matplotlib.ticker import MultipleLocator
2
3ax.xaxis.set_major_locator(MultipleLocator(2))
4ax.xaxis.set_minor_locator(MultipleLocator(1))

Major ticks are typically labeled. Minor ticks are often unlabeled and used only for visual guidance.

This gives more control than trying to solve everything with one dense set of labeled ticks.

Limit the Number of Ticks Automatically

If the axis range changes dynamically, a fixed interval may not scale well. In that case, ask Matplotlib for a reasonable count instead.

python
1from matplotlib.ticker import MaxNLocator
2
3ax.xaxis.set_major_locator(MaxNLocator(nbins=5))
4ax.yaxis.set_major_locator(MaxNLocator(nbins=6))

This does not force an exact spacing. It asks Matplotlib to choose a readable set of ticks with roughly that many bins.

Date Axes Need Date-Aware Locators

Date plots often require different tools because evenly spaced numeric intervals may not correspond to meaningful calendar intervals.

python
1import matplotlib.pyplot as plt
2import matplotlib.dates as mdates
3import datetime as dt
4
5x = [dt.date(2024, 1, d) for d in range(1, 11)]
6y = [3, 5, 2, 6, 7, 5, 8, 9, 6, 4]
7
8fig, ax = plt.subplots()
9ax.plot(x, y)
10ax.xaxis.set_major_locator(mdates.DayLocator(interval=2))
11ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d"))
12plt.xticks(rotation=45)
13plt.tight_layout()
14plt.show()

For time-series charts, using date-aware locators is much better than forcing raw numeric spacing.

The Same Principle Applies in Other Libraries

Even if you are not using Matplotlib, the thinking is similar:

  • set an interval when you want predictable spacing
  • set a target tick count when the range changes often
  • use domain-specific axis logic for dates, logs, or categories

The library-specific syntax changes, but the readability tradeoff does not.

Avoid Overcrowding by Checking Label Size

Tick frequency is not only about numerical scale. It also depends on label size. Long dates or formatted values need more horizontal or vertical room.

If labels overlap, you may need to:

  • reduce frequency
  • rotate labels
  • widen the figure
  • abbreviate the formatter

Changing tick frequency is often one part of a broader axis-layout adjustment.

Common Pitfalls

  • Adding too many ticks and making labels overlap or become unreadable.
  • Using a fixed interval even when the data range changes dramatically between plots.
  • Forgetting to choose date-aware locators for date axes.
  • Mixing major and minor tick intent and ending up with cluttered labeling.
  • Treating tick frequency as purely cosmetic instead of part of the chart's readability.

Summary

  • Tick frequency controls how often axis tick marks appear.
  • In Matplotlib, MultipleLocator is good for fixed spacing and MaxNLocator is good for approximate tick counts.
  • Major and minor ticks should usually be treated separately.
  • Date axes need date-aware locators instead of generic numeric intervals.
  • The best tick frequency is the one that makes the chart easier to read without adding clutter.

Course illustration
Course illustration

All Rights Reserved.