Python 的 matplotlib
我在显示matplotlib窗口时遇到了麻烦。
我下载了Python 3.3、适用于Python 3.3的matplotlib和numpy。
我还安装了Visual Studio 2012的Python工具,这样我就可以在这个环境中创建Python项目。
这些都搞定后……我运行了这个非常简单的脚本:
import numpy as np
import matplotlib.pyplot as plt
import pylab
# Come up with x and y
x = np.arange(0, 5, 0.1)
y = np.sin(x)
# plot the x and y and you are supposed to see a sine curve
plt.plot(x, y)
# without the line below, the figure won't show
pylab.show()
这个脚本编译时没有任何警告或错误,但只显示了我的控制台窗口;没有图表或交互窗口出现。我尝试从命令提示符运行这个脚本,想着可能是Visual Studio环境搞错了,但还是不行。
我还尝试用Python 2.7运行,也没有成功。
我找到的每个教程都确认这个应该能正常工作。我快抓狂了,任何帮助我都非常感激。
相关问题:
2 个回答
0
你需要先放入
plt.figure()
这样才能在开始绘图之前创建一个图形窗口。
如果你使用的是像Spyder这样的图形界面控制台,记得要开启交互式绘图,这样图会在一个单独的窗口中显示,而不是直接在控制台里。
3
你应该输入
plt.show()
来代替。