Question

I am trying to make it so that I can run an infinite loop asking for user input as well as running a matplotlib simple diagram. Any suggestions how this can work? Currently I have for my code:

def createGraph():
 fig = plt.figure()
 fig.suptitle('A Graph ', fontsize=14, fontweight='bold')

 ax = fig.add_subplot(111)
 fig.subplots_adjust(top=.9)

 ax.set_xlabel('X Score')
 ax.set_ylabel('Y Score')
 plt.plot([1,2,3,4,5,6,7],[1,3,3,4,5,6,7], 'ro')
 plt.show()

def sub_proc(q,fileno):
 sys.stdin = os.fdopen(fileno)  #open stdin in this process
 some_str = ""
 while True:
    some_str = raw_input("> ")

    if some_str.lower() == "quit":
        return
    q.put_nowait(some_str)

if __name__ == "__main__":
    q = Queue()
    fn = sys.stdin.fileno() #get original file descriptor
    qproc = Process(target=sub_proc, args=(q,fn))
    qproc.start()
    qproc.join()
    zproc = Process(target=createGraph)
    zproc.start()
    zproc.join()

As you see, I am trying to get processes to get this to work, so the code works in parallel. Ultimately, I would like to get it so that a user can display a graph, while at the same time being able to input in the console. Thanks for any help!!

Was it helpful?

Solution

Is this what you want?

import matplotlib.pyplot as plt
import numpy as np

if __name__ == "__main__":
    fig, ax = plt.subplots(1, 1)
    theta = np.linspace(0, 2*np.pi, 1024)
    ln, = ax.plot(theta, np.sin(theta))

    plt.ion()
    plt.show(block=False)
    while True:
        w = raw_input("enter omega: ")
        try:
            w = float(w)
        except ValueError:
            print "you did not enter a valid float, try again"
            continue
        y = np.sin(w * theta)
        ln.set_ydata(y)
        plt.draw()

I think I was sending you down a much too complicated path in the comments

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top