plt.show() 在 spyder ide 中不起作用

fmc*_*100 6 python matplotlib

我是 Python 和 Spyder 的新手。我使用的是 Python 2.7.13 和 Spyder 3.1.4。我无法plt.show()处理我的数据,也无法从网站上重现简单的直方图示例。plt.draw()也不起作用。我已将图形后端从内联更改为自动,将 Qt4 更改为 Qt5 作为提出的类似问题,但这些都没有奏效。如果fig在 IPython 控制台中键入,它将显示图形。我在这里发布示例。任何有关如何解决此问题的建议表示赞赏。

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()
Run Code Online (Sandbox Code Playgroud)

小智 10

更改您的 IPython 控制台图形设置。

如果您使用的是 Spyder,请执行以下步骤:

  1. Tools
  2. Preferences
  3. 选择IPython console
  4. 转到Graphics选项卡。
  5. 在该Graphics backend部分下,选择Automatic作为后端类型。

然后,重新启动内核。

在此处输入图片说明


小智 8

使用plt.draw()代替plt.show()。这对我有用。这是我发现的参考:https : //github.com/spyder-ide/spyder/issues/2402