尽管我调用pyplot.show(),但matplotlib不会显示我的绘图

问题:尽管我调用pyplot.show(),但matplotlib不会显示我的绘图

matplotlib上需要帮助。是的,我没有忘记调用pyplot.show()。

$ ipython –pylab

import matplotlib.pyplot as p 
p.plot(range(20), range(20))

matplotlib.lines.Line2D at 0xade2b2c作为输出返回。

p.show()

没事了 没有错误讯息。没有新窗口。没有。我matplotlib使用pip进行安装,但未收到任何错误消息。

细节:

我用,

  • 的Ubuntu
  • IPython v0.11
  • Python v2.6.6
  • matplotlib v1.0.1

Help required on matplotlib. Yes, I did not forget calling the pyplot.show().

$ ipython –pylab

import matplotlib.pyplot as p 
p.plot(range(20), range(20))

It returns matplotlib.lines.Line2D at 0xade2b2c as the output.

p.show()

There is nothing to happen. No error message. No new window. Nothing. I install matplotlib by using pip and I didn’t take any error messages.

Details:

I use,

  • Ubuntu
  • IPython v0.11
  • Python v2.6.6
  • matplotlib v1.0.1

回答 0

如果将后端设置为template~/.matplotlib/matplotlibrc,则可以重现您的症状:

〜/ .matplotlib / matplotlibrc:

# backend      : GtkAgg
backend      : template

请注意,该文件matplotlibrc可能不在目录中~/.matplotlib/。在这种情况下,以下代码显示其位置:

>>> import matplotlib
>>> matplotlib.matplotlib_fname()

In [1]: import matplotlib.pyplot as p

In [2]: p.plot(range(20),range(20))
Out[2]: [<matplotlib.lines.Line2D object at 0xa64932c>]

In [3]: p.show()

如果您~/.matplotlib/matplotlibrc将后端编辑为,并将其更改为GtkAgg,则应该会看到一个图。您可以使用以下命令列出计算机上所有可用的后端

import matplotlib.rcsetup as rcsetup
print(rcsetup.all_backends)

它应该返回类似以下的列表:

['GTK', 'GTKAgg', 'GTKCairo', 'FltkAgg', 'MacOSX', 'QtAgg', 'Qt4Agg',
'TkAgg', 'WX', 'WXAgg', 'CocoaAgg', 'agg', 'cairo', 'emf', 'gdk', 'pdf',
'ps', 'svg', 'template']

参考:

If I set my backend to template in ~/.matplotlib/matplotlibrc, then I can reproduce your symptoms:

~/.matplotlib/matplotlibrc:

# backend      : GtkAgg
backend      : template

Note that the file matplotlibrc may not be in directory ~/.matplotlib/. In this case, the following code shows where it is:

>>> import matplotlib
>>> matplotlib.matplotlib_fname()

In [1]: import matplotlib.pyplot as p

In [2]: p.plot(range(20),range(20))
Out[2]: [<matplotlib.lines.Line2D object at 0xa64932c>]

In [3]: p.show()

If you edit ~/.matplotlib/matplotlibrc and change the backend to something like GtkAgg, you should see a plot. You can list all the backends available on your machine with

import matplotlib.rcsetup as rcsetup
print(rcsetup.all_backends)

It should return a list like:

['GTK', 'GTKAgg', 'GTKCairo', 'FltkAgg', 'MacOSX', 'QtAgg', 'Qt4Agg',
'TkAgg', 'WX', 'WXAgg', 'CocoaAgg', 'agg', 'cairo', 'emf', 'gdk', 'pdf',
'ps', 'svg', 'template']

Reference:


回答 1

我在Ubuntu 12.04上遇到了完全相同的问题,因为我使用以下命令安装了matplotlib(在virtualenv中)

pip install matplotlib

长话短说,我的建议是:不要尝试使用pip或手工安装matplotlib;让真正的软件包管理器(例如apt-get / synaptic)为您安装它及其所有依赖项。

不幸的是,matplotlib的后端(用于实际绘制图的替代方法)具有pip无法处理的各种依赖关系。更糟糕的是,它无声地失败了。也就是说,pip install matplotlib似乎成功安装了matplotlib。但是,当您尝试使用它时(例如pyplot.show()),将不会出现绘图窗口。我尝试了网络上人们建议的所有不同后端(Qt4Agg,GTK等),但它们都失败了(即,当我尝试导入matplotlib.pyplot时,我得到了,ImportError因为它试图导入缺少的某些依赖项)。然后,我研究了如何安装这些依赖项,但这只是让我想放弃使用pip(在virtualenv内)作为任何具有非Python软件包依赖项的软件包的可行安装解决方案。

整个经历使我爬回apt-get / synaptic(即Ubuntu软件包管理器)来安装matplotlib之类的软件。那很好。当然,这意味着您只能安装到您的系统目录中,没有virtualenv的好处,并且您受困于Ubuntu发行的版本,这可能落后于当前版本…

I ran into the exact same problem on Ubuntu 12.04, because I installed matplotlib (within a virtualenv) using

pip install matplotlib

To make long story short, my advice is: don’t try to install matplotlib using pip or by hand; let a real package manager (e.g. apt-get / synaptic) install it and all its dependencies for you.

Unfortunately, matplotlib’s backends (alternative methods for actually rendering your plots) have all sorts of dependencies that pip will not deal with. Even worse, it fails silently; that is, pip install matplotlib appears to install matplotlib successfully. But when you try to use it (e.g. pyplot.show()), no plot window will appear. I tried all the different backends that people on the web suggest (Qt4Agg, GTK, etc.), and they all failed (i.e. when I tried to import matplotlib.pyplot, I get ImportError because it’s trying to import some dependency that’s missing). I then researched how to install those dependencies, but it just made me want to give up using pip (within virtualenv) as a viable installation solution for any package that has non-Python package dependencies.

