【发布时间】:2019-06-30 11:05:22
【问题描述】:
我正在尝试根据一组点的跟踪数据创建动画电影 - 5 个点。
数据集格式
5 个点的 x,y 坐标位于 Pandas 数据框中,共有 10 列 - 每对列是任意给定时间点的 x,y 坐标 >。每行给出 1/10 秒后点的位置。示例如下所示 -
x1 y1 x2 y2 x3 y3 x4 y4 x5 y5
0 1 3 2 1 9 1 1 1 8 1
1 5 7 5 5 7 5 5 8 4 5
2 7 1 7 9 7 5 4 8 0 7
3 3 5 4 6 3 6 9 7 3 0
绘图函数
我用来从点创建动画的函数是这样的:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
df = pd.read_csv(file)
def _update_plot(i, fig, scat):
scat.set_offsets(([dff.iloc[i,0], dff.iloc[i,1]], [dff.iloc[i,2],dff.iloc[i,3]], [dff.iloc[i,4],dff.iloc[i,5]], [dff.iloc[i,6],dff.iloc[i,7]], [dff.iloc[i,8],dff.iloc[i,9]]))
return scat,
anim = animation.FuncAnimation(fig, _update_plot, fargs = (fig, scat),
frames = len(dff.index)-1, interval = 100)
这对我有用,没有任何问题。但是,我现在想用这些点创建一个 Voronoi 动画。我认为这就像从scipy.spatial 导入后将其添加到绘图函数一样简单:
vor = Voronoi(points) ##points are the rows of the `df`
voronoi_plot_2d(vor)
然而,这并没有成功。没有任何错误,但没有任何改变。我被引导相信创建动画比我尝试的要复杂一些。
我想到的最终输出将与this video 非常相似(当然,更多点)。 如何使用 matplotlib 创建动画?任何帮助将不胜感激。
【问题讨论】:
标签: python matplotlib animation voronoi