10

I am new to Python and Spyder. I am using Python 2.7.13 and Spyder 3.1.4. I cannot get plt.show() to work on my data, and cannot reproduce a simple histogram example from the website. plt.draw() does not work either. I have changed the graphics backend from inline to automatic to Qt4 to Qt5 as a similar question proposed, but non of this has worked. If type fig in the IPython console, it will display the graph. I am posting the example here. Any suggestions on how to fix this are appreciated.

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

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)

fig = plt.figure()
ax = fig.add_subplot(111)

# the histogram of the data
n, bins, patches = ax.hist(x, 50, normed=1, facecolor='green', alpha=0.75)

# hist uses np.histogram under the hood to create 'n' and 'bins'.
# np.histogram returns the bin edges, so there will be 50 probability
# density values in n, 51 bin edges in bins and 50 patches.  To get
# everything lined up, we'll compute the bin centers
bincenters = 0.5*(bins[1:]+bins[:-1])
# add a 'best fit' line for the normal PDF
y = mlab.normpdf( bincenters, mu, sigma)
l = ax.plot(bincenters, y, 'r--', linewidth=1)

ax.set_xlabel('Smarts')
ax.set_ylabel('Probability')
#ax.set_title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$')
ax.set_xlim(40, 160)
ax.set_ylim(0, 0.03)
ax.grid(True)

plt.show()
fmc100
  • 171
  • 1
  • 1
  • 12
  • Can you add a precise description of how you run this code in spyder? Is it a new IPython console each time? Do you want to use IPython and show the graph inline or do you want it to display as its own figure window? – ImportanceOfBeingErnest Aug 14 '17 at 18:04
  • I am running it out of the IPython console and as a .py file. It is the same IPython console each time, but I use %reset before running to clear out the variables. When I run it line by line using F9 in the editor, plt.show() does nothing. If I run the .py file as a whole, the plot displays whether I have the plt.show() in the file or commented out. It would be great to know how to show the graph as its own figure window. – fmc100 Aug 14 '17 at 18:24
  • If you don't need the IPython console, [this answer](https://stackoverflow.com/questions/42655533/python-spyder-choose-where-and-when-to-show-plots) might be what you're looking for. So is there a reason to use the IPython console? If so, is it necessary to be able to process the code line by line? The more precisely you state what you need, the higher the chances to get a satisfying solution. – ImportanceOfBeingErnest Aug 14 '17 at 18:38
  • I am using spyder similarly to how I would use matlab, so writing commands in the IPython window is how I normally work when analyzing a dataset. I thought part of the point on IPython windows was to provide a place to try new commands as a script was created. I don't fully understand the content in the link provided, but `%matplotlib qt` or `%matplotlib qt5` procduces a huge error when I run them. Sorry, very new to python and concerned when a basic example doesn't run like it seems like it should... – fmc100 Aug 14 '17 at 18:45
  • 1
    The `%matplotlib` only works in IPython, not in the "dedicated Python console". If you want to use some specific backend in IPython console inside Spyder you need to select it in the preferences. Either "TK" or "QT" should work (that would create a new window). However, this is only possible if `plt.show()` is written in the same cell as the plot is created. You might want to run your scripts in a new dedicated console, but use IPython to try out stuff. – ImportanceOfBeingErnest Aug 14 '17 at 19:00
  • I am still not following. So in an IPython console, if I type something basic like x = plt.plot([1, 2, 3, 4]), x.show() is not working, and it is not possible to display the graph? Oh if only the world used matlab... – fmc100 Aug 14 '17 at 19:08
  • 1
    `x.show()` is not defined. It should be `plt.show()`. Maybe look at [this screenshot](https://i.stack.imgur.com/sxs9e.png). However, you don't even need `plt.show()` in that case. – ImportanceOfBeingErnest Aug 14 '17 at 19:19
  • thanks for you help – fmc100 Aug 14 '17 at 19:37

2 Answers2

12

Change your IPython console graphics settings.

If you are using Spyder here are the steps:

  1. Go to Tools.
  2. Go to Preferences.
  3. Select IPython console.
  4. Go to Graphics tab.
  5. Under theGraphics backend section, select Automatic as the backend type.

Then, restart your kernel.

enter image description here

costaparas
  • 5,047
  • 11
  • 16
  • 26
Abhi Agarwal
  • 156
  • 1
  • 4
  • Worked perfectly for me. But being new to Spyder figuring out "restart your kernel" part took some time. Would be nice to add it in your answer as well. Until then if anyone is also looking for the same, you need to go to the options(three horizontal bars) in your console and select 'Restart Kernel'.. – Sachin Mar 20 '21 at 17:46
  • "Automatic" didn't work for me. I had to set Backend to "Inline" (to have it show up on the Plots panel). – Reinier Jan 23 '22 at 20:17
8

Use plt.draw() instead of plt.show(). This worked for me. Here is the reference what I found: https://github.com/spyder-ide/spyder/issues/2402

finefoot
  • 9,914
  • 7
  • 59
  • 102
Harsh
  • 81
  • 1
  • 2