views:

167

answers:

4

Hi All

I am working in linux and I don't know why using python and matplotlib commands draws me only once the chart I want. The first time I call show() the plot is drawn, wihtout any problem, but not the second time and the following.

I close the window showing the chart between the two calls. Do you know why and hot to fix it?

Thanks AFG

from numpy import *
from pylab import *

data = array( [ 1,2,3,4,5] )
plot(data)
[<matplotlib.lines.Line2D object at 0x90c98ac>]
show() # this call shows me a plot

#..now I close the window...

data = array( [ 1,2,3,4,5,6] )
plot(data)
[<matplotlib.lines.Line2D object at 0x92dafec>]
show()  # this one doesn't shows me anything
+1  A: 

in windows this works perfect:

from pylab import *
plot([1,2,3,4])
[<matplotlib.lines.Line2D object at 0x03442C10>]
#close window here
plot([1,2,3,4])
[<matplotlib.lines.Line2D object at 0x035BC570>]

did you try with:

from matplotlib import interactive
interactive(True)

sometimes matplotlib produces some headaches because we have to remember that some options are set in matplotlibrc (such as the backend or the interactive parameters). If you use matplotlib from different editors (IDLE-tk, pycrust-wxpython) or alternating interactive with scripting, then you have to take into account that the configuration that works in one mode could give you problems in the other mode and must be modified programmatically or using a dedicated configuration file.

The example I give, works directly (and without show()) because in matplotlibrc I have interactive set to True as default

joaquin
THanks for the response but it doesn't work. Probably I have some issue because now when I try to close the windows the OS says that it doesn't respond.
Abruzzo Forte e Gentile
which shell/IDE are you using? and which backend have you set?do you get the same problems using ipython ?
joaquin
A: 

I'm guessing that you are doing this in IDLE on Windows because that's where I've noticed this same problem.

From what I've deduced, there is a problem with using the TkAgg backend,which comes with the basic Python dist and appears to be the default for matplotlib, when using matplotlib with IDLE. It has something to do with the way IDLE uses subprocesses because if I start IDLE with the -n option, which disables subprocesses, I don't have this problem. An easy way to start it IDLE with the -n option on Windows is to right click and file and select 'Open with IDLE'. If you do this you should get an IDLE shell which says === No Subprocess ===

just above the prompt. For instance, borrowing code from joaquin's solution, you could try this simple code:

from matplotlib import interactive
interactive(True)
from pylab import *
plot([1,2,3,4])

then close the window and type the last line into the console again. It works for me in IDLE with the -n option.

So what can you do? You can always run IDLE in the mode without subprocesses, but there are dangers to that. You can use a different IDE. Many people suggest IPython though I'm not sold on it yet myself. You could also try a different backend for matplotlib. I'm going to try that in a little while cause I've been wondering whether it will work.

Justin Peel
+1  A: 

You likely have conflicts between your editor/IDE windowing system, and your plot windows.

A very good way around this is to use IPython. IPython is a great interactive environment, and has worked out these issues plus has many other advantages. At the beginning, start IPython with the command (from a terminal window) ipython -pylab to put it in the interactive pylab mode.

tom10
A: 

show() is only meant to be used once in a program, at the very end: it is a never ending loop that checks for events in the graphic windows.

The normal way of doing what you want is:

# … plot …
draw()  # Draws for real
raw_input()  # Or anything that waits for user input

# … 2nd plot …
draw()
raw_input()

# Last plot
show()  # or, again, draw(); raw_input()

You could try to see whether this works for you.

Alternatively, you can try to change the backend, as some backends work better than others:

import matplotlib
matplotlib.use('TkAgg')  # For other backends, do matplotlib.use('') in a shell
EOL