我想创建一个动画,我的数据点将逐渐出现在我的图形上,并在所有数据点都出现时冻结。我已经看过了使用相关性,我只是不太确定如何仅使用单个点本身
这不会显示任何特别有用的东西,但我认为它看起来会很酷,因为我正在尝试在地图上可视化一些位置数据
我知道这不是很清楚,所以请澄清,我不太确定如何很好地表达我的问题。
谢谢
发布于 2015-10-22 15:26:12
matplotlib.animation.FuncAnimation是适合您的工具。首先创建一个空图,然后在函数中逐渐向其添加数据点。下面这段代码将对此进行说明:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
x = np.arange(10)
y = np.random.random(10)
fig = plt.figure()
plt.xlim(0, 10)
plt.ylim(0, 1)
graph, = plt.plot([], [], 'o')
def animate(i):
graph.set_data(x[:i+1], y[:i+1])
return graph
ani = FuncAnimation(fig, animate, frames=10, interval=200)
plt.show()结果(保存为gif文件)如下所示:

编辑:要使动画在matplotlib窗口中完成时看起来已停止,您需要将其设置为无限大(忽略FuncAnimation中的frames参数),并将帧计数器设置为帧序列中的最后一个数字:
def animate(i):
if i > 9:
i = 9
graph.set_data(x[:i+1], y[:i+1])
return graph
ani = FuncAnimation(fig, animate, interval=200)或者,您可以根据对this问题的回答,将FuncAnimation中的repeat参数设置为False。
EDIT 2:要为散点图设置动画,您需要一大堆其他方法。一段代码胜过千言万语:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
x = np.arange(10)
y = np.random.random(10)
size = np.random.randint(150, size=10)
colors = np.random.choice(["r", "g", "b"], size=10)
fig = plt.figure()
plt.xlim(0, 10)
plt.ylim(0, 1)
graph = plt.scatter([], [])
def animate(i):
graph.set_offsets(np.vstack((x[:i+1], y[:i+1])).T)
graph.set_sizes(size[:i+1])
graph.set_facecolors(colors[:i+1])
return graph
ani = FuncAnimation(fig, animate, repeat=False, interval=200)
plt.show()https://stackoverflow.com/questions/33275189
复制相似问题