Non-interactive plotting to window?

I have a sizeable number of python data-inspection scripts that work as
follows:

- read some data, or do something with it
- plot the data
- query the user on the command line and get a response
- do what the user commands.

These use the venerable PGPLOT package for the graphics, but this has been
static for years and is getting increasingly vulnerable to obsolescence of
the supporting packages (e.g., numarray).

I'm having a hard time converting these scripts to matplotlib because when
you do a show(), the mainloop takes over. It's possible to work around by
doing a show() for every plot, and then killing the plot manually, but after
a couple of hundred manual kills this gets tiresome. It would also be
possible to restructure the code so that the various options get controlled
by key press events in the plot, I suppose, but the effort involved in
converting all the scripts would be pretty large.

So my question: Is there a simple way of getting matplotlib to display a
plot in a window and then surrender control to the main program, without
destroying the plot? Something like a method to kill mainloop would be
ideal.

Thanks.

[Apologies if this is general knowledge -- I couldn't find an answer.]

···


View this message in context: http://old.nabble.com/Non-interactive-plotting-to-window--tp32981792p32981792.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

2011/12/15 John Thorstensen <john.thorstensen@...3417...>:

So my question: Is there a simple way of getting matplotlib to display a
plot in a window and then surrender control to the main program, without
destroying the plot? Something like a method to kill mainloop would be
ideal.

Just make sure you use interactive mode and get rid of the show calls:

import matplotlib.pyplot as plt
plt.ion() # set interactive mode
plt.plot(foo) # plot something
bar() # do stuff while the plot is visible
plt.close() # if you want to close the plot window from the script

This works in mpl 1.1.0 al least with tkagg, gtkagg and qt4agg backends.

Goyo

Here is a script that solves the problem, based on Goyo's input:

···

******************

#!/usr/bin/env python
"""simple_matplot.py --
   Repeatedly plots sinusoids (shifted a bit each time).
     - control returns to the user after each plot;
     - the old plot is erased between cycles.
"""

import numpy as np
import matplotlib
import matplotlib.pyplot as plt

x = np.arange(0.,2.*np.pi,0.02)
y = np.sin(x)

c = "go"

plt.ion()
fig = plt.figure()
ax = fig.add_subplot(1,1,1)

while c.find("go") > -1 :

   try:
       del ax.lines[0]
   except :
       pass
   line, = ax.plot(x,y,'-')
   plt.draw()
   print "stop or go?"
   c = raw_input()
   x = x + 0.5

**********

John Thorstensen wrote:

I have a sizeable number of python data-inspection scripts that work as
follows:

- read some data, or do something with it
- plot the data
- query the user on the command line and get a response
- do what the user commands.

These use the venerable PGPLOT package for the graphics, but this has been
static for years and is getting increasingly vulnerable to obsolescence of
the supporting packages (e.g., numarray).

I'm having a hard time converting these scripts to matplotlib because when
you do a show(), the mainloop takes over. It's possible to work around by
doing a show() for every plot, and then killing the plot manually, but
after a couple of hundred manual kills this gets tiresome. It would also
be possible to restructure the code so that the various options get
controlled by key press events in the plot, I suppose, but the effort
involved in converting all the scripts would be pretty large.

So my question: Is there a simple way of getting matplotlib to display a
plot in a window and then surrender control to the main program, without
destroying the plot? Something like a method to kill mainloop would be
ideal.

Thanks.

[Apologies if this is general knowledge -- I couldn't find an answer.]

--
View this message in context: http://old.nabble.com/Non-interactive-plotting-to-window--tp32981792p32987821.html
Sent from the matplotlib - users mailing list archive at Nabble.com.