How to set xlim and ylim for a subplot
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
In Matplotlib, each subplot has its own Axes object, and axis limits should normally be set on that object directly. The key is to use the object-oriented API such as ax.set_xlim(...) and ax.set_ylim(...), especially when you have several subplots in one figure and only want to change one of them.
Set Limits on a Single Subplot
When you create subplots, Matplotlib returns both the figure and one or more axes. To adjust one subplot, call methods on that specific axis.
This is clearer than using plt.xlim(...) because the axis you are modifying is explicit. That matters more once a figure contains multiple panels.
Work with Multiple Subplots Safely
If the figure has several subplots, index into the axes array and set limits only where needed.
Each subplot now has its own limits. This is the standard answer when different panels should emphasize different regions of the data.
Apply Common Limits Across Several Axes
Sometimes the goal is the opposite: all panels should share the same range to make visual comparison fair. In that case, loop through the axes and set identical limits.
This is especially useful for dashboards, model comparison plots, or before-and-after figures where differing scales could mislead the reader.
Understand Shared Axes and Autoscaling
Matplotlib also supports sharex=True and sharey=True when creating subplots.
With shared axes, changing limits on one subplot can affect the others. That is often desirable, but it can surprise people who expect each axis to stay independent.
Also remember that autoscaling may reset or override expectations if you set limits before plotting additional data. In practice, a reliable order is:
- plot the data
- set titles, labels, and legends
- set final limits
That makes the resulting figure easier to reason about.
Alternative Syntax
Matplotlib offers a compact axis method too:
The order is xmin, xmax, ymin, ymax. It works, but set_xlim and set_ylim are often easier to read and less error-prone in review.
Use Limits Intentionally with Log Scales and Comparisons
Axis limits also matter when plots use log scaling or when one subplot is meant to highlight a narrow region of interest. If you switch to ax.set_yscale("log"), make sure the chosen limits are valid for the scale and still show the data you care about.
In comparison-heavy figures, explicitly setting the same x or y range across panels is often more important than the plotting code itself. Readers will compare heights and slopes visually, so inconsistent axis limits can create misleading differences even when the underlying data is similar.
Common Pitfalls
- Calling
plt.xlim(...)in a multi-subplot figure and accidentally affecting the wrong current axis. - Setting limits before plotting, then being surprised when later plotting changes autoscaling behavior.
- Forgetting that
sharexandshareycan couple several subplots together. - Applying different axis ranges across panels when the real goal is fair visual comparison.
- Using
axis([...])without remembering the required ordering of the four values.
Summary
- Set subplot limits on the specific
Axesobject withset_xlimandset_ylim. - Index the right axis when a figure contains multiple subplots.
- Loop over axes when several panels should share the same limits.
- Be careful with shared axes and autoscaling.
- Prefer the object-oriented API because it scales better than pyplot globals.
Related reading
- How to shift a column in Pandas DataFrame
- How to show all columns' names on a large pandas dataframe?
- How to show PIL Image in ipython notebook
- How to show training and predicted values on Tensorboard using python
- How to shuffle two numpy datasets using TensorFlow 2.0?
- How to simplify Tensorboard graph with shared variables?
- How to skip the headers when processing a csv file using Python?
- How to smooth a curve for a dataset
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.