October 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 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 desk3 min

How to Set the Axis Range in Matplotlib with xlim and ylim

Use ax.set_xlim() and ax.set_ylim() to choose a Matplotlib plot's visible range, or use pyplot's xlim() and ylim() for the current Axes. Learn how limits interact with autoscaling and margins.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set a Matplotlib axis range with ax.set_xlim(left, right) or ax.set_ylim(bottom, top). For an Axes created with plt.subplots(), these methods make it clear which plot is being changed. In pyplot-style code, use plt.xlim(left, right) and plt.ylim(bottom, top) on the current Axes.

Set the x- and y-axis range on an Axes

For object-oriented Matplotlib code, set the limits after creating the Axes and plotting the data:

As an Amazon Associate I earn from qualifying purchases.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xlim(0, 10)   # show x values from 0 to 10
ax.set_ylim(-1, 1)   # show y values from -1 to 1

The first argument is the lower end of the range and the second is the upper end: left, right for x and bottom, top for y. These values define the visible window in data coordinates; they do not change the underlying data.

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

Choose between Axes methods and pyplot

Both interfaces set limits, but they target the plot differently:

Approach Example Targets
Axes methods ax.set_xlim(0, 10)
ax.set_ylim(-1, 1)
The specific Axes object stored in ax.
Pyplot functions plt.xlim(0, 10)
plt.ylim(-1, 1)
The current Axes, which pyplot selects implicitly.

Prefer the Axes methods when using fig, ax = plt.subplots(), especially if a figure has multiple subplots. Use pyplot functions when working in a pyplot-style workflow where the current Axes is the intended target. Calling plt.xlim() or plt.ylim() without arguments returns the current limits.

You can set both dimensions together with ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax)). Pyplot’s plt.axis([xmin, xmax, ymin, ymax]) also accepts both ranges in one call.

Change only one limit

To preserve one end of a range and change the other, use a named argument. For example, ax.set_ylim(top=5) changes only the upper y limit, while plt.ylim(bottom=1) changes only the lower y limit on the current Axes. The Axes.set_ylim API also provides an auto parameter for controlling autoscaling behavior; see the Axes.set_ylim API reference for its details.

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

Understand what happens to autoscaling

Matplotlib normally adjusts axis limits so plotted data remains visible. Setting an explicit limit turns autoscaling off for that axis by default. If you add more data afterward, it may fall outside the fixed view. To recalculate limits from the plotted data, call ax.autoscale(), which re-enables autoscaling and updates the view. See the Matplotlib autoscaling guide.

Use margins for automatic breathing room

If you want padding around the data but do not need an exact numeric window, keep automatic limits and adjust the margins instead. Matplotlib documents default x and y margins of 0.05 each—5% of the data span—in its version 3.11.1 autoscaling guide. Set them per Axes with, for example, ax.margins(x=0.1, y=0.2).

Some artists, including images displayed with imshow, have sticky edges that can prevent margins from expanding outward at the data boundary. If that behavior is not wanted, set ax.use_sticky_edges = False for the Axes.

Reverse an axis direction or set an aspect mode

Reverse the direction

Pass the bounds in reverse order to reverse the displayed direction. For example, ax.set_ylim(5000, 0) puts 5000 at the bottom and 0 at the top, a useful arrangement for depth values. This is a reversed range, not an error in the bounds.

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

Do not confuse aspect modes with limits

plt.axis also accepts modes such as 'equal', 'scaled', 'tight', 'auto', 'image' and 'square'. These control presentation or aspect behavior rather than simply specifying a fixed x and y range. In particular, axis('equal') can change limits so that the scales match; use explicit limits when a numeric window is the requirement. The options are described in the pyplot.axis API reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check the documentation for your installed version

The Matplotlib documentation pages available on October 4, 2026 showed version labels 3.11.1 for the autoscaling guide and 3.11.2 for the API references. If you need behavior pinned to a particular release, consult the documentation for the version installed in your environment.

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 *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver 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.