如何在每个子图中设置轴限制 [英] How to set axes limits in each subplot
问题描述
我用这个代码创建了一个子图:
I created a subplot figure with this code:
f, axs =plt.subplots(2,3)
现在在一个循环中,我通过执行以下操作在每个子图上绘制一个图:
Now on a loop I make a plot on each subplot by doing:
for i in range(5):
plt.gcf().get_axes()[i].plot(x,u)
是否有类似的代码来设置我正在访问的子图的轴限制?
Is there a similar code to set the axis limits of the subplot I'm accessing ?
推荐答案
是的,有,但让我们在处理代码时清理一下:
Yes, there is, but let's clean up that code while we're at it:
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()
.
Using axs.flat
transforms your axs
(2,3) array of Axes into a flat iterable of Axes of length 6. Much easier to use than plt.gcf().get_axes()
.
如果仅使用 range
语句在轴上进行迭代,而从不使用索引 i
,则仅在 axs
上进行迭代./p>
If you only are using the range
statement to iterate over the axes and never use the index i
, just iterate over 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)
这篇关于如何在每个子图中设置轴限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!