【发布时间】:2021-07-02 08:20:08
【问题描述】:
我正在将一些数据绘制为散点图,这些散点图覆盖在图像上。我想通过一次绘制一个散点来制作动画。这是我现在使用和编辑来自here 的答案的代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
x = random.sample(range(0, 287), 20)
y = random.sample(range(0, 380), 20)
size = [20 for x in range(20)]
colors = ["r" for x in range(20)]
cm = plt.get_cmap('jet')
fig = plt.figure(figsize=(18,9))
graph = plt.scatter([], [],marker='+')
url = 'https://raw.githubusercontent.com/kornelski/pngquant/master/test/img/test.png'
im = plt.imread(url)
def animate(i):
implot = plt.imshow(im)
graph.set_offsets(np.vstack((x[:i+1], y[:i+1])).T)
graph.set_sizes(size[:i])
graph.set_facecolors(colors[:i+1])
return graph
ani = FuncAnimation(fig, animate, repeat=False, interval=0.1)
plt.show()
有两件事我需要帮助。
- 我希望散点图的颜色根据第三个变量而改变,即使用
cmap。但是,set_facecolors 不接受这样的论点。 - 当我尝试使用
ani.save('files/animation.gif',writer='imagemagick', fps=60)保存动画时,我的 jupyter 笔记本崩溃了。
有人可以帮我吗?
【问题讨论】:
标签: python matplotlib matplotlib-animation