Python 所有子批次的等轴标签和范围
假设我正在绘制一个包含4个子图的图像,如下所示:Python 所有子批次的等轴标签和范围,python,matplotlib,Python,Matplotlib,假设我正在绘制一个包含4个子图的图像,如下所示: import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(221) plt.xlim(0, 10) plt.ylim(0, 20) plt.xlabel('Label_x') plt.ylabel('Label_y') plt.plot(something_1) ax2 = fig.add_subplot(222) plt.xlim(0, 10) plt.
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(221)
plt.xlim(0, 10)
plt.ylim(0, 20)
plt.xlabel('Label_x')
plt.ylabel('Label_y')
plt.plot(something_1)
ax2 = fig.add_subplot(222)
plt.xlim(0, 10)
plt.ylim(0, 20)
plt.xlabel('Label_x')
plt.ylabel('Label_y')
plt.plot(something_2)
ax3 = fig.add_subplot(223)
plt.xlim(0, 10)
plt.ylim(0, 20)
plt.xlabel('Label_x')
plt.ylabel('Label_y')
plt.plot(something_3)
ax4 = fig.add_subplot(224)
plt.xlim(0, 10)
plt.ylim(0, 20)
plt.xlabel('Label_x')
plt.ylabel('Label_y')
plt.plot(something_4)
plt.show()
正如您所看到的,子地块之间唯一发生变化的是最后一行(即:正在绘制的内容),但轴的标签和范围保持不变
如何一次性设置轴标签和范围并将其应用于所有子批次?使用
plt。子批次
:
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()
...:
使用
plt.子批次
:
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()
...:
使用
plt.子批次
:
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()
...:
使用
plt.子批次
:
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()
...:
我知道这是前一段时间发布的,但是对于现在看到这篇文章的人,我想在@zhangxaochen答案中添加一点,即在
plt.subplot
命令中也有参数sharex
和sharey
。因此,如果您愿意,您可以使用:
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()
我知道这是前一段时间发布的,但是对于现在看到这篇文章的人,我想在@zhangxaochen答案中添加一点,即在
plt.subplot
命令中也有参数sharex
和sharey
。因此,如果您愿意,您可以使用:
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()
我知道这是前一段时间发布的,但是对于现在看到这篇文章的人,我想在@zhangxaochen答案中添加一点,即在
plt.subplot
命令中也有参数sharex
和sharey
。因此,如果您愿意,您可以使用:
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()
我知道这是前一段时间发布的,但是对于现在看到这篇文章的人,我想在@zhangxaochen答案中添加一点,即在
plt.subplot
命令中也有参数sharex
和sharey
。因此,如果您愿意,您可以使用:
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()