Live plotting using matplotlib without hault

696 views Asked by At

Here is a minimum working example of my code.

I am trying to plot a live graph using matplotlib by taking some inputs from the user via gui. For building the gui, I used the library easygui

However, there is one problem: the graph stops building while taking an update from the user and I wish it to continue. Is there something that I'm missing here.

#!/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

here is the download link for easygui

sudo python  setup.py  install

based on your version of linux or OS. use the following link

1

There are 1 answers

0
pistal On

Thanks to 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