matplotlib 中没有绘图窗口

发布于 2024-08-18 21:14:22 字数 262 浏览 2 评论 0原文

我刚刚使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

ヤ经典坏疍 2024-08-25 21:14:22

您可以输入

import pylab
pylab.show()

或更好地使用ipython -pylab


由于使用 pylab 不再推荐,现在的解决方案是

import matplotlib.pyplot as plt

plt.plot([1,2,3])

plt.show()

You can type

import pylab
pylab.show()

or better, use ipython -pylab.


Since the use of pylab is not recommended anymore, the solution would nowadays be

import matplotlib.pyplot as plt

plt.plot([1,2,3])

plt.show()
我们只是彼此的过ke 2024-08-25 21:14:22

pylab.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 the from … import … part (%pylab works, too, in newer IPython versions).

寻找我们的幸福 2024-08-25 21:14:22

试试这个:

import matplotlib
matplotlib.use('TkAgg') 

导入 pylab 之前

Try this:

import matplotlib
matplotlib.use('TkAgg') 

BEFORE import pylab

风吹短裙飘 2024-08-25 21:14:22

下面的代码片段适用于 Eclipse 和 Python shell:

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()

The code snippet below works on both Eclipse and the Python shell:

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()
新雨望断虹 2024-08-25 21:14:22

有什么错误出现吗?这可能是没有设置后端的问题。您可以从 Python 解释器或主目录中的配置文件 (.matplotlib/matplotlibrc) 进行设置。

要在代码中设置后端,您可以执行

import matplotlib
matplotlib.use('Agg')

以下操作,其中“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

import matplotlib
matplotlib.use('Agg')

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

若相惜即相离 2024-08-25 21:14:22

现代 IPython 使用“--matplotlib”参数和可选的后端参数。它默认为“自动”,这在 Mac 和 Windows 上通常就足够了。我还没有在 Ubuntu 或任何其他 Linux 发行版上测试过它,但我希望它能够工作。

ipython --matplotlib

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.

ipython --matplotlib
烛影斜 2024-08-25 21:14:22

如果您遇到 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.

贪恋 2024-08-25 21:14:22

如果您使用 --pylab 选项启动 IPython,则不需要调用 show()draw()。试试这个:

ipython  --pylab=inline

If you are starting IPython with the --pylab option, you shouldn't need to call show() or draw(). Try this:

ipython  --pylab=inline
清醇 2024-08-25 21:14:22

--pylab 不再适用于 Jupyter,但幸运的是我们可以在 ipython_config.py 文件中添加调整,以获得 pylab 以及 autoreload 功能。

c.InteractiveShellApp.extensions = ['autoreload', 'pylab']
c.InteractiveShellApp.exec_lines = ['%autoreload 2', '%pylab']

--pylab no longer works for Jupyter, but fortunately we can add a tweak in the ipython_config.py file to get both pylab as well as autoreload functionalities.

c.InteractiveShellApp.extensions = ['autoreload', 'pylab']
c.InteractiveShellApp.exec_lines = ['%autoreload 2', '%pylab']
怀里藏娇 2024-08-25 21:14:22

如果您是 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

自此以后,行同陌路 2024-08-25 21:14:22
import numpy as np
import matplotlib.pyplot as plt
import pylab

plt.plot(y)

pylab.show(block=False)

plt.pause(3) #After 3 seconds the plot closes.
import numpy as np
import matplotlib.pyplot as plt
import pylab

plt.plot(y)

pylab.show(block=False)

plt.pause(3) #After 3 seconds the plot closes.
灯下孤影 2024-08-25 21:14:22

%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.

转瞬即逝 2024-08-25 21:14:22

使用 easy_install 时的另一种可能性是您需要最新版本的 matplotlib。
尝试:

import pkg_resources
pkg_resources.require("matplotlib")

在导入 matplotlib 或其任何模块之前。

Another possibility when using easy_install is that you need to require the most recent version of matplotlib.
Try:

import pkg_resources
pkg_resources.require("matplotlib")

before you import matplotlib or any of its modules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文