自学内容网 自学内容网

The animation function must return a sequence of Artist objects.

常见报错

动画中plt.text, plt.title内容不会随时间更新

报错  The animation function must return a sequence of Artist objects.

update函数只能返回plt.gca().images, 不能返回plt, 否则会报错如上

原因&解决方法

上述问题都是因为 animation.FuncAnimation 函数中blit参数值设置成为了true, 在此情况下, blit会使动画开启优化, 只更新update中返回的部分, 如果我们不在update中主动返回多个对象, 就会报错“ The animation function must return a sequence of Artist objects.”, 即使我们返回了plt.gca().images取消了上述报错, 动画还是不会更新我们的plt.title & plt.text内容, 要想更新此内容, 必须返回上述两者对象, 示例如下:

# 更新函数
def update(frame):
    global angle
    plt.clf()  
    both_all = both_newtown_and_michellson(X, Y, frame)
    im = plt.imshow(both_all, cmap=black_to_yellow, extent=(-10, 10, -10, 10), alpha=0.5, vmin=-1, vmax=1)
    
    # 更新标题和文本
    title_text = plt.title(f'非等厚非等倾°')
    text = plt.text(0.023, 0.01, f'非等厚非等倾, θ = {angle:.6f} °', fontsize=12, color='red')
    
    plt.axis('off')
    
    # 返回更新的对象
    return [im, title_text, text]  #返回所有需要改变的值

参考博客

相同问题博客链接:动画函数必须返回Artist对象序列-腾讯云开发者社区-腾讯云 (tencent.com)


原文地址:https://blog.csdn.net/li_peixiansang/article/details/142603554

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!