2

The following Python script make a plot (of the function x^2) and shows it:

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(0,10,11)

y=x**2

plt.plot(x,y)
plt.show()

All very well, but what I've noticed is that the Python shell does not remain 'available' after running this script. Specifically (see figure below), the cursor blinks below the last command prompt (">>>") and nothing I type appears next to a command prompt. Only after I close the figure can I type in the command prompt again.

Does anybody know how to keep the plot and the command prompt simultaneously? (I'm running iPython on Linux; I've run similar scripts on Windows using the Enthought Python Distribution and not experienced such problems).

Python shell after running the script above

P.S. I've tried adding plt.ion() to the script, and although it does keep the command line available, it yields a blank figure (see below). Entering "%run plot_test.py" in the shell gives "SyntaxError: invalid syntax" with "%" highlighted.

Python shell after running the script above with plt.ion()

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
  • 1
    possible duplicate of [Is there a way to detach matplotlib plots so that the computation can continue?](http://stackoverflow.com/questions/458209/is-there-a-way-to-detach-matplotlib-plots-so-that-the-computation-can-continue) – mgilson Apr 30 '14 at 05:58
  • Try using the `%run` command in IPython shell: `%run script_name.py` – Ashwini Chaudhary Apr 30 '14 at 06:00
  • Using "%run" gives a syntax error in iPython. (I do recall it being the correct command in Enthought Python Distribution, however). – Kurt Peek Apr 30 '14 at 07:39
  • Related question: http://stackoverflow.com/questions/11764293/python-and-update-figure-in-matplotlib/11764620#11764620 – jmetz Apr 30 '14 at 10:33

1 Answers1

7

Try to using ion as

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(0,10,11)

y=x**2

plt.ion() # here
plt.plot(x,y) # now it shows plot window
plt.draw()

plt.plot(x,y*10) # will be updated
plt.draw()
plt.plot(x,y*100) # will be updated
plt.draw()
emesday
  • 6,078
  • 3
  • 29
  • 46
  • 1
    This does have the desired effect of keeping the command line usable, but has the undesired side-effect that the figure is now empty (see figure above). – Kurt Peek Apr 30 '14 at 07:38
  • See also, e.g. http://stackoverflow.com/questions/11764293/python-and-update-figure-in-matplotlib/11764620#11764620 – jmetz Apr 30 '14 at 10:44