用于绘制三个点的语法'使用FuncAnimation进行移动

时间:2016-11-20 14:56:03

标签: python animation matplotlib plot

我的代码:

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

def animate(i):
    ax.set_data(ax.scatter(ptx1, pty1, ptz1, c='red'),
        ax.scatter(ptx2, pty2, ptz2, c='blue'),
        ax.scatter(ptx3, pty3, ptz3, c='green'))

ani = FuncAnimation(fig, animate, frames=10, interval=200)

plt.show()

我试图绘制三点的运动。每个ptx / y / z / 1/2/3是一个浮点列表,给出了点的坐标。我只是不确定如何使用FuncAnimation来动画我的点数。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

简单的例子。 animate被多次调用,每当您必须使用不同的数据来查看动画时。

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random

# create some random data    
ptx1 = [random.randint(0,100) for x in range(20)]
pty1 = [random.randint(0,100) for x in range(20)]

fig = plt.figure()
ax = fig.add_subplot(111)

def animate(i):
    # use i-th elements from data
    ax.scatter(ptx1[:i], pty1[:i], c='red')

    # or add only one element from list
    #ax.scatter(ptx1[i], pty1[i], c='red')

ani = FuncAnimation(fig, animate, frames=20, interval=500)

plt.show()