【问题标题】:The animation function must return a sequence of Artist objects动画函数必须返回一系列 Artist 对象
【发布时间】:2020-10-28 06:27:09
【问题描述】:

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

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()

终端显示错误:

raise RuntimeError('The animation function must return a '
RuntimeError: The animation function must return a sequence of Artist objects.

试了好几次都搞不定

【问题讨论】:

    标签: python matplotlib animation pycharm distribution


    【解决方案1】:

    正如hereinit_func 的解释,由于您在FuncAnimation 定义中设置参数blit = True,您的update 函数必须返回绘图对象。作为替代方案,您可以设置blit = False;在这种情况下,你会得到这个动画:

    【讨论】:

    • 什么是“绘图对象”?例如。我有一个图形、轴和一个由axes.scatter() 创建的东西。从我的 animate 函数中返回任何这些都不会使错误消失。
    • 请用您的具体问题和相关代码发布其他问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 2015-11-05
    • 1970-01-01
    • 2019-12-31
    • 2018-05-23
    • 2023-02-04
    • 1970-01-01
    相关资源
    最近更新 更多