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()

4

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.