动画移动点错误

时间:2016-02-20 09:47:23

标签: python animation matplotlib

我是Python的初学者。我试图制作一个在水平方向上移动的点的动画。但是,当我运行代码时,我收到了以下错误:

TypeError: 'PathCollection' object is not iterable

我不知道如何修复它。

#----------------------------------------------------------------------
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

Acc_11 = [1,2,3,4,6,7]
lenAcc11 = len(Acc_11)
Acc_12 = [2,2,2,2,2,2]

# Scatter plot
fig = plt.figure(figsize = (5,5))
ax = plt.axes()
scat = ax.scatter([],[])

#initial func
def init():
    return scat 
#animation func
def ani (i):

    for i in range(0,lenAcc11):
        acc_11 = Acc_11[i]
        print (acc_11)
        acc_11_pos = Acc_12[i]
        print (acc_11_pos)
    scat = scat.set_data(acc_11,acc_11_pos)
    return scat
ani = FuncAnimation(fig, ani, init_func = init, interval = 10, blit =True)

plt.show()             


#--------------------------------------------------------------------------

2 个答案:

答案 0 :(得分:1)

有关散点图动画here的类似问题,我希望能解决您的问题。此外,散点图句柄没有set_data方法尝试使用set_array

答案 1 :(得分:0)

显示所有积分

这显示了所有要点:

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

Acc_11 = [1,2,3,4,6,7]
Acc_12 = [2,2,2,2,2,2]

# Scatter plot
fig = plt.figure(figsize = (5,5))


def ani(coords):
     return plt.scatter([coords[0]],[coords[1]], color='g')

def frames():
    for acc_11_pos, acc_12_pos in zip(Acc_11, Acc_12):
        yield acc_11_pos, acc_12_pos

ani = FuncAnimation(fig, ani, frames=frames, interval=1000)

plt.show()

仅显示一个移动点

这一次只显示一个点:

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

Acc_11 = [1,2,3,4,6,7]
Acc_12 = [2,2,2,2,2,2]

# Scatter plot
fig = plt.figure(figsize = (5,5))
axes = fig.add_subplot(111)
axes.set_xlim(min(Acc_11), max(Acc_11))
axes.set_ylim(min(Acc_12), max(Acc_12))

point, = axes.plot([Acc_11[0]],[Acc_12[0]], 'go')

def ani(coords):
    point.set_data([coords[0]],[coords[1]])
    return point

def frames():
    for acc_11_pos, acc_12_pos in zip(Acc_11, Acc_12):
        yield acc_11_pos, acc_12_pos

ani = FuncAnimation(fig, ani, frames=frames, interval=1000)

plt.show()