Setting the same axis boundaries for all subtasks in matplotlib

This problem seems simple enough, but I can not find a pythonic way to solve it. I have several (four) subnets that should have the same xlim and ylim . Iterate over all subtasks Γ  la

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

- This is not the most pleasant way to do things, especially for 2x2 subheadings - that’s what I really mean. I am looking for something like plt.all_set_xlim(xval1, xval2) .

Please note that I do not want to change anything (ticks and tags must be controlled separately).

EDIT: I am using the plt.subplots(2, 2) wrapper. After dienzs answer, I tried plt.subplots(2, 2,sharex=True, sharey=True) - almost correctly, but now the ticks have disappeared, except for the left and bottom lines.

+6
source share
4 answers

You can use common axes that will share x and / or y-constraints, but allow you to adjust the axes in any other way.

+3
source

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) 
+1
source

It's not elegant at all, but it worked out ...

 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) 
0
source

Set the property of the Artist object using 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. xlim = (0, 100) ylim = (-100, 100) # Setting the values for all axes. plt.setp(ax, xlim=xlim, ylim=ylim) 
0
source

All Articles