Matplotlib创建实时动画图 [英] Matplotlib create real time animated graph
问题描述
我很难设置代码来创建实时动画图,我的代码是在收集数据之后绘制图形,而不显示每次迭代.我的脚本运行回归函数,然后存储在文件中,然后访问文件并对其进行打印,这就是我所拥有的,我需要移动或更改以使其实时图形化的内容是什么?我尝试在for
循环内移动出图函数,但这没用,有什么建议吗?
I am having a hard time setting up my code to create a real time animated graph, my code is graphing after the data is being collected, not showing every iteration. My script runs a regression function then stores in a file, then I access the files and plot them, here is what I have, what do I need to move around or change to have it graph real time? I tried moving the plot functions inside the for
loop but that didn't work, any suggestions?
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
num = 10
for idx in range(1,num):
c,e = Regr_magic()
with open("CK_output.txt",'a') as CK:
CK.write("{0},{1}\n".format(idx,c))
with open("error_output.txt",'a') as E:
E.write("{0},{1}\n".format(idx,e))
def animate(i):
pull = open('error_output.txt','r').read()
data = pull.split('\n')
xar = []
yar = []
for each in data:
if len(each)>1:
x,y = each.split(',')
xar.append(float(x))
yar.append(float(y))
ax1.plot(xar, yar)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
仅供参考,数据文件包含以下内容,迭代编号和Ck值或错误,因此它们看起来像这样
FYI, data files contain the following, the iteration number and Ck value or error, so they look like this
1,.0554
2,.0422
3,.0553
4,.0742
5,.0232
推荐答案
预先计算结果的解决方案
这会根据您的输出文件中的数据制作出不错的动画:
Solution for pre-computed results
This makes a decent animation from the data in your output file:
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
产生的数据进行实时动画处理:
Solution for a real-time animation as the values are computed
Here a version that allows real-time animation of data produce by 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
等(第二个数字是随机数).它也有时间延迟来模仿计算时间.
The class RegrMagic
is a helper the mocks Regr_magic()
. The __call__
method makes an instance of this class behave like a function. It has state and produces the numbers 1, 0.56565
, 2, 0.65566
etc. for each call (second number is a random number). It also has a time delay to mimic the computation time.
重要的是frames()
.将Regr_magic()
替换为Regr_magic()
,应该很好.
The important thing is frames()
. Replace Regr_magic()
with Regr_magic()
and should be good to go.
没有模拟的版本:
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()
这篇关于Matplotlib创建实时动画图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!