0

I have a simple python script as

import numpy as np
import matplotlib.pyplot as plt
import pylab

x = np.arange(0, 5, 0.1)
y = np.sin(x)
plt.plot(x, y)
pylab.show()

If I run it from the terminal, it opens the interactive window of pylab to show the output graph. But if I run the script by Spyder, only a static image is shown in the console.

How can I tell Spyder to open the interactive graph of pylab? Or how should I modify my code to diplay an interactive graph (with the ability to zoom and enlarge).

Googlebot
  • 15,159
  • 44
  • 133
  • 229
  • This will not solve the problem, but just as a side remark here: It would be recommended not to use `pylab` at all. From the [usage guide](http://matplotlib.org/faq/usage_faq.html#matplotlib-pyplot-and-pylab-how-are-they-related): *"pylab is a convenience module that bulk imports matplotlib.pyplot (for plotting) and numpy (for mathematics and working with arrays) in a single name space. Although many examples use pylab, it is no longer recommended."* Simply use `import matplotlib.pyplot as plt` and call `plt.show()` at the end. – ImportanceOfBeingErnest Aug 19 '17 at 18:41
  • Possible duplicate of [How do I get interactive plots again in Spyder/IPython/matplotlib?](https://stackoverflow.com/questions/23585126/how-do-i-get-interactive-plots-again-in-spyder-ipython-matplotlib) – Chiel Aug 19 '17 at 18:47
  • I'm not sure if this is a duplicate, since the other question specifically asks for the IPython backend, while here a behaviour similar to terminal is mentionned in the question. At least I provided an answer mentionning both options. – ImportanceOfBeingErnest Aug 19 '17 at 19:11

1 Answers1

1

You basically have two different options.

Run in dedicated Python console

In Spyder go to Preferences/Run and select "Execute in new dedicated Python console". This will run your script as if it was started from the terminal.

enter image description here

Chose IPython backend

Go to Preferences/IPython/Graphics and chose either "Automatic" or "Tkinter", in any case something different than "Inline". This will run your script in IPython (if the "Execute in current IPython console option is active), but without the use of the Inline backend.

enter image description here

Community
  • 1
  • 1
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712