Show () no longer redraws

I work on Linux and I don't know why using python and matplotlib commands only attracts me after I want to. The first time I call show () the graph is drawn with any problem, but not the second time and the next.

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

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
+2


a source to share


4 answers


on windows this works great:

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

      

Have you tried:



from matplotlib import interactive
interactive(True)

      

sometimes matplotlib causes some headaches, because we have to remember that some parameters are set in matplotlibrc (like backend or interactive parameters). If you are using matplotlib from different editors (IDLE-tk, pycrust-wxpython) or alternating with interactive scripting, then you need to take into account that a configuration running in one mode may cause problems in another mode and must be modified programmatically or using a dedicated file configuration.

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

+1


a source


You probably have conflicts between your editor / IDE windowing system and your graph windows.



A very nice way to use IPython . IPython is a great interactive environment and has worked around these problems and has many other benefits. First, start IPython with a command (from a terminal window) ipython -pylab

to put it in pylab interactive mode.

+1


a source


I am assuming that you are doing this in IDLE on Windows because that's where I noticed this same issue.

From what I have inferred, there is a problem using the TkAgg backend that comes with a 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 run IDLE with the -n option that disables the subprocesses, I don't have this problem. An easy way to run it IDLE with the -n option on Windows is to right-click the file and choose "Open with IDLE". If you do, you should end up with an IDLE wrapper that says === No Subprocess ===

just above the tip. For example, borrowing code from joaquin's solution, you can try this simple code:

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

      

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

So what can you do? You can always run IDLE in non-subprocess mode, but there are dangers to this. You can use a different IDE. A lot of people offer IPython, although I haven't sold it myself yet. You can also try another backend for matplotlib. I'm going to try this at some point because I was wondering if this would work.

0


a source


show()

intended only for use in the program at the very end: it is an endless loop that checks for events in the graphics windows.

The usual way to do what you need 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 can try to find out if this works for you.

Alternatively, you can try changing the backend as some servers perform better than others:

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

      

0


a source







All Articles