【问题标题】:matplotlib FuncAnimation, "tuple object is not callable"matplotlib FuncAnimation,“元组对象不可调用”
【发布时间】:2021-08-02 06:53:37
【问题描述】:

因为我注意到随着绘制更多点,anim.save 所需的时间呈指数增长,所以我尝试导出 400 点的动画,然后使用 ffmpeg 将它们粘合在一起。但是,我遇到了TypeError: 'tuple' object is not callable,我一直无法进步。

为了完成这个顺便说一句,我创建了一个初始化函数,而不是清理画布,静态地绘制所有以前绘制的点,然后我从那里开始绘制

我的动画功能:

def animate(i, pplotted):

    Pot = GenPwr[pplotted:i]
    t_pot = Time[pplotted:i]
    RotSp = RotSpeed[pplotted:i]
    RotTh = RotThrust[pplotted:i]
    WindV = WindVel[pplotted:i]

    # ___________GENPWR___________#
    ax1.text(max(Time) / 10, 1500, "GenPwr (kW)=" + str(GenPwr[i]), fontsize=8)
    ax1.set_xlabel("Time (s)").set_size(tittle_axes_size)
    ax1.grid(True, axis="y")

    # ___________ROTSPEED___________#
    ax2.text(
        max(Time) / 10,
        max(RotSpeed) / 13,
        "RotSpeed (rpm)=" + str(RotSpeed[i]),
        fontsize=8,
    )
    ax2.set_xlabel("Time (s)").set_size(tittle_axes_size)
    ax2.grid(True, axis="y")

    # ___________ROTTHRUST___________#
    ax3.text(
        max(Time) / 10,
        max(RotThrust) / 10,
        "RotThrust (kW)=" + str(RotThrust[i]),
        fontsize=8,
    )
    ax3.set_xlabel("Time (s)").set_size(tittle_axes_size)
    ax3.grid(True, axis="y")

    # ___________WINDVEL___________#
    ax4.text(
        max(Time) / 10, 1, "WindVel (m/s)=" + str(round(WindVel[i], 2)), fontsize=8
    )
    ax4.set_xlabel("Time (s)").set_size(tittle_axes_size)
    ax4.grid(True, axis="y")

    # updating line objects data
    line[0].set_data(t_pot, Pot)
    line[1].set_data(t_pot, RotSp)
    line[2].set_data(t_pot, RotTh)
    line[3].set_data(t_pot, WindV)

    return line,

我的初始化函数:

def init(pplotted, linesn, data):
    print(pplotted, linesn, data)
    for l in range(1, linesn + 1):
        print(l)
        line[l - 1].set_data(data[0][:pplotted], data[l][:pplotted])
    return line,

我的动画调用和保存片段循环:

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=True)
plt.tight_layout()

(line1,) = ax1.plot([], [], lw=1)
(line2,) = ax2.plot([], [], lw=1, color="r")
(line3,) = ax3.plot([], [], lw=1, color="c")
(line4,) = ax4.plot([], [], lw=1, color="m")
line = [line1, line2, line3, line4]

Pot = []
RotSp = []
RotTh = []
WindV = []
t_pot = []

dataPointsNumber = len(Time)
shards = int(dataPointsNumber / 400)

for shardN in range(1, shards + 1):
    Pot = GenPwr[: 400 * (shardN - 1)]
    t_pot = Time[: 400 * (shardN - 1)]
    RotSp = RotSpeed[: 400 * (shardN - 1)]
    RotTh = RotThrust[: 400 * (shardN - 1)]
    WindV = WindVel[: 400 * (shardN - 1)]

    data = [t_pot, Pot, RotSp, RotTh, WindV]

    anim = animation.FuncAnimation(
        fig,
        animate(400, pplotted=400 * (shardN - 1)),
        init_func=init(pplotted=400 * (shardN - 1), linesn=4, data=data),
        frames=400,
        interval=20,
        blit=True,
        cache_frame_data=False,
    )
    plt.show()

    writer = animation.writers["ffmpeg"](fps=240)
    anim.save("animation_shard_%s.mp4" %shardN, writer=writer, dpi=200)

【问题讨论】:

  • 我知道问题不是来自 init 函数,因为我可以删除动画对象中的调用,它会返回相同的错误
  • 另外,如果有人能给我指点如何为嵌入在情节中的文本设置动画,那就太好了

标签: python matplotlib matplotlib-animation


【解决方案1】:

更新:

一方面,通过挖掘源代码,我发现当你创建动画对象时,你可以在没有参数的情况下调用动画函数,并指定一个列表 em> 使用 fargs 的参数。而不是:

anim = animation.FuncAnimation(
        fig,
        animate(400, pplotted=400 * (shardN - 1)),
        init_func=init(pplotted=400 * (shardN - 1), linesn=4, data=data),
        frames=400,
        interval=20,
        blit=True,
        cache_frame_data=False,
    )

你必须这样写:

pplotted = (400 * (shardN - 1))
anim = animation.FuncAnimation(
        fig,
        animate,
        init_func=init(pplotted=400 * (shardN - 1), linesn=4, data=data),
        frames=400,
        interval=20,
        blit=True,
        fargs=[pplotted]
        cache_frame_data=False,
    )

另一方面,init func 也必须在不带参数的情况下调用。但是,如果没有指定 init 函数,在我创建的循环中,相同的 fig 会不断重复使用,因此新点的绘制将始终在“绘制”画布上进行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 2018-09-02
    • 2020-08-29
    • 2021-09-30
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多