Skip to content Skip to sidebar Skip to footer

Equal Axis Labels And Ranges For All Subplots

Say I'm plotting an image with 4 subplots like so: import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(221) plt.xlim(0, 10) plt.ylim(0, 20) plt.xlabel('Label_

Solution 1:

use plt.subplots:

In [36]: import matplotlib.pyplot as plt
    ...: fig, axes=plt.subplots(2, 2)
    ...: for ax in axes.ravel(): #ravel axes to a flattened array 
    ...:     ax.set_xlim(0, 10)
    ...:     ax.set_ylim(0, 20)
    ...:     ax.set_xlabel('Label_x')
    ...:     ax.set_ylabel('Label_y')
    ...: plt.show()
    ...: 

Solution 2:

I know this was posted a while ago, but for anyone seeing this post now, I would add to @zhangxaochen answer to note that in plt.subplots command also has the parameters sharex and sharey. So if you want, you could just use:

import matplotlib.pyplot as plt
fig, axes=plt.subplots(2, 2, sharex = True, sharey = True)
for ax in axes.ravel(): #ravel axes to a flattened array # Do plotting here
plt.show()

Post a Comment for "Equal Axis Labels And Ranges For All Subplots"