简体   繁体   English

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

[英]plt.show() not working in spyder ide

I am new to Python and Spyder.我是 Python 和 Spyder 的新手。 I am using Python 2.7.13 and Spyder 3.1.4.我使用的是 Python 2.7.13 和 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.show()处理我的数据,也无法从网站上重现简单的直方图示例。 plt.draw() does not work either. plt.draw()也不起作用。 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.我已将图形后端从内联更改为自动,将 Qt4 更改为 Qt5 作为提出的类似问题,但这些都没有奏效。 If type fig in the IPython console, it will display the graph.如果在 IPython 控制台中键入fig ,它将显示图形。 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()

Change your IPython console graphics settings.更改您的 IPython 控制台图形设置。

If you are using Spyder here are the steps:如果您使用的是 Spyder,请执行以下步骤:

  1. Go to Tools .转到Tools
  2. Go to Preferences .转到Preferences
  3. Select IPython console .选择IPython console
  4. Go to Graphics tab.转到Graphics选项卡。
  5. Under the Graphics backend section, select Automatic as the backend type.Graphics backend部分下,选择Automatic作为后端类型。

Then, restart your kernel.然后,重新启动内核。

在此处输入图片说明

Use plt.draw() instead of plt.show() .使用plt.draw()而不是plt.show() This worked for me.这对我有用。 Here is the reference what I found: https://github.com/spyder-ide/spyder/issues/2402这是我发现的参考: https : //github.com/spyder-ide/spyder/issues/2402

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM