matplotlib in interactive mode from a script

hi there,

I've got a bit stuck with running matplotlib in interactive mode. maybe
what I want to do can't be done easily.

want I want is a simple python script which I can run with a file argument.
this will then create some plots. the script will wait for user input (via
sys.stdin.readline()), use the input to adjust a threshold and replot my
plots. after the user is satisfied with the thresholds the script exit the
loop and will perform other processing. Basically I'm trying to get a
simple interactive program with plots but without having use wxpython or
simliar

I have been trying to test the potting part of this.

I have successfully set interactive mode on and from the python interpreter
I can plot and I see my plots and updates to them without needing to use
pyplot.show().

when I run the same scripts by using 'python scriptname.py' I don't see the
plots unless I use pyplot.show().

my simple test script is as follows

···

############
#test interactive matplotlib plotting

import numpy as np
import sys
import matplotlib.pylab as pyp

a=np.array([0,4,5,5,3,4,5])
pyp.figure()
pyp.plot(a)
#pyp.show()
input=sys.stdin.readline()
pyp.xlabel('my xlabel %s' %input)
input=sys.stdin.readline()

any help much appreciated
--
View this message in context: http://old.nabble.com/matplotlib-in-interactive-mode-from-a-script-tp29023641p29023641.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Hi,

I think what you are after is the interactive mode of matplotlib. You can turn
is on by "ion" and redraw the current figure using "draw". In ipythons "pylab"
mode this is done implicit. I attached some example lines which guide you to
the right direction. I'm not sure why I need two draws in my attached script,
but at least it seems to do the job.
For more infos you may visit:
http://matplotlib.sourceforge.net/users/shell.html#controlling-interactive-
updating

Kind regards,
Matthias

example_interactive_mode.py (359 Bytes)

···

On Tuesday, June 29, 2010 03:32:00 pm ninjasmith wrote:

hi there,

I've got a bit stuck with running matplotlib in interactive mode. maybe
what I want to do can't be done easily.

want I want is a simple python script which I can run with a file argument.
this will then create some plots. the script will wait for user input (via
sys.stdin.readline()), use the input to adjust a threshold and replot my
plots. after the user is satisfied with the thresholds the script exit the
loop and will perform other processing. Basically I'm trying to get a
simple interactive program with plots but without having use wxpython or
simliar

I have been trying to test the potting part of this.

I have successfully set interactive mode on and from the python interpreter
I can plot and I see my plots and updates to them without needing to use
pyplot.show().

when I run the same scripts by using 'python scriptname.py' I don't see the
plots unless I use pyplot.show().

my simple test script is as follows

############
#test interactive matplotlib plotting

import numpy as np
import sys
import matplotlib.pylab as pyp

a=np.array([0,4,5,5,3,4,5])
pyp.figure()
pyp.plot(a)
#pyp.show()
input=sys.stdin.readline()
pyp.xlabel('my xlabel %s' %input)
input=sys.stdin.readline()

any help much appreciated

Hi,

I think what you are after is the interactive mode of matplotlib. You can
turn
is on by "ion" and redraw the current figure using "draw". In ipythons
"pylab"
mode this is done implicit. I attached some example lines which guide you
to
the right direction. I'm not sure why I need two draws in my attached
script,
but at least it seems to do the job.
For more infos you may visit:
http://matplotlib.sourceforge.net/users/shell.html#controlling-interactive-
updating

Kind regards,
Matthias

import numpy as np
import sys
import matplotlib.pylab as pyp

a = np.array([0, 4, 5, 5, 3, 4, 5])
pyp.ion()
pyp.figure()
pyp.plot(a)
pyp.draw()
pyp.draw()

input = sys.stdin.readline()
print "input 1 : %s " % (input)
pyp.xlabel('my xlabel %s' % input)
pyp.draw()
pyp.draw()

input = sys.stdin.readline()
print "input 2 : %s " % (input)

pyp.ioff()
pyp.show()

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

that almost fixes it. I can now plot and re draw during the execution of my
script. However I cannot interact with the plots. i.e. I can't zoom in on
an area. when the script gets to the point where pyp.show() is called then
I'm able to do this. I'm thinking there may be no way round this?

or is there some way to run matplot lib plot in a different thread?

···

--
View this message in context: http://old.nabble.com/matplotlib-in-interactive-mode-from-a-script-tp29023641p29047262.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

ipython does this, I believe, when you call it with the -pylab option, but I have never tried it with a script.

Ben Root

···

On Thu, Jul 1, 2010 at 10:42 AM, ninjasmith <henrylindsaysmith@…287…> wrote:

Hi,

I think what you are after is the interactive mode of matplotlib. You can

turn

is on by “ion” and redraw the current figure using “draw”. In ipythons

“pylab”

mode this is done implicit. I attached some example lines which guide you

to

the right direction. I’m not sure why I need two draws in my attached

script,

but at least it seems to do the job.

For more infos you may visit:

http://matplotlib.sourceforge.net/users/shell.html#controlling-interactive-

updating

Kind regards,

Matthias

import numpy as np

import sys

import matplotlib.pylab as pyp

a = np.array([0, 4, 5, 5, 3, 4, 5])

pyp.ion()
pyp.figure()

pyp.plot(a)

pyp.draw()

pyp.draw()

input = sys.stdin.readline()

print "input 1 : %s " % (input)
pyp.xlabel(‘my xlabel %s’ % input)

pyp.draw()

pyp.draw()

input = sys.stdin.readline()

print "input 2 : %s " % (input)

pyp.ioff()

pyp.show()


This SF.net email is sponsored by Sprint

What will you do first with EVO, the first 4G phone?

Visit sprint.com/firsthttp://p.sf.net/sfu/sprint-com-first


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

that almost fixes it. I can now plot and re draw during the execution of my

script. However I cannot interact with the plots. i.e. I can’t zoom in on

an area. when the script gets to the point where pyp.show() is called then

I’m able to do this. I’m thinking there may be no way round this?

or is there some way to run matplot lib plot in a different thread?

ipython does this, I believe, when you call it with the -pylab option, but

I have never tried it with a script.

Ben Root

ok made some prgoress with this so thought I'd update

the following script works on widnows

'''
Created on Jul 1, 2010

@author: henrylindsaysmith
'''
# Id:

#test interactive matplotlib plotting

import threading
import numpy as np
import sys
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pylab as pyp

class MyThread(threading.Thread):
    
    def __init__ (self, data, label):
        self.data = data
        self.label = label
        threading.Thread.__init__ ( self )
    
    def run(self):
        pyp.plot(self.data)
        pyp.xlabel('%s' % self.label)
  pyp.show()

a = np.array([0, 4, 5, 5, 3, 4, 5])
MyThread(a,'first x label').start()
print ("please input x label")
input = sys.stdin.readline()
MyThread(a,input).start()

so basically abandoning interactive mode and running in a thread works. the
first show shows the plot which I can then interact with. After I input
from the keyboard the second plot updates the figure.

BUT

on mac I get the following errors

1) the figure window appears but has no content
2) after the keyboard input the xlabel updates and the figure content
displays and I get the following error

Exception in thread Thread-2:
Traceback (most recent call last):
  File
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py",
line 525, in __bootstrap_inner
    self.run()
  File "threadedMatplotLIb.py", line 28, in run
    pyp.show()
  File
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/backends/backend_tkagg.py",
line 79, in show
   Tk.mainloop()
  File
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py",
line 325, in mainloop
    _default_root.tk.mainloop(n)
RuntimeError: Calling Tcl from different appartment

I tried the backend macosx and the results were worse!! I'd rather use mac
if I can due to scikits.audiolab not installing in windows.

anyone shed any lights on the thread problem?

···

--
View this message in context: http://old.nabble.com/matplotlib-in-interactive-mode-from-a-script-tp29023641p29055179.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

A lot of GUI toolkits don't permit multi-threaded access to their
event loops. What you really want is to integrate your interaction
into that loop itself, so that the GUI handles things for you. Try
looking at the examples here:

http://matplotlib.sourceforge.net/examples/event_handling/

Specifically:

http://matplotlib.sourceforge.net/examples/event_handling/keypress_demo.html

Ryan

···

On Fri, Jul 2, 2010 at 6:20 AM, ninjasmith <henrylindsaysmith@...287...> wrote:

ok made some prgoress with this so thought I'd update

the following script works on widnows

'''
Created on Jul 1, 2010

@author: henrylindsaysmith
'''
# Id:

#test interactive matplotlib plotting

import threading
import numpy as np
import sys
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pylab as pyp

class MyThread(threading.Thread):

def __init__ (self, data, label):
self.data = data
self.label = label
threading.Thread.__init__ ( self )

def run(self):
pyp.plot(self.data)
pyp.xlabel('%s' % self.label)
pyp.show()

a = np.array([0, 4, 5, 5, 3, 4, 5])
MyThread(a,'first x label').start()
print ("please input x label")
input = sys.stdin.readline()
MyThread(a,input).start()

so basically abandoning interactive mode and running in a thread works. the
first show shows the plot which I can then interact with. After I input
from the keyboard the second plot updates the figure.

BUT

on mac I get the following errors

1) the figure window appears but has no content
2) after the keyboard input the xlabel updates and the figure content
displays and I get the following error

Exception in thread Thread-2:
Traceback (most recent call last):
File
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py",
line 525, in __bootstrap_inner
self.run()
File "threadedMatplotLIb.py", line 28, in run
pyp.show()
File
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/backends/backend_tkagg.py",
line 79, in show
Tk.mainloop()
File
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py",
line 325, in mainloop
_default_root.tk.mainloop(n)
RuntimeError: Calling Tcl from different appartment

I tried the backend macosx and the results were worse!! I'd rather use mac
if I can due to scikits.audiolab not installing in windows.

anyone shed any lights on the thread problem?

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma