from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
with open('error_output.txt') as fobj:
x, y = zip(*([float(x) for x in line.split(',')] for line in fobj))
def animate(n):
line, = plt.plot(x[:n], y[:n], color='g')
return line,
anim = animation.FuncAnimation(fig, animate, frames=len(x), interval=1000)
plt.show()
这里是一个允许通过以下方式实时生成数据动画的版本regr_magic
:
import random
import time
from matplotlib import pyplot as plt
from matplotlib import animation
class RegrMagic(object):
"""Mock for function Regr_magic()
"""
def __init__(self):
self.x = 0
def __call__(self):
time.sleep(random.random())
self.x += 1
return self.x, random.random()
regr_magic = RegrMagic()
def frames():
while True:
yield regr_magic()
fig = plt.figure()
x = []
y = []
def animate(args):
x.append(args[0])
y.append(args[1])
return plt.plot(x, y, color='g')
anim = animation.FuncAnimation(fig, animate, frames=frames, interval=1000)
plt.show()
全班RegrMagic
都是模拟的帮助者Regr_magic()
。该__call__
方法使此类的实例的行为类似于函数。它有状态,并产生数字1, 0.56565
,2, 0.65566
等等。对于每个呼叫(第二个数字是任意的数)。它还具有时间延迟以模仿计算时间。
重要的是frames()
。替换Regr_magic()
为Regr_magic()
,应该很好。
没有模拟的版本:
import random
import time
from matplotlib import pyplot as plt
from matplotlib import animation
def frames():
while True:
yield Regr_magic()
fig = plt.figure()
x = []
y = []
def animate(args):
x.append(args[0])
y.append(args[1])
return plt.plot(x, y, color='g')
anim = animation.FuncAnimation(fig, animate, frames=frames, interval=1000)
plt.show()