Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallSet 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.
Choose between Axes methods and pyplot
Both interfaces set limits, but they target the plot differently:
#1 Best Overall
| 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.
Rank #2
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.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →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.
Rank #3
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.
Rank #4
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.
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.
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.




