How to set the subplot axis range
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
Setting axis limits on subplots is one of the most important steps in making plots comparable and readable. In Matplotlib, each subplot is its own Axes object, so axis ranges are controlled per subplot unless you explicitly share or synchronize them. The right approach depends on whether you want independent views or consistent visual comparison across panels.
Set Axis Range on a Single Subplot
If you only need to control one subplot, use set_xlim and set_ylim on that axes object.
This is the most direct way to zoom into a region of interest.
Set Ranges for Multiple Subplots Individually
When you create several subplots, each one can have different limits.
This is useful when each subplot needs a different focus area.
Use Shared Limits for Fair Comparison
If subplots represent the same metric, consistent axis ranges are often more important than local detail. Otherwise, the viewer may overinterpret differences created by scaling rather than by the data.
sharex=True and sharey=True help keep scales aligned, but setting explicit limits still makes the intent unambiguous.
Apply Limits to Every Subplot in a Grid
For larger subplot grids, loop over the axes array.
This pattern avoids copy-paste and keeps scaling rules consistent.
Difference Between axis, set_xlim, and set_ylim
Matplotlib also lets you set both axes at once with axis.
The four values are [xmin, xmax, ymin, ymax]. This is compact, but set_xlim and set_ylim are usually clearer in production code.
Autoscale Versus Manual Limits
By default, Matplotlib autoscale chooses limits based on the data. That is convenient, but it can be misleading in comparative subplots.
Use autoscale when:
- each subplot is independent
- you want the data to fill the panel naturally
Use manual limits when:
- you need fair visual comparison
- you want stable plots across runs
- you are zooming into a known range
Stable axis rules are especially valuable in reporting pipelines where charts are compared week over week.
Invert or Log-Scale Axes When Needed
Axis range control also works with transformed axes.
Choose the scale first, then choose limits that make sense for that transformed view.
Common Pitfalls
One common mistake is setting limits on the wrong axes object when working with subplot arrays. Always call set_xlim and set_ylim on the intended subplot.
Another mistake is allowing each subplot to autoscale when the reader is supposed to compare magnitudes across panels. That produces misleading visuals.
Developers also forget that sharex and sharey affect linked axes behavior. Changing one subplot can update others.
Finally, very tight limits can clip markers, annotations, or error bars. Leave enough room for the plot elements, not just the raw data line.
Summary
- Use
set_xlimandset_ylimon each subplot to control the visible range. - Share and align limits when subplots should be compared directly.
- Loop over
axes.flatfor consistent scaling across grids. - Prefer explicit limits over autoscale in reporting and comparison plots.
- Check that the chosen limits support the story the chart is supposed to tell.
Related reading
- How to set weights in Keras with a numpy array?
- How to set weights in Keras with a numpy array?
- How to set xlim and ylim for a subplot
- 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?
.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.