The whole experience sent me crawling back to apt-get / synaptic (i.e. the Ubuntu package manager) to install software like matplotlib. That worked perfectly. Of course, that means you can only install into your system directories, no virtualenv goodness, and you are stuck with the versions that Ubuntu distributes, which may be way behind the current version…


回答 2

%matplotlib内联

对于使用笔记本的我来说,在绘图工作之前添加以上行。

%matplotlib inline

For me working with notebook, adding the above line before the plot works.


回答 3

备查,

我遇到了同样的问题-pylab没有在ipython下显示。通过更改ipython的配置文件{ipython_config.py}已解决了该问题。在配置文件中

c.InteractiveShellApp.pylab = 'auto'

我将’auto’更改为’qt’,现在我看到了图表

For future reference,

I have encountered the same problem — pylab was not showing under ipython. The problem was fixed by changing ipython’s config file {ipython_config.py}. In the config file

c.InteractiveShellApp.pylab = 'auto'

I changed ‘auto’ to ‘qt’ and now I see graphs


回答 4

只需输入:

plt.ion()

请在23:30 参见https://www.youtube.com/watch?v=1zmV8lZsHF4

plt使用是因为我的导入:import matplotlib.pyplotas plt

我在带有iTerm2的Mac上使用python2.7。

Just type:

plt.ion()

See https://www.youtube.com/watch?v=1zmV8lZsHF4 at 23:30 !

plt is used because of my import: import matplotlib.pyplot as plt

I’m using python2.7 on a mac with iTerm2.


回答 5

解决我问题的方法只是在顶部的ipython Notebook中使用以下两行

%matplotib inline
%pylab inline

而且有效。我正在使用Ubuntu16.04和ipython-5.1

What solved my problem was just using the below two lines in ipython notebook at the top

%matplotib inline
%pylab inline

And it worked. I’m using Ubuntu16.04 and ipython-5.1


回答 6

我必须从源代码安装matplotlib才能使它工作。关键说明(来自http://www.pyimagesearch.com/2015/08/24/resolved-matplotlib-figures-not-showing-up-or-displaying/)为:

$ workon plotting
$ pip uninstall matplotlib
$ git clone https://github.com/matplotlib/matplotlib.git
$ cd matplotlib
$ python setup.py install

正如@unutbu所说,通过更改后端,我遇到了很多问题,所有不同的后端都不起作用。

I had to install matplotlib from source to get this to work. The key instructions (from http://www.pyimagesearch.com/2015/08/24/resolved-matplotlib-figures-not-showing-up-or-displaying/) are:

$ workon plotting
$ pip uninstall matplotlib
$ git clone https://github.com/matplotlib/matplotlib.git
$ cd matplotlib
$ python setup.py install

By changing the backend, as @unutbu says, I just ran into loads more problems with all the different backends not working either.


回答 7

在导入pylab之前添加以下两行似乎对我有用

import matplotlib
matplotlib.use("gtk")

import sys
import pylab
import numpy as np

Adding the following two lines before importing pylab seems to work for me

import matplotlib
matplotlib.use("gtk")

import sys
import pylab
import numpy as np

回答 8

确保启用此启动脚本:(“首选项”>“控制台”>“高级选项”)

/usr/lib/python2.7/dist-packages/spyderlib/scientific_startup.py

如果启用了标准的PYTHONSTARTUP,则不会有交互式绘图

Be sure to have this startup script enabled : ( Preferences > Console > Advanced Options )

/usr/lib/python2.7/dist-packages/spyderlib/scientific_startup.py

If the standard PYTHONSTARTUP is enabled you won’t have an interactive plot


回答 9

类似@Rikki,我通过升级解决了这个问题matplotlibpip install matplotlib --upgrade。如果无法升级,则可以卸载并重新安装。

pip uninstall matplotlib
pip install matplotlib

Similar to @Rikki, I solved this problem by upgrading matplotlib with pip install matplotlib --upgrade. If you can’t upgrade uninstalling and reinstalling may work.

pip uninstall matplotlib
pip install matplotlib

回答 10

对我来说,如果我只是在macOS 下创建一个 matplotlibrc文件,就会发生问题~/.matplotlib。在其中添加“后端:macosx”可解决此问题。

我认为这是一个错误:如果backend未在我的代码中指定,matplotlibrc则应采用默认值。

For me the problem happens if I simply create an empty matplotlibrc file under ~/.matplotlib on macOS. Adding “backend: macosx” in it fixes the problem.

I think it is a bug: if backend is not specified in my matplotlibrc it should take the default value.


回答 11

运行代码后,包括:

import pylab as p
p.show()

After running your code include:

import pylab as p
p.show()

回答 12

我发现我需要window = Tk(),然后window.mainloop()

I found that I needed window = Tk() and then window.mainloop()


回答 13

对于Ubuntu 12.04:

sudo apt-get install python-qt4
virtualenv .env --no-site-packages
source .env/bin/activate
easy_install -U distribute
ln -s /usr/lib/python2.7/dist-packages/PyQt4 .
ln -s /usr/lib/python2.7/dist-packages/sip.so .
pip install matplotlib

For Ubuntu 12.04:

sudo apt-get install python-qt4
virtualenv .env --no-site-packages
source .env/bin/activate
easy_install -U distribute
ln -s /usr/lib/python2.7/dist-packages/PyQt4 .
ln -s /usr/lib/python2.7/dist-packages/sip.so .
pip install matplotlib