matplotlib 中没有绘图窗口
我刚刚使用 synaptic 软件包系统在 Ubuntu 9.10 中安装了 matplotlib。 但是,当我尝试以下简单示例时,
>>> from pylab import plot;
>>> plot([1,2,3],[1,2,3])
[<matplotlib.lines.Line2D object at 0x9aa78ec>]
我没有得到绘图窗口。关于如何显示绘图窗口有什么想法吗?
I just installed matplotlib in Ubuntu 9.10 using the synaptic package system.
However, when I try the following simple example
>>> from pylab import plot;
>>> plot([1,2,3],[1,2,3])
[<matplotlib.lines.Line2D object at 0x9aa78ec>]
I get no plot window. Any ideas on how to get the plot window to show?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您可以输入
或更好地使用
ipython -pylab
。由于使用
pylab
不再推荐,现在的解决方案是You can type
or better, use
ipython -pylab
.Since the use of
pylab
is not recommended anymore, the solution would nowadays bepylab.show()
可以工作,但会阻塞(您需要关闭窗口)。一个更方便的解决方案是在启动时执行
pylab.ion()
(交互式模式):所有(pylab 的等效项)pyplot.*
命令都会显示其绘图立即地。 有关交互模式的更多信息可以在官方网站。我还使用了更方便的
ipython -pylab
(--pylab
,在较新的版本中),它允许您跳过from ... import ...
code> 部分(%pylab
也适用于较新的 IPython 版本)。pylab.show()
works but blocks (you need to close the window).A much more convenient solution is to do
pylab.ion()
(interactive mode on) when you start: all (the pylab equivalents of)pyplot.*
commands display their plot immediately. More information on the interactive mode can be found on the official web site.I also second using the even more convenient
ipython -pylab
(--pylab
, in newer versions), which allows you to skip thefrom … import …
part (%pylab
works, too, in newer IPython versions).试试这个:
导入 pylab 之前
Try this:
BEFORE import pylab
下面的代码片段适用于 Eclipse 和 Python shell:
The code snippet below works on both Eclipse and the Python shell:
有什么错误出现吗?这可能是没有设置后端的问题。您可以从 Python 解释器或主目录中的配置文件 (
.matplotlib/matplotlibrc
) 进行设置。要在代码中设置后端,您可以执行
以下操作,其中“Agg”是后端的名称。存在哪些后端取决于您的安装和操作系统。
http://matplotlib.sourceforge.net/faq/installing_faq.html#backends
http://matplotlib.org/users/customizing.html
Any errors show up? This might an issue of not having set the backend. You can set it from the Python interpreter or from a config file (
.matplotlib/matplotlibrc
) in you home directory.To set the backend in code you can do
where 'Agg' is the name of the backend. Which backends are present depend on your installation and OS.
http://matplotlib.sourceforge.net/faq/installing_faq.html#backends
http://matplotlib.org/users/customizing.html
现代 IPython 使用“
--matplotlib
”参数和可选的后端参数。它默认为“自动”,这在 Mac 和 Windows 上通常就足够了。我还没有在 Ubuntu 或任何其他 Linux 发行版上测试过它,但我希望它能够工作。Modern IPython uses the "
--matplotlib
" argument with an optional backend parameter. It defaults to "auto", which is usually good enough on Mac and Windows. I haven't tested it on Ubuntu or any other Linux distribution, but I would expect it to work.如果您遇到
pylab.show()
冻结 IPython 窗口的问题(这可能是 Mac OS X 特定的;不确定),您可以在 IPython 窗口中使用 cmd-c,切换到情节窗口,它就会爆发。显然,以后对 pylab.show() 的调用不会冻结 IPython 窗口,只会冻结第一次调用。不幸的是,我发现每次重新安装 matplotlib 时,绘图窗口的行为/与 show() 的交互都会发生变化,因此该解决方案可能并不总是有效。
If you encounter an issue in which
pylab.show()
freezes the IPython window (this may be Mac OS X specific; not sure), you can cmd-c in the IPython window, switch to the plot window, and it will break out.Apparently, future calls to
pylab.show()
will not freeze the IPython window, only the first call. Unfortunately, I've found that the behavior of the plot window / interactions with show() changes every time I reinstall matplotlib, so this solution may not always hold.如果您使用
--pylab
选项启动 IPython,则不需要调用show()
或draw()
。试试这个:If you are starting IPython with the
--pylab
option, you shouldn't need to callshow()
ordraw()
. Try this:--pylab
不再适用于 Jupyter,但幸运的是我们可以在ipython_config.py
文件中添加调整,以获得pylab
以及autoreload
功能。--pylab
no longer works for Jupyter, but fortunately we can add a tweak in theipython_config.py
file to get bothpylab
as well asautoreload
functionalities.如果您是 Anaconda 和 Spyder 的用户,那么最适合您的解决方案是:
工具
-->
偏好设置
-->
Ipython控制台
-->
图形部分
然后在图形支持 (Matplotlib) 部分:
选择两个可用选项
,然后在图形后端中:
选择自动
If you are user of Anaconda and Spyder then best solution for you is that :
Tools
-->
Preferences
-->
Ipython console
-->
Graphic Section
Then in the Support for graphics (Matplotlib) section:
select two avaliable options
and in the Graphics Backend:
select Automatic
%matplotlib inline
当你的任何库的绘图隐藏在 jupyter 笔记本的后端时,使用此行。
这对我来说效果很好。
%matplotlib inline
use this line whenever your plot with any library hides in the backend of jupyter notebook.
This works fine for me.
使用 easy_install 时的另一种可能性是您需要最新版本的 matplotlib。
尝试:
在导入 matplotlib 或其任何模块之前。
Another possibility when using easy_install is that you need to require the most recent version of matplotlib.
Try:
before you import matplotlib or any of its modules.