首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >动画函数必须返回Artist对象序列

动画函数必须返回Artist对象序列
EN

Stack Overflow用户
提问于 2020-07-08 06:13:02
回答 1查看 2.4K关注 0票数 1

我正尝试在我的Pycharm上测试一个Matplotlib动画示例,如下所示:

代码语言:javascript
运行
复制
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation

n = 1000
def update(curr):
    if curr == n:
        a.event_source.stop()

    subplot1.cla()
    subplot2.cla()
    subplot3.cla()
    subplot4.cla()

    # subplots re-set to standard positions
    x1 = np.random.normal(0, 1, n)
    x2 = np.random.gamma(2, 1, n)
    x3 = np.random.exponential(2, n)
    x4 = np.random.uniform(0, 6, n)

    # increment the number of bins in every 10th frame
    # bins = 20 + curr // 10
    bins = 10 + curr

    # drawing the subplots
    subplot1.hist(x1, bins=bins, alpha=0.5, color='red')
    subplot2.hist(x2, bins=bins, alpha=0.5, color='green')
    subplot3.hist(x3, bins=bins, alpha=0.5, color='blue')
    subplot4.hist(x4, bins=bins, alpha=0.5, color='darkorange')

    # set all ticks to null
    subplot1.set_xticks([])
    subplot2.set_xticks([])
    subplot3.set_xticks([])
    subplot4.set_xticks([])
    subplot1.set_yticks([])
    subplot2.set_yticks([])
    subplot3.set_yticks([])
    subplot4.set_yticks([])

    # name the subplots
    subplot1.set_title('Normal')
    subplot2.set_title('Gamma')
    subplot3.set_title('Exponential')
    subplot4.set_title('Uniform')

    # the title will change to reflect the number of bins
    fig.suptitle('No of bins: {}'.format(bins))

    # no redundant space left for saving into mp4
    plt.tight_layout()

# Set up formatting for the movie files
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Kuba Siekierzynski', title='Distributions'), bitrate=1800)

fig, ([subplot1, subplot2], [subplot3, subplot4]) = plt.subplots(2, 2)
a = animation.FuncAnimation(fig, update, interval=100, save_count=500, blit=True, frames=100)

# will only work with ffmpeg installed!!!
a.save('distributions.mp4', writer=writer)
plt.show()

终端显示错误:

代码语言:javascript
运行
复制
raise RuntimeError('The animation function must return a '
RuntimeError: The animation function must return a sequence of Artist objects.

我试了好几次,但都没弄明白。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-08 06:27:50

正如hereinit_func的情况所解释的,由于您在FuncAnimation定义中设置参数blit = True,因此您的update函数必须返回绘图对象。作为另一种选择,您可以设置blit = False;在这种情况下,您将获得此动画:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62784710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档