How do you update inline images in ipython?

Edit: My question is not about the "animation" as such. My question here is simply about how to constantly show a new inline image in a for loop on an Ipython laptop.

In essence, I would like to show the updated image in the same place inside, and update it in a loop to show. So my code 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 at the moment it's just ..., well, it shows the first image, and then it never changes.

Instead, I would just like to show the updated image at each iteration embedded in the same place. How should I do it? Thank.

+4
source share
2

figure.canvas.draw() , - . ( ). :

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)

IPython, .

+2

, . matplotlib . , . matplotlib.animation.FuncAnimation, , , .

:

%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)

. , , %matplotlib notebook.

EDIT: , , . animation_demo " " plt.pause(0.5), .

+2

Source: https://habr.com/ru/post/1688762/


All Articles