plt.show() 在 spyder ide 中不起作用 [英] plt.show() not working in spyder ide
问题描述
我是Python和Spyder的新手.我正在使用Python 2.7.13和Spyder 3.1.4.我无法使用 plt.show()
处理我的数据,也无法从网站上复制一个简单的直方图示例.plt.draw()
也不起作用.我提出了类似的问题,将图形后端从内联更改为自动更改为Qt4更改为Qt5,但是这些都不起作用.如果在 IPython 控制台中键入 fig
,它将显示图形.我在这里发布示例.任何有关如何解决此问题的建议都将受到赞赏.
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()
推荐答案
更改您的IPython控制台图形设置.
Change your IPython console graphics settings.
如果您使用的是 Spyder,请按以下步骤操作:
If you are using Spyder here are the steps:
- 转到
工具
. - 转到
首选项
. - 选择
IPython控制台
. - 转到
图形
标签. - 在
Graphics backend
部分下,选择Automatic
作为后端类型.
- Go to
Tools
. - Go to
Preferences
. - Select
IPython console
. - Go to
Graphics
tab. - Under the
Graphics backend
section, selectAutomatic
as the backend type.
然后,重新启动内核.
这篇关于plt.show() 在 spyder ide 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!