Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

How to Set the X-Axis Range and Tick Interval in Matplotlib

Generate the x-axis tick positions you want with set_xticks, then call set_xlim to keep the visible range exact.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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

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.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
ax.set_xticks(ticks, minor=True)
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

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
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

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.