0

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

modred
  • 5
  • 4
  • You are re-inventing the wheel, use the main event loop that already exists for you in one of the gui frame works you can embed matplotlib in. – tacaswell Sep 14 '13 at 17:01
  • Thanks tcaswell, could you link me to something that would help me in the gui frameworks? I'm kind of struggling to find out how to do this – modred Sep 14 '13 at 17:05
  • http://matplotlib.org/examples/user_interfaces/ – tacaswell Sep 14 '13 at 17:07
  • thanks boss, but which of these would allow for simple user input? In my real program I have multiple graphs that also use text boxes, but I would like something simple that the user can just write in, in order to set the state of a variable. – modred Sep 14 '13 at 17:11
  • All of them. matplotlib will embed itself in to any of those toolkits, and you can use any of those toolkits to build fully functional GUIs – tacaswell Sep 14 '13 at 17:15
  • I might be sending you down a much more involved path than you want. You might be better of just starting an interactive python session and using `plt.ion()` – tacaswell Sep 14 '13 at 17:19
  • Well what I want to do is have a console (like Terminal on OSX) open and be able to respond to prompts, while at the same time having a plot open. is plt.ion() necessary for this? – modred Sep 14 '13 at 19:23
  • 1
    it makes opening a figure non-blocking so you can do what ever you want in the terminal (think the MATLAB style interface) while the figure(s) are shown. – tacaswell Sep 14 '13 at 19:26

1 Answers1

0

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

tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • yeah, this is awesome, except for one thing. It seems like while you can input from the console -- which is awesome thanks so much -- the graph itself is frozen. Is there anyway to resolve this? I would like the user to be able to move around and stuff on my graphs. – modred Sep 14 '13 at 20:32
  • what do you mean 'frozen'? For me zoom, pan, and the various graph editing widgets work as expected. – tacaswell Sep 14 '13 at 20:35
  • I am getting the spinning wheel whenever I put my cursor over the graph, thus i cannot interact with the graph. – modred Sep 14 '13 at 20:43
  • which version, os and backend are you using? – tacaswell Sep 14 '13 at 20:47
  • OSX 10.6.8, python 4.2.1, sorry i don't know how to find the backend. Numpy? – modred Sep 14 '13 at 20:50
  • `matplotlib.get_backend()` will tell you the backend. `matplotlib.__version__` will tell you the mpl version. I assume you mean python 2.4.1 – tacaswell Sep 14 '13 at 20:53
  • for reference, I am testing on linux, python 2.7.5+, mpl 1.4.x (master branch), and Qt4Agg. I suspect part of your problem is you are using a very old version of both python and matplotlib – tacaswell Sep 14 '13 at 20:54
  • Python 2.7.3 -- 32-bit, matplotlib version 1.2.0, backend WXAgg – modred Sep 14 '13 at 20:57
  • ill try it with the backend you are using – modred Sep 14 '13 at 21:03
  • hmm, i tried to use matplotlib.rcParams['backend'] = 'Qt4Agg' to change the backend, but to no avail – modred Sep 14 '13 at 21:13
  • you have to do that before `pyplot` is imported – tacaswell Sep 14 '13 at 21:15
  • Ok. Do I need to change my matplotlib version and python version, or do you think this is a problem with the backends? – modred Sep 14 '13 at 21:43
  • There is obviously some extra thread-magic that the QT backend is doing that the WXbackend is not. – tacaswell Sep 15 '13 at 01:53