如何在子图中循环遍历轴 [英] How to loop over the axes in a subplot

查看:63
本文介绍了如何在子图中循环遍历轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环,可生成15张图像.我创建了一个子图(5,3):

I have a loop that generates 15 images. I created a subplot(5,3):

图,轴= plt.subplots(5,3)对于范围内的我(28,43):(...).plot(...,ax = axes [?,?])

我希望图像按顺序出现:轴[0,0],轴[0,1],轴[0,2],轴[1,0] ...

I would like that the images appears in sequence: axes[0,0], axes[0,1], axes[0,2], axes[1,0]...

我应该把哪个参数放在轴上?

Which argument should I put on axes?

还是有必要在实际内部创建另一个循环?

Or is it necessary to create another loop inside the actual?

推荐答案

当然,您可以简单地计算出数字.

Of course you may simply calculate the numbers.

fig, axes = plt.subplots(5,3)
for i in range(28,43):
      (...).plot(..., ax=axes[(i-28)//3,(i-28)%3])

但是通常情况下,您宁愿在轴上循环

But usually you would rather loop over the axes

fig, axes = plt.subplots(5,3)
for i, ax in enumerate(axes.flat):
    (...).plot(..., ax=ax)

这篇关于如何在子图中循环遍历轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