Use ax.set_xticks(positions, labels) to place x-axis ticks at specific values and display matching text. If you omit labels, Matplotlib’s active formatter supplies the text. Setting ticks can expand the visible axis range, so set xlim afterward when you need a particular range.
Set fixed x-axis positions and labels
In Matplotlib 3.10.9, Axes.set_xticks(ticks, labels=None, *, minor=False, **kwargs) sets tick locations and optionally their labels. The positions are values in the axis’s data units; the labels are the text shown at those positions. Labels do not determine where ticks go.
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xticks([0, 1, 2], labels=["first", "second", "third"])
Supply a one-dimensional, array-like sequence of positions. When you provide labels, give one for each position; the two sequences must have the same length. Matplotlib uses those labels as supplied. They can be formatted strings or multiline text if that makes the chart clearer. See the Matplotlib 3.10.9 Axes.set_xticks reference for the method specification.
Choose between fixed labels and formatter-generated labels
Use explicit labels for specific text
Pass both positions and labels when each tick needs a particular name, category, or date-like string. This fixes the locations and associates the text with them.
#1 Best Overall
Keep formatter-generated labels
Pass positions alone when you want the active axis formatter to choose the displayed text:
ax.set_xticks([0, 5, 10])
The formatter may not label every arbitrary location. For example, log-axis formatters commonly label decade ticks rather than all positions. If a formatter’s output does not suit the locations, choose an appropriate formatter or pass explicit labels.
Rank #2
Set minor ticks or remove ticks
By default, set_xticks sets major ticks. Set minor=True to target minor ticks instead:
ax.set_xticks([1, 3, 5], minor=True)
Pass an empty list to remove the selected set of ticks. This example removes major ticks:
ax.set_xticks([])
Control the visible x-axis range
Setting ticks can expand the view limits so all requested tick locations are visible, including locations outside the current range. If you want to keep or impose a different range, set it after setting the ticks:
ax.set_xticks([0, 5, 10])
ax.set_xlim(0, 8)
The official API reference explains that this expansion is intentional to prevent a requested tick from being hidden.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Style tick labels without destabilizing their positions
When passing labels to set_xticks, its **kwargs can set text properties. For general tick appearance—such as tick-label styling—use tick_params rather than relying on labels to establish positions.
Avoid using set_xticklabels by itself to assign text while leaving tick locations free to change. Labels depend on tick positions, so if the ticks move, labels can appear at unintended locations. Prefer setting positions and labels together with set_xticks(positions, labels). Matplotlib documents this warning in its Axes.set_xticklabels reference.
Free tools Windows power users keep installed
One-click scans. No signup required.
Quick Recap
Best Value
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.




