Use Axes.set_xticks() to choose x-axis tick positions and Axes.set_xlim() to control the visible range. To use a regular interval, generate the positions first, then set the limits after the ticks so Matplotlib does not expand the range to show every tick.
Set a regular x-axis tick interval and exact range
set_xticks accepts tick positions; it does not take start, stop, and interval arguments. Generate the positions with a tool such as NumPy, apply them, then set the x-axis limits:
As an Amazon Associate I earn from qualifying purchases.
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(x, y)
start, stop, step = 0, 10, 2
ticks = np.arange(start, stop + step, step)
ax.set_xticks(ticks)
ax.set_xlim(start, stop)
plt.show()
Here the tick positions are 0, 2, 4, 6, 8, and 10, while the visible x-axis runs from 0 to 10. Replace start, stop, and step with the values appropriate for your data. The tick locations are expressed in the axis’s units.
Check the endpoint when using fractional steps
np.arange can produce a final value that differs from the endpoint you intended, especially with non-integer steps. Inspect ticks if the endpoint matters, and adjust the generated positions as needed. Setting xlim controls the view; it does not add a missing tick position.
#1 Best Overall
Why set the limits after the ticks?
Matplotlib may expand the view limits to ensure all positions passed to set_xticks are visible. If you need an exact range, call set_xlim(start, stop) after set_xticks(ticks). Ticks outside those limits will not be visible within the chosen range.
Set custom labels or minor ticks
To specify labels, provide one label for each tick position:
ax.set_xticks([0, 2, 4, 6], labels=["zero", "two", "four", "six"])
The locations and labels must correspond one-for-one. If you omit labels, Matplotlib uses the axis formatter. The default call sets major ticks; use minor=True to set minor ticks instead:
ax.set_xticks(ticks, minor=True)
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.When tick marks appear without labels
A tick position and its label are separate concerns: the axis formatter decides which positions receive text. Some formatters do not label arbitrary locations. For example, logarithmic formatters label decades by default, so a custom position between decades may have a tick mark but no label. Supply explicit labels with set_xticks(ticks, labels=...) or use an appropriate explicit formatter when every position needs text.
Rank #3
These behaviors are documented in the Matplotlib 3.11.1 Axes.set_xticks API reference, checked October 7, 2026. If you use another Matplotlib release, consult that release’s API reference.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




