October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Matplotlib set_xticks: Set X-Axis Tick Positions and Labels

Use Matplotlib’s set_xticks to place x-axis ticks at chosen values with matching labels, or let the active formatter generate the text. Learn how tick settings affect axis limits.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.