繁体   English   中英

如何在Matplotlib中将图添加到动画列表中?

[英]How do I add a plot to an animated list of images in Matplotlib?

我从这里获取了以下代码。 plt.imshow()我想为每个“框架”绘制一个位于不同位置的红点。

如何使用plt.plot()唯一的plt.imshow()

下面的代码保留了先前“框架”中先前绘制的红点。

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

fig = plt.figure()


def f(x, y):
    return np.sin(x) + np.cos(y)

x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
# ims is a list of lists, each row is a list of artists to draw in the
# current frame; here we are just animating one artist, the image, in
# each frame
ims = []
for i in range(60):
    x += np.pi / 15.
    y += np.pi / 20.
    im = plt.imshow(f(x, y), animated=True)

    # my added code here
    plt.plot(pos_x, pos_y, 'ro') # pos_x, pos_y change on every iteration

    ims.append([im])

ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
                                repeat_delay=1000)

# ani.save('dynamic_images.mp4')

plt.show()

在动画对象列表中包含通过plot创建的Line2D对象。

line, = plt.plot(pos_x, pos_y, 'ro')

ims.append([im, line])

暂无
暂无

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

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