Matplotlib多行动画多行

Matplotlib多行动画多行,matplotlib,jquery-animate,Matplotlib,Jquery Animate,我一直在研究如何为一条飞行路线设置多条线的动画。该对象指示我读取多个GPS文件时间同步,并根据时间设置每条路径的动画。我发现如何在动画函数中使用append设置一行的动画。现在我需要添加第二个和第三个,因为导入的文件越多 我知道问题出在如何使用这些行执行set_数据。我看过多个示例,但我不明白设置多条线路需要什么结构。是的,我是个新手 fig = plt.figure() ax1 = plt.axes(xlim=(-108, -104), ylim=(31,34)) line, = ax1.pl

我一直在研究如何为一条飞行路线设置多条线的动画。该对象指示我读取多个GPS文件时间同步,并根据时间设置每条路径的动画。我发现如何在动画函数中使用append设置一行的动画。现在我需要添加第二个和第三个,因为导入的文件越多

我知道问题出在如何使用这些行执行set_数据。我看过多个示例,但我不明白设置多条线路需要什么结构。是的,我是个新手

fig = plt.figure()
ax1 = plt.axes(xlim=(-108, -104), ylim=(31,34))
line, = ax1.plot([], [], lw=2)
plt.xlabel('Longitude')
plt.ylabel('Latitude')



plotlays, plotcols = [2], ["black","red"]
lines = []
for index in range(2):
    lobj = ax1.plot([],[],lw=2,color=plotcols[index])[0]
    lines.append(lobj)


def init():
    for line in lines:
        line.set_data([],[])
    return lines

x1,y1 = [],[]
x2,y2 = [],[]

frame_num = len(gps_data[0])

# animation function.  This is called sequentially
def animate(i):

    x = gps_data[0][i,3]
    y = gps_data[0][i,2]
    x1.append(x)
    y1.append(y)

    x = gps_data[1][i,3]
    y = gps_data[1][i,2]
    x2.append(x)
    y2.append(y)

    #X = np.array(x1, x2)
    #Y = np.array(y1, y2)

    #for index in range(0,1):
    for lnum,line in enumerate(lines):
        line.set_data([x1,y1, x2,y2])
    return lines,


# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=frame_num, interval=1, blit=True)


plt.show()

的Matplotlib文档解释了set_数据的工作原理。它“接受:2D数组(行是x,y)或两个1D数组。”它也适用于列表。你给了它一个四元素列表。您需要分别设置每行的x和y数据。下面我举了一个假数据的例子

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

fig = plt.figure()
ax1 = plt.axes(xlim=(-108, -104), ylim=(31,34))
line, = ax1.plot([], [], lw=2)
plt.xlabel('Longitude')
plt.ylabel('Latitude')

plotlays, plotcols = [2], ["black","red"]
lines = []
for index in range(2):
    lobj = ax1.plot([],[],lw=2,color=plotcols[index])[0]
    lines.append(lobj)


def init():
    for line in lines:
        line.set_data([],[])
    return lines

x1,y1 = [],[]
x2,y2 = [],[]

# fake data
frame_num = 100
gps_data = [-104 - (4 * random.rand(2, frame_num)), 31 + (3 * random.rand(2, frame_num))]


def animate(i):

    x = gps_data[0][0, i]
    y = gps_data[1][0, i]
    x1.append(x)
    y1.append(y)

    x = gps_data[0][1,i]
    y = gps_data[1][1,i]
    x2.append(x)
    y2.append(y)

    xlist = [x1, x2]
    ylist = [y1, y2]

    #for index in range(0,1):
    for lnum,line in enumerate(lines):
        line.set_data(xlist[lnum], ylist[lnum]) # set data for each line separately. 

    return lines

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=frame_num, interval=10, blit=True)


plt.show()

非常感谢。我用你的代码画了一个时间序列。但是我必须删除
blit=True
,因为它防止了x轴上的值被更新。@Molly,如何根据代码中定义的绘图添加图例?我得到错误:x=gps_数据[0][0,I]类型错误:“int”对象不可下标