【发布时间】: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