gpt4 book ai didi

python - 如何在每个子图中设置轴限制

转载 作者:行者123 更新时间:2023-12-04 00:27:00 25 4
gpt4 key购买 nike

我用这段代码创建了一个子图:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com