Matplotlib
Python
Data Visualization
Axis Limits
Plotting

How to set the axis limits in Matplotlib?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Setting axis limits in Matplotlib is one of the fastest ways to control what part of your data the viewer sees. You can do it through pyplot helpers such as plt.xlim() and plt.ylim(), or directly on an Axes object with set_xlim() and set_ylim(). The Axes methods are usually the clearer choice once a plot has more than one subplot.

Set Limits With xlim And ylim

For simple scripts using pyplot, the shortest approach is:

python
1import matplotlib.pyplot as plt
2
3x = [0, 1, 2, 3, 4]
4y = [0, 1, 4, 9, 16]
5
6plt.plot(x, y)
7plt.xlim(0, 4)
8plt.ylim(0, 20)
9plt.show()

This explicitly sets the visible x-axis and y-axis ranges.

Once you set limits manually, Matplotlib stops relying fully on autoscaling for that axis until you change it again.

Use Axes.set_xlim() And Axes.set_ylim() In Real Plots

When you work with subplots or reusable plotting code, use the Axes methods instead of the global pyplot state.

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots()
4ax.plot([0, 1, 2, 3], [10, 5, 7, 12])
5ax.set_xlim(0, 3)
6ax.set_ylim(0, 15)
7plt.show()

This is easier to read because the limits are attached to the specific axes object.

Set Both Axes At Once

Matplotlib also provides axis() as a compact shorthand.

python
1import matplotlib.pyplot as plt
2
3plt.plot([0, 1, 2], [2, 3, 5])
4plt.axis([0, 2, 0, 6])
5plt.show()

The four values are:

  • x minimum
  • x maximum
  • y minimum
  • y maximum

This is convenient for quick scripts, though set_xlim() and set_ylim() are usually clearer in larger programs.

Invert An Axis By Reversing The Limits

If you want an axis to run in the opposite direction, pass the larger value first.

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots()
4ax.plot([0, 1, 2, 3], [1, 2, 3, 4])
5ax.set_xlim(3, 0)
6plt.show()

This is a common pattern for countdown-style displays, depth plots, and some scientific charts.

Return To Autoscaling When Needed

If you set limits manually and later want Matplotlib to recompute them from the data, you can re-enable autoscaling.

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots()
4ax.plot([0, 1, 2, 3], [1, 4, 9, 16])
5ax.set_xlim(0, 2)
6ax.autoscale(enable=True, axis="x")
7plt.show()

This is useful in interactive plotting or in code that first focuses on one region and later wants to show the full data range again.

It is also handy when you update an existing axes object repeatedly in a loop and do not want old manual bounds to keep clipping new data.

Common Pitfalls

The most common mistake is setting limits on the wrong axes object when working with subplots. If you use pyplot helpers in a multi-axes figure, you may affect the current axes instead of the one you intended.

Another issue is forgetting that manual limits override autoscaling for that axis. If later data seems to disappear, the fixed limits may be the reason.

It is also easy to clip data accidentally by using overly tight bounds. That can make a plot look empty or misleading even when the data is valid.

Finally, be careful with reversed limits. They are useful, but if you set them unintentionally the plot may look wrong even though Matplotlib is obeying your instructions.

Summary

  • Use plt.xlim() and plt.ylim() for quick pyplot scripts.
  • Prefer ax.set_xlim() and ax.set_ylim() when working with explicit axes objects.
  • 'axis([xmin, xmax, ymin, ymax]) sets both axes at once.'
  • Reversing the order of limits inverts the axis direction.
  • Manual limits disable normal autoscaling for that axis until you change it again.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.