subplot
axis range
matplotlib
data visualization
plotting techniques

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.

Practice ML system design

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.

python
1import numpy as np
2import matplotlib.pyplot as plt
3
4x = np.linspace(0, 10, 200)
5y = np.sin(x)
6
7fig, ax = plt.subplots()
8ax.plot(x, y)
9ax.set_xlim(2, 8)
10ax.set_ylim(-0.5, 0.5)
11ax.set_title("Single subplot with custom axis range")
12
13plt.show()

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.

python
1import numpy as np
2import matplotlib.pyplot as plt
3
4x = np.linspace(0, 10, 200)
5
6fig, axes = plt.subplots(1, 2, figsize=(10, 4))
7
8axes[0].plot(x, np.sin(x))
9axes[0].set_xlim(0, 10)
10axes[0].set_ylim(-1.2, 1.2)
11axes[0].set_title("Full range")
12
13axes[1].plot(x, np.sin(x))
14axes[1].set_xlim(2, 4)
15axes[1].set_ylim(-1.0, 1.0)
16axes[1].set_title("Zoomed view")
17
18plt.tight_layout()
19plt.show()

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.

python
1import numpy as np
2import matplotlib.pyplot as plt
3
4x = np.linspace(0, 10, 200)
5y1 = np.sin(x)
6y2 = 0.5 * np.sin(x)
7
8fig, axes = plt.subplots(1, 2, figsize=(10, 4), sharex=True, sharey=True)
9
10axes[0].plot(x, y1)
11axes[0].set_title("Amplitude 1.0")
12
13axes[1].plot(x, y2)
14axes[1].set_title("Amplitude 0.5")
15
16for ax in axes:
17    ax.set_xlim(0, 10)
18    ax.set_ylim(-1.2, 1.2)
19
20plt.tight_layout()
21plt.show()

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.

python
1import numpy as np
2import matplotlib.pyplot as plt
3
4x = np.linspace(0, 10, 200)
5
6fig, axes = plt.subplots(2, 2, figsize=(8, 6))
7
8for i, ax in enumerate(axes.flat, start=1):
9    ax.plot(x, np.sin(i * x / 3))
10    ax.set_xlim(0, 10)
11    ax.set_ylim(-1.2, 1.2)
12    ax.set_title(f"Plot {i}")
13
14plt.tight_layout()
15plt.show()

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.

python
1import matplotlib.pyplot as plt
2import numpy as np
3
4x = np.linspace(0, 10, 100)
5y = np.cos(x)
6
7fig, ax = plt.subplots()
8ax.plot(x, y)
9ax.axis([1, 7, -0.5, 1.0])
10
11plt.show()

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.

python
1import numpy as np
2import matplotlib.pyplot as plt
3
4x = np.linspace(1, 100, 200)
5y = x ** 2
6
7fig, ax = plt.subplots()
8ax.plot(x, y)
9ax.set_xscale("log")
10ax.set_xlim(1, 100)
11ax.set_ylim(0, 10000)
12
13plt.show()

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_xlim and set_ylim on each subplot to control the visible range.
  • Share and align limits when subplots should be compared directly.
  • Loop over axes.flat for 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
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.