作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用这段代码创建了一个子图:
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/
我是一名优秀的程序员,十分优秀!