我用这段代码创建了一个子图:
f, axs =plt.subplots(2,3)
现在在一个循环中,我通过执行以下操作在每个子图上绘制一个图:
for i in range(5):
plt.gcf().get_axes()[i].plot(x,u)
是否有类似的代码来设置我正在访问的子图的轴限制?
最佳答案
是的,有,但让我们清理一下代码:
f, axs = plt.subplots(2, 3)
for i in range(5): #are you sure you don't mean 2x3=6?
axs.flat[i].plot(x, u)
axs.flat[i].set_xlim(xmin, xmax)
axs.flat[i].set_ylim(ymin, ymax)
使用 axs.flat
将您的 axs
(2,3) 轴数组转换为长度为 6 的平面可迭代轴。比 更容易使用plt.gcf().get_axes()
.
如果您只使用 range
语句来遍历轴并且从不使用索引 i
,则只需遍历 axs
。
f, axs = plt.subplots(2, 3)
for ax in axs.flat: #this will iterate over all 6 axes
ax.plot(x, u)
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
关于python - 如何在每个子图中设置轴限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30261054/