AttributeError:“列表”对象没有属性“ get_zorder”

时间:2019-10-07 04:47:28

标签: python-3.x matplotlib animation projectile

我正在尝试在matplotlib.animation的帮助下对弹丸运动进行动画处理,但是我遇到了一些错误。请帮我解决一下这个。 非常感谢

我尝试通过Internet进行搜索,并且确实实现了一些类似问题的解决方案,但是代码仍然出现错误

"/login/token.php?username=YOUR_FORM_USERNAME&password=YOUR_FORM_PASSWORD&service=moodle_mobile_app"

key = lambda x:x.get_zorder())

AttributeError:“列表”对象没有属性“ get_zorder”

2 个答案:

答案 0 :(得分:0)

我认为问题在于,在proj_animation()中您将返回两个列表的元组,但是FuncAnimation()在直接寻找可迭代的绘制对象。最快的解决方法是将dot_listline_list串联并返回串联列表。 Nb这也应该在您的初始化函数中完成。

答案 1 :(得分:0)

我试图使用子图绘制传感器数据,但遇到了同样的错误。我修复它的方法是只返回一个变量或一个列表。在动画函数中,我返回了一个列表列表,我只是将这个列表列表展平了,代码就可以工作了。适合您的代码的解决方案如下:

import matplotlib as mat
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as mat_anim

u = 5                                                                
g = 9.8       
theta_degree = np.rad2deg([0, np.pi/6, np.pi/4, np.pi/3, np.pi/2])
theta_rad = [0, np.pi/6, np.pi/4, np.pi/3, np.pi/2]
fr = 100
print(1)

def projectile_range():
    # calculate projectile range
    rng = ((u**2)*(np.sin(np.multiply(2.0, theta_rad))))/g
    return rng

def max_height():
    # calculate maximum height of projectile
    max_ht = ((u*np.sin(theta_rad))**2)/(2*g)
    return max_ht

def projectile():
    # calculating projectile path
    r = projectile_range()
    for j in range(len(r)):
        x = np.linspace(0, r[j], 100)
        y.append(x*(np.tan(theta_rad[j])) - ((0.5*g*(x**2))/(u*np.cos(theta_rad[j]))**2))
    return y

fig1, ax1 = plt.subplots(1,1)
fig1.suptitle("Projectile Motion Range", fontsize = 10)
ax1.set_xlim([0, round(max(projectile_range()))+1])
ax1.set_ylim([0, round(max(max_height()))+1])
# ax_range, = ax1.plot([], [])
dots, = ax1.plot([], [], 'o')
lines, = ax1.plot([], [], lw = 2)

plot_colour = ["black", "red", "green", "yellow", "blue"]
line_list = []
dot_list = []
print(2)
for index in range(len(theta_rad)):
    line_obj = ax1.plot([], [], lw = 2, color = plot_colour[index])[0]
    dot_obj = ax1.plot([], [], 'o', color = plot_colour[len(theta_rad)-index-1])[0]
    line_list.append(line_obj)
    dot_list.append(dot_obj)
print(3)

def initialize():
    # initializing projectile range plot
    print(4)
    for line in line_list:
        line.set_data([], [])
    for dot in dot_list:
        dot.set_data([], [])
    print(5)

    return dot_list, line_list,

print(6)

def proj_animation(i):
    # animation function
    print(7)
    n = 100
    # fr = n
    y = np.empty([len(theta_rad), n], dtype = float)
    x = np.empty([len(theta_rad), n], dtype = float)
    r = projectile_range()
    graph_list = []
    for j in range(len(r)):
        x[j] = np.linspace(0, r[j], n)
        y[j] = np.multiply(x[j], np.tan(theta_rad[j])) - ((0.5*g*(np.square(x[j])))/(u*np.cos(theta_rad[j]))**2)

    for count, element in enumerate(line_list):
        element.set_data(x[count][:i], y[count][:i])

    for count, element in enumerate(dot_list):
        element.set_data(x[count][i], y[count][i])
    
    graph_list.append(dot_list)
    graph_list.append(line_list)
    graph_list = [item for sublist in graph_list for item in sublist] 
    print(8)
    return graph_list

proj_anim = mat_anim.FuncAnimation(fig1, proj_animation, frames = fr,
                                   interval = 20, blit = True)
proj_anim.save("projectile_range.mp4", fps = 20, extra_args = ['-vcodec', 'libx264'])

plt.show()

我测试了代码并且它有效。