Setting the same axis limits for all subplots
Asked Answered
L

5

40

This problem seems simple enough, but I can't find a pythonic way to solve it. I have several (four) subplots that are supposed to have the same xlim and ylim. Iterating over all subplots à la

f, axarr = plt.subplots(4)
for x in range(n):
    axarr[x].set_xlim(xval1, xval2)
    axarr[x].set_ylim(yval1, yval2)

isn't the nicest way of doing things, especially for 2x2 subplots – which is what I'm actually dealing with. I'm looking for something like plt.all_set_xlim(xval1, xval2).

Note that I don't want anything else to change (ticks and labels should be controlled separately).

EDIT: I'm using the plt.subplots(2, 2) wrapper. Following dienzs answer, I tried plt.subplots(2, 2,sharex=True, sharey=True) – almost right, but now the ticks are gone except for the left and bottom row.

Lumbye answered 23/6, 2015 at 15:24 Comment(0)
E
54

Set the xlim and ylim properties on the Artist object by matplotlib.pyplot.setp() https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.setp.html

# Importing matplotlib.pyplot package.
import matplotlib.pyplot as plt

# Assigning 'fig', 'ax' variables.
fig, ax = plt.subplots(2, 2)

# Defining custom 'xlim' and 'ylim' values.
custom_xlim = (0, 100)
custom_ylim = (-100, 100)

# Setting the values for all axes.
plt.setp(ax, xlim=custom_xlim, ylim=custom_ylim)
Emulsify answered 18/6, 2019 at 10:2 Comment(1)
more explanation is needed here. This was not helpfulWaltman
V
8

You could use shared axes which will share the x- and/or y-limits, but allow to customise the axes in any other way.

Vexation answered 23/6, 2015 at 15:35 Comment(1)
Well, that still involves giving every subplot the sharex or sharey option separately. I should have mentioned I'm using the plt.subplots() wrapper. Now that wrapper does take sharex=True as an optional argument, but that gets rid of the axis ticks and I can't see an option in the documentation that restores them easily. Of course this question is all about simple and terse code. I'm trying to avoid repetitive sections – think of a 8x2 subplot figure or something.Lumbye
S
7

You can try this.

#set same x,y limits for all subplots
fig, ax = plt.subplots(2,3)
for (m,n), subplot in numpy.ndenumerate(ax):
    subplot.set_xlim(xval1,xval2)
    subplot.set_ylim(yval1,yval2)
Styrene answered 16/3, 2017 at 10:57 Comment(0)
I
5

If you have multiple subplots, i.e.

fig, ax = plt.subplots(4, 2)

You can use it. It gets limits of y ax from first plot. If you want other subplot just change index of ax[0,0].

plt.setp(ax, ylim=ax[0,0].get_ylim())
Isham answered 24/5, 2020 at 14:8 Comment(4)
That is exactly what the top answer says.Pitre
It's not. In this example, I get the limits from multiple plots, not customized range.Isham
Oh I see. You extend the ylim of the first plot to the others.Pitre
Thanks this is exactly what I needed, custom limit not sized oneMclain
L
1

This is not at all elegant, but it worked for me...

fig, axes = plt.subplots(6, 3, sharex=True)
axes[0, 0].set_xlim(right=10000) # sharex=True propagates it to all plots
for i in range(6):
    for j in range(3):
        axes[i, j].plot('Seconds', df.columns[2+3*i+j], data=df)  # your plot instructions
plt.subplots_adjust(wspace=.5, hspace=0.2)
Laird answered 22/7, 2018 at 22:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.