简体   繁体   English

你如何在Ipython中更新内嵌图像?

[英]How do you update inline images in Ipython?

Edit: My question is not in regards to an "animation" per se. 编辑:我的问题不是关于“动画”本身。 My question here, is simply about how to continuously show , a new inline image , in a for loop, within an Ipython notebook. 我在这里的问题只是关于如何在Ipython笔记本中连续显示一个新的 内嵌 图像 ,在for循环中。

In essence, I would like to show an updated image, at the same location, inline, and have it update within the loop to show. 本质上,我想在同一位置,内联显示更新的图像,并在循环内更新以显示。 So my code currently looks something like this: 所以我的代码目前看起来像这样:

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from IPython import display
%matplotlib inline  

fig, ax = plt.subplots(nrows = 1, ncols = 1, figsize=(10, 10))
for ii in xrange(10):
    im = np.random.randn(100,100)
    ax.cla()
    ax.imshow(im, interpolation='None')
    ax.set_title(ii)
    plt.show()

The problem is that this currently just..., well, shows the first image, and then it never changes. 问题是,目前这只是...,好吧,显示第一个图像,然后它永远不会改变。

Instead, I would like it to simply show the updated image at each iteration, inline, at the same place. 相反,我希望它只是在每次迭代,内联,在同一个地方显示更新的图像。 How do I do that? 我怎么做? Thanks. 谢谢。

You can call figure.canvas.draw() each time you append something new to the figure. 每次向图中附加新内容时,都可以调用figure.canvas.draw() This will refresh the plot (from here ). 这将刷新情节(从这里 )。 Try: 尝试:

import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from IPython import display
from time import sleep

fig = plt.figure()
ax = fig.gca()
fig.show()

for ii in range(10):
    im = np.random.randn(100, 100)
    plt.imshow(im, interpolation='None')
    ax.set_title(ii)
    fig.canvas.draw()
    sleep(0.1)

I could not test this in an IPython Notebook, however. 但是,我无法在IPython笔记本中测试它。

I am not sure that you can do this without animation. 我不确定你能否在没有动画的情况下做到这一点。 Notebooks capture the output of matplotlib to include in the cell once the plotting is over. 一旦绘图结束,笔记本就会捕获matplotlib的输出以包含在单元格中。 The animation framework is rather generic and covers anything that is not a static image. 动画框架非常通用,涵盖了任何非静态图像。 matplotlib.animation.FuncAnimation would probably do what you want. matplotlib.animation.FuncAnimation可能会做你想要的。

I adapted your code as follows: 我调整了你的代码如下:

%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation

f = plt.figure()
ax = f.gca()

im = np.random.randn(100,100)
image = plt.imshow(im, interpolation='None', animated=True)

def function_for_animation(frame_index):
    im = np.random.randn(100,100)
    image.set_data(im)
    ax.set_title(str(frame_index))
    return image,

ani = matplotlib.animation.FuncAnimation(f, function_for_animation, interval=200, frames=10, blit=True)

Note: You must restart the notebook for the %matplotlib notebook to take effect and use a backend that supports animation. 注意:您必须重新启动笔记本以使%matplotlib notebook生效并使用支持动画的后端。

EDIT: There is normally a way that is closer to your original question but it errors on my computer. 编辑:通常有一种方法更接近你原来的问题,但它在我的电脑上出错。 In the example animation_demo there is a plain "for loop" with a plt.pause(0.5) statement that should also work. 在示例animation_demo中,有一个简单的“for循环”,其中包含plt.pause(0.5)语句,该语句也应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM