无法绘制图形:需要使用matplotlib进行绘图。

7

我能够生成*.dat文件:

vikas@server:~/memory_profiler-0.36$ ./mprof run --python test_sl.py 
vikas@server:~/memory_profiler-0.36$ ls *.dat
mprofile_20151001035123.dat

但是当我尝试绘制图形时,它显示“需要matplotlib才能绘图”。

vikas@server:~/memory_profiler-0.36$ ./mprof plot --output=plot.png 
matplotlib is needed for plotting.

我有什么遗漏吗?

被我分析的函数是运行在file_to_be_profiled.py文件中的run()。

#!/usr/bin/python

import time
import os, sys, commands
from memory_profiler import profile
from guppy import hpy

@profile
def run():
    d = {}
    l = []
    hp = hpy()
    before = hp.heap()

    d["k1"] = 'val1'
    d["k2"] = 10
    count = 0
    while (count < 9):
        l.append(count)
        print 'The count is:', count
        count = count + 1
    print "Good bye!"
    after = hp.heap()
    leftover = after - before
    print leftover

if __name__ == '__main__':
    sys.exit(run())

我正在从我的测试脚本test_sl.py中调用run():

#!/usr/bin/python

import commands

# for the time being I am running my test to profile method: run() inside file_to_be_profiled.py
run_cmd = './file_to_be_profiled.py
commands.getstatusoutput(run_cmd)

我正在运行ubuntu机器。但apt-get install没有起作用。然后我从源代码进行安装。但仍然出现相同的错误。

apt-get失败:

vikas@server:~/memory_profiler-0.36$ sudo apt-get install -y python-matplotlib
[sudo] password for vikaskuk:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package python-matplotlib is not available, but is referred to by another package.

This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'python-matplotlib' has no installation candidate
vikask@server:~/memory_profiler-0.36$ ./mprof plot --output=plot.png
matplotlib is needed for plotting.

然后我尝试了 pip install...,但也没有成功。
vikas@server:~/memory_profiler-0.36$ pip install matplotlib
Downloading/unpacking matplotlib
  Cannot fetch index base URL https://pypi.python.org/simple/
  Could not find any downloads that satisfy the requirement matplotlib
Cleaning up...
No distributions at all found for matplotlib
Storing debug log for failure in /home/vikaskuk/.pip/pip.log

最后我尝试了从源代码安装,似乎成功了:

vikas@server:~/memory_profiler-0.36$ cd ../matplotlib
vikas@server:~/matplotlib$ cd matplotlib-master/

(virtualenv)vikaskuk@beehive:~/matplotlib/matplotlib-master$ vi INSTALL
(virtualenv)vikaskuk@beehive:~/matplotlib/matplotlib-master$ python setup.py build
============================================================================
Edit setup.cfg to change the build options

BUILDING MATPLOTLIB
            matplotlib: yes [1.5.dev1]
                python: yes [2.6.6 (r266:84292, Sep 15 2010, 16:22:56)  [GCC
                        4.4.5]]
              platform: yes [linux2]

REQUIRED DEPENDENCIES AND EXTENSIONS
                 numpy: yes [not found. pip may install it below.]
                   six: yes [using six version 1.5.2]
              dateutil: yes [using dateutil version 2.1]
                  pytz: yes [pytz was not found. pip will attempt to install
                        it after matplotlib.]
               tornado: yes [tornado was not found. It is required for the
                        WebAgg backend. pip/easy_install may attempt to
                        install it after matplotlib.]
             pyparsing: yes [pyparsing was not found. It is required for
                        mathtext support. pip/easy_install may attempt to
                        install it after matplotlib.]
                libagg: yes [pkg-config information for 'libagg' could not
                        be found. Using local copy.]
              freetype: no  [The C/C++ header for freetype2 (ft2build.h)
                        could not be found.  You may need to install the
                        development package.]
                   png: no  [pkg-config information for 'libpng' could not
                        be found.]
                 qhull: yes [pkg-config information for 'qhull' could not be
                        found. Using local copy.]

