我有一个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我想在每次迭代中显示数组,但imshow()不起作用,它只是在循环终止时显示图像。
PS。 我正在使用Ipython笔记本
我在网上发现了不同的东西,但是他们都没有在我的电脑上工作(例如我试图使用matplotlib的动画模块)
奇怪的是,如果我尝试使用标准Python提示符执行此示例( http://matplotlib.org/examples/animation/dynamic_image2.html ),则一切正常,而在Ipython笔记本计算机上则无法正常工作。 任何人都能解释我为什么?
笔记:
也许我简化了我的代码;
我正在研究森林火灾模型,数组是一个填充0 =空站点,1 =树,2 =火的网格。
在每个时间步骤(迭代)中:
一棵树被丢弃在一个随机选择的网站上,如果该网站是免费的,则树被种植 一棵树以概率f点燃我想使用颜色映射来显示数组以可视化我的模型的演变
I have a numpy array whose elements are updated in a for loop:
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] += 1I want to display the array at each iteration, but imshow() doesn't work, it just displays the image once the loop terminates.
ps. I'm using an Ipython notebook
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)
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. 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.
At each time step (iteration):
a tree is dropped on a randomly chosen site and if the site is free the tree is planted a tree ignites with a probability fI want to display the array using a colormap to visualize the evolution of my model
最满意答案
imshow(a)会将数组a的值绘制为像素值,但不会显示绘图。 要在for循环的每次迭代后查看图像,您需要添加show() 。
这应该做你想做的事情:
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] += 1imshow(a) will plot the values of the array a as pixel values, but it won't display the plot. To view the image after each iteration of the for loop, you need to add 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更多推荐
发布评论