[英]Display numpy array in a for loop using matplotlib imshow
I have a numpy array whose elements are updated in a for loop: 我有一个numpy数组,其元素在for循环中更新:
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
for t in range(0,10):
imshow(a)
for i in range(0,a.shape[0]):
for j in range(0,a.shape[1]):
a[i][j] += 1
I want to display the array at each iteration, but imshow() doesn't work, it just displays the image once the loop terminates. 我想在每次迭代时显示数组,但是imshow()不起作用,它只是在循环终止后显示图像。
ps. PS。 I'm using an Ipython notebook
我正在使用Ipython笔记本
I found different things on the web but none of them work on my computer (for example I tried to use matplotlib's animation module) 我在网上发现了不同的东西,但没有一个在我的电脑上工作(例如我试图使用matplotlib的动画模块)
The strange thing is that if I try to execute this example ( http://matplotlib.org/examples/animation/dynamic_image2.html ) using the standard python prompt everything works fine, while on the Ipython notebook it doesn't work. 奇怪的是,如果我尝试使用标准的python提示执行此示例( http://matplotlib.org/examples/animation/dynamic_image2.html ),一切正常,而在Ipython笔记本上它不起作用。 Can anyone explain me why?
谁能解释我为什么?
notes: 笔记:
Maybe I oversimplified my code; 也许我过分简化了我的代码;
I'm working on a forest-fire model, the array is a grid filled with 0 = empty site, 1 = tree, 2 = fire. 我正在研究森林火灾模型,数组是一个填充0 =空站点,1 =树,2 =火的网格。
At each time step (iteration): 在每个时间步骤(迭代):
I want to display the array using a colormap to visualize the evolution of my model 我想使用色图来显示数组,以可视化我的模型的演变
imshow(a)
will plot the values of the array a as pixel values, but it won't display the plot. imshow(a)
将数组a的值绘制为像素值,但不会显示图。 To view the image after each iteration of the for loop, you need to add show()
. 要在for循环的每次迭代后查看图像,您需要添加
show()
。
This should do what you want: 这应该做你想要的:
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
for t in range(0,10):
imshow(a)
show()
for i in range(0,a.shape[0]):
for j in range(0,a.shape[1]):
a[i][j] += 1
imshow
is for showing images in a plot. imshow
用于在图中显示图像。 Use print
to show a numpy array: 使用
print
显示numpy数组:
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
for t in range(0,10):
print a
# ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.