使用matplotlib进行实时绘图,无需考虑

时间:2013-09-11 11:02:07

标签: python linux matplotlib easygui

以下是我的代码的最低工作示例。

我正在尝试使用matplotlib绘制一个实时图形,通过gui从用户那里获取一些输入。为了构建gui,我使用了库easygui

然而,有一个问题: 图表在从用户处获取更新时停止构建,我希望它继续。这里有什么东西我不知道。

#!/usr/bin/env python

from easygui import *
from matplotlib.pylab import *
import numpy
import random

n = 0
fig=plt.figure()
x=list()
y=list()
plt.title("live-plot generation")
plt.xlabel('Time(s)')
plt.ylabel('Power(mw)')
plt.ion()
plt.show()
calculated=[random.random() for a in range(40)]
recorded=[random.random() for a in range(40)]
possible=[random.random() for a in range(5)]

plt.axis([0,40,0,10000])
for a in range(0, len(recorded)):
        temp_y= recorded[a]
        x.append(a)
        y.append(temp_y)
        plt.scatter(a,temp_y)
        plt.draw()
        msg = "Change"
        title = "knob"
        choices = possible
        if a>9:
                b = (a/10) - numpy.fix(a/10)
                if b==0:
                        choice = choicebox(msg, title, choices)
                        print "change:", choice

这是easygui

的下载链接
sudo python  setup.py  install

基于您的Linux或OS版本。使用以下link

1 个答案:

答案 0 :(得分:0)

感谢J.F. Sebastian

import easygui
from Tkinter    import Tk
from contextlib import contextmanager

@contextmanager
def tk(timeout=5):
    root = Tk() # default root
    root.withdraw() # remove from the screen

    # destroy all widgets in `timeout` seconds
    func_id = root.after(int(1000*timeout), root.quit)
    try:
        yield root
    finally: # cleanup
        root.after_cancel(func_id) # cancel callback
        root.destroy()

with tk(timeout=1.5):
        easygui.msgbox('message') # it blocks for at most `timeout` seconds