Matplotlib 使用 for 循环显示多个图像 [英] Matplotlib show multiple images with for loop
问题描述
我想在Matplotlib中显示多个图形.这是我的代码:
I want to display multiple figure in Matplotlib. Here's my code:
for i in range(8):
a = sitk.ReadImage("000%d.IMA"%(i+1))
...
plt.figure(i+1)
plt.imshow(a_np,cmap=plt.cm.gray)
但是,在此过程中将显示Figure(1)到Figure(7),但最后只有Figure(8)保留.如何同时查看所有数字?当我将环境更改为spyder时,我的环境是Ipython笔记本,这使我感到困惑.
However, figure(1) to figure(7) will show during the process but only figure(8) stay in the end. How can I see all of the figures at the same time? It confused me that my environment is Ipython notebook when I change the environment to spyder the result will be correct.
推荐答案
如果你想在 8 个不同的窗口中显示 8 个不同的图形,这里有一个可行的例子:
If you want 8 different figures in 8 different windows, here is an example that will work:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,10)
y = np.sin(x)
for i in range(8):
plt.plot(x,y)
plt.figure(i+1)
plt.show()
这将使用 x vs y
绘制 8 个不同的窗口,并且所有窗口将保持活动"状态,直到您关闭它们.
This will plot 8 different windows with x vs y
and all the windows will stay "alive" until you close them.
确保在 for
循环之外调用 plt.show()
Make sure you call plt.show()
outside the for
loop
这篇关于Matplotlib 使用 for 循环显示多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!