[IPython-User] %run and matplotlib event loop

Fernando Perez fperez.net@gmail....
Wed Jan 4 20:25:54 CST 2012


On Wed, Jan 4, 2012 at 3:26 PM, Richard Everson
<R.M.Everson@exeter.ac.uk> wrote:
>
> In [1]: !cat demo.py
>
> import numpy as np
> import matplotlib.pyplot as plt
>
> plt.plot(np.random.rand(20))
>
>
> In [2]: run demo
>
> No figure window appears, but plt.show() (or plain show()) causes the plot to be displayed.  Interestingly, "run demo" and show() a second time doesn't display the updated plot unless the figure has been closed or beforehand.

You're missing an explicit plt.show() call at the end of your script.
This was a deliberate decision made long ago: we want *scripts* to
behave as much as possible when called via %run as they would when
called in standalone form as 'python foo.py'.  While calling
'plt.plot(x)' *interactively* will immediately display a figure,
running a script does require an explicit show() call.

This may appear to be an annoyance, but it will save a lot of
head-scratching from new users who would otherwise wonder why their
scripts work in ipython but not outside.

Best

f


More information about the IPython-User mailing list