【发布时间】:2017-05-28 20:32:03
【问题描述】:
我想创建一个 matplotlib 动画,但我不想让 matplotlib 给我打电话,我想打电话给 matplotlib。例如我想这样做:
from random import random
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def update(frame):
plt.scatter(random(),random())
fig, ax = plt.subplots()
ani = FuncAnimation(fig, update, interval=340, repeat=True)
ani.save("my.mov", "avconv")
像这样:
def update():
plt.scatter(random(),random())
fig, ax = plt.subplots()
ani = MadeUpSubClassPassiveAnimation(fig)
while True:
update()
ani.update()
# do other stuff ...
ani.save("my.mov", "avconv")
我意识到我可以像这样驱动现场情节:
def update():
plt.scatter(x, y)
plt.pause(0.01)
fig, ax = plt.subplots()
plt.ion()
while True:
update()
time.sleep(1)
但是 AFAIK 我需要使用 Animation 来实现 save() 功能。那么,是否可以驾驶Animation 而不是让它驾驶我?如果有怎么办?
【问题讨论】:
-
对于使用循环的示例,请参见例如this question,但周围还有数百个其他示例。但是,这听起来像是典型的XYproblem。能否请您具体说明这样做的目的是什么?
-
这不是 XY 问题。为什么?因为就代码结构而言,它更有意义。我希望 matplotlib 视图只是另一个输出处理程序。就像模型/视图模式中的视图。我的模型做了一些事情,然后通知处理程序,其中一个可能是也可能不是 matplotlib 动画。
-
看,我们正在进行典型的 XY 问题讨论。 ;-) while 循环不会通知任何人任何事情。您能否以一种清楚您需要什么的方式写下这个问题?
-
不,对不起,我不能。上面写的问题的形式简洁地达到了我想要的核心,没有任何分心/臃肿。我在 SO 上与许多人进行过讨论,我对如何写问题的看法没有改变;)。
-
@ImportanceOfBeingErnest AFAICT 我的问题不是这些问题的重复,因为这些问题不使用动画。我已经更新了我的问题以尝试更好地解释。
标签: python animation matplotlib