import numpy as np
import matplotlib.pyplot as plt
# Come up with x and y
x = np.arange(0, 5, 0.1)
y = np.sin(x)
# Just print x and y for fun
print x
print y
# 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
plt.show()
16
现代IPython使用“ --matplotlib " argument with an optional backend parameter. It defaults to " auto”,这在Mac和Windows上通常都足够好 . 我没有在Ubuntu或任何其他Linux发行版上测试它,但我希望它可以工作 .
ipython --matplotlib
2
如果你遇到一个问题,其中 pylab.show() 冻结了IPython窗口(这可能是特定于Mac OS X;不确定),你可以在IPython窗口中使用cmd -c,切换到绘图窗口,它就会爆发 .
10 回答
你可以输入
或者更好,使用
ipython -pylab
.pylab.show()
可以阻止(你需要关闭窗口) .更方便的解决方案是在启动时执行
pylab.ion()
(交互模式):all(相当于pylab)pyplot.*
命令立即显示其绘图 . More information on the interactive mode可以在官方网站上找到 .我还第二次使用更方便的
ipython -pylab
(--pylab
,在较新的版本中),它允许你跳过from … import …
部分(%pylab
也适用于较新的IPython版本) .试试这个:
在导入pylab之前
出现任何错误?这可能是一个没有设置后端的问题 . 您可以从Python解释器或主目录中的配置文件(
.matplotlib/matplotlibrc
)进行设置 .要在代码中设置后端,您可以执行此操作
其中'Agg'是后端的名称 . 哪些后端存在取决于您的安装和操作系统 .
http://matplotlib.sourceforge.net/faq/installing_faq.html#backends
http://matplotlib.org/users/customizing.html
下面的代码片段适用于Eclipse和Python shell:
现代IPython使用“
--matplotlib
" argument with an optional backend parameter. It defaults to " auto”,这在Mac和Windows上通常都足够好 . 我没有在Ubuntu或任何其他Linux发行版上测试它,但我希望它可以工作 .如果你遇到一个问题,其中
pylab.show()
冻结了IPython窗口(这可能是特定于Mac OS X;不确定),你可以在IPython窗口中使用cmd -c,切换到绘图窗口,它就会爆发 .显然,未来对
pylab.show()
的调用不会冻结IPython窗口,只会是第一次调用 . 不幸的是,我发现每次重新安装matplotlib时,绘图窗口/与show()交互的行为都会发生变化,所以这个解决方案可能并不总是成立 .如果您使用
--pylab
选项启动IPython,则不需要调用show()
或draw()
. 试试这个:--pylab
不再适用于Jupyter,但幸运的是我们可以在ipython_config.py
文件中添加一个调整来获得pylab
以及autoreload
功能 .使用easy_install时的另一种可能性是您需要最新版本的matplotlib . 尝试:
在导入matplotlib或其任何模块之前 .