OPTIONAL SUBPACKAGES
           sample_data: yes [installing]
              toolkits: yes [installing]
                 tests: yes [using nose version 1.3.0 / using mock 1.0.1]
        toolkits_tests: yes [using nose version 1.3.0 / using mock 1.0.1]

OPTIONAL BACKEND EXTENSIONS
                macosx: no  [Mac OS-X only]
                qt5agg: no  [PyQt5 not found]
                qt4agg: no  [PySide not found; PyQt4 not found]
               gtk3agg: no  [Requires pygobject to be installed.]
             gtk3cairo: no  [Requires cairocffi or pycairo to be installed.]
                gtkagg: no  [Requires pygtk]
                 tkagg: no  [The C/C++ header for Tk (tk.h) could not be
                        found.  You may need to install the development
                        package.]
                 wxagg: no  [requires wxPython]
                   gtk: no  [Requires pygtk]
                   agg: yes [installing]
                 cairo: no  [cairocffi or pycairo not found]
             windowing: no  [Microsoft Windows only]

OPTIONAL LATEX DEPENDENCIES
                dvipng: no
           ghostscript: yes [version 8.71]
                 latex: yes [version 3.1415926]
               pdftops: no

============================================================================
                        * The following required packages can not be built:
                        * freetype, png
(virtualenv)vikaskuk@beehive:~/matplotlib/matplotlib-master$ python setup.py install
============================================================================
Edit setup.cfg to change the build options

BUILDING MATPLOTLIB
            matplotlib: yes [1.5.dev1]
                python: yes [2.6.6 (r266:84292, Sep 15 2010, 16:22:56)  [GCC
                        4.4.5]]
              platform: yes [linux2]

REQUIRED DEPENDENCIES AND EXTENSIONS
                 numpy: yes [not found. pip may install it below.]
                   six: yes [using six version 1.5.2]
              dateutil: yes [using dateutil version 2.1]
                  pytz: yes [pytz was not found. pip will attempt to install
                        it after matplotlib.]
               tornado: yes [tornado was not found. It is required for the
                        WebAgg backend. pip/easy_install may attempt to
                        install it after matplotlib.]
             pyparsing: yes [pyparsing was not found. It is required for
                        mathtext support. pip/easy_install may attempt to
                        install it after matplotlib.]
                libagg: yes [pkg-config information for 'libagg' could not
                        be found. Using local copy.]
              freetype: no  [The C/C++ header for freetype2 (ft2build.h)
                        could not be found.  You may need to install the
                        development package.]
                   png: no  [pkg-config information for 'libpng' could not
                        be found.]
                 qhull: yes [pkg-config information for 'qhull' could not be
                        found. Using local copy.]

OPTIONAL SUBPACKAGES
           sample_data: yes [installing]
              toolkits: yes [installing]
                 tests: yes [using nose version 1.3.0 / using mock 1.0.1]
        toolkits_tests: yes [using nose version 1.3.0 / using mock 1.0.1]

OPTIONAL BACKEND EXTENSIONS
                macosx: no  [Mac OS-X only]
                qt5agg: no  [PyQt5 not found]
                qt4agg: no  [PySide not found; PyQt4 not found]
               gtk3agg: no  [Requires pygobject to be installed.]
             gtk3cairo: no  [Requires cairocffi or pycairo to be installed.]
                gtkagg: no  [Requires pygtk]
                 tkagg: no  [The C/C++ header for Tk (tk.h) could not be
                        found.  You may need to install the development
                        package.]
                 wxagg: no  [requires wxPython]
                   gtk: no  [Requires pygtk]
                   agg: yes [installing]
                 cairo: no  [cairocffi or pycairo not found]
             windowing: no  [Microsoft Windows only]

OPTIONAL LATEX DEPENDENCIES
                dvipng: no
           ghostscript: yes [version 8.71]
                 latex: yes [version 3.1415926]
               pdftops: no

============================================================================
                        * The following required packages can not be built:
                        * freetype, png

但是当我尝试绘制图形时,我遇到了最初的问题:

vikas@server:~/matplotlib/matplotlib-master$ cd ../../memory_profiler-0.36
vikas@server:~/memory_profiler-0.36$ ./mprof plot --output=plot.png 
matplotlib is needed for plotting.

我能成功执行apt-get update(可能是由于防火墙问题)并成功执行sudo apt-get install -y python-matplotlib。

但是在运行mprof时仍然遇到了一些问题:

vikas@server:~/memory_profiler-0.36$ ./mprof plot --output=plot.png
Using last profile data.
Traceback (most recent call last):
  File "./mprof", line 494, in <module>
    actions[get_action()]()
  File "./mprof", line 454, in plot_action
    pl.figure(figsize=(14, 6), dpi=90)
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 423, in figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 79, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 87, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

为什么需要设置显示器?我正在使用托管在Ubuntu上的机器。


1
其实,你的源代码构建也没有成功。你的机器上没有安装freetypelibpng,因此无法构建matplotlibpip install遇到了另一个问题(你是否在防火墙后面?),但很快也会遇到相同的问题。我不确定为什么你在通过包管理器安装时遇到问题,但是使用apt-get install libfreetype6-devlibpng-devpython-tk,然后你应该能够从源代码构建。 - Joe Kington
4个回答

3

按照指示,您需要安装matplotlibmprof找不到matplotlib时会抛出此错误)。

您可以使用pip来安装matplotlibpip install matplotlib(您可能需要在前面加上sudo),也可以使用您的软件包管理器:

  • 在Debian、Ubuntu及其衍生产品上,使用sudo apt-get install -y python-matplotlib
  • 在RHEL、CentOS及其衍生产品上,使用sudo yum install -y python-matplotlib

我正在运行Ubuntu机器。但是apt-get install对我没有起作用。然后我从源代码进行了安装。编辑问题并附上我所做的步骤。 - vikas
@vikas 请确保您解释为什么apt-get install对您无效。是否有错误信息? - Thomas Orozco
可能是我的组织中有某种防火墙...这就是为什么apt-get会失败的原因。因此,我尝试了源代码安装,看起来似乎成功了。但是,原始问题仍然存在。请查看我的原始问题...我现在已经编辑了我所做的所有内容。 - vikas
1
@vikas 不,源代码安装失败了(请查看输出的末尾)。在运行apt-get install之前,请尝试运行sudo apt-get update。您最终需要让apt-get正常工作,因为您缺少许多matplotlib的依赖项,而apt-get是实际上唯一获取它们的方法。 - Thomas Orozco
2
成功执行了apt-get update之后,我能够安装matplotlib...但是mprof仍然抱怨显示问题。请查看原始问题。我已经引用了我遇到的错误。 - vikas

3

看起来在conda环境中,matplotlib: 3.0.1 和python 3.7不兼容。df.plot()命令无法正常执行,显示“ImportError: matplotlib is required for plotting”,但实际上已经安装了matplotlib。

我尝试将matplotlib降级到3.0.0,然后一切都正常了。

可以使用以下命令在您的conda环境中安装matplotlib 3.0.0:

conda install -n [your_conda_envionment] matplotlib==3.0.0


1

我在刚安装好的Ubuntu 16.04上遇到了同样的问题。为了解决它,我这样安装了SciPy:

sudo pip install scipy

此外,我还需要像这样安装tkinter库:

sudo apt-get install python-tk

我猜在过去的某个时候,matplotlib有scipy作为依赖项,但现在它只有numpy,而mprof plot需要scipy


0

我曾经遇到过同样的问题。我使用了memory_profiler库并输入以下命令(mprof run runner.pymprof plot),但是出现了“需要matplotlib才能绘图”的提示。

我试着按照在这里找到的建议来解决这个问题,最终通过以下简单步骤解决了这个问题:

  1. 删除与matplotlib相关的/usr/lib64/python2.7/site-packages/文件(或者你可以使用普通方式卸载matplotlib)

  2. 使用sudo yum-builddep python-matplotlib安装相关软件包(共安装了222个软件包,如果你需要apt-get,则需要使用以下命令:sudo apt-get build-dep python-matplotlib

  3. 安装matplotlib sudo yum reinstall python-matplotlib(或者使用安装命令,对于apt-get:sudo apt-get install python-matplotlib

我相信这会有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接