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.

我错过了什么吗?

我正在分析的函数是 run(),它在里面:file_to_be_profiled.py

#!/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())

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

#!/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 更新(有一些防火墙问题,然后我成功地做到了: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 托管机器。

4

4 回答 4

3

您需要matplotlib按照说明进行安装(找不到时抛出此错误mprofmatplotlib)。

您可以使用 pip pip install matplotlib(您可能需要在此前缀前加上sudo)或您的包管理器来执行此操作:

  • sudo apt-get install -y python-matplotlib在 Debian、Ubuntu 和衍生产品上。
  • sudo yum install -y python-matplotlib在 RHEL、CentOS 和衍生产品上。
于 2015-10-01T11:12:26.300 回答
3

似乎 matplotlib: 3.0.1 在 conda 环境中与 python 3.7 不兼容。df.plot() 命令无法正确执行,显示“ImportError: matplotlib is required for plotting”,但实际上已安装 matplotlib。

我试图将 matplotlib 降级到 3.0.0 然后一切都很好。

conda install -n [your_conda_envionment] matplotlib==3.0.0

于 2018-11-01T00:28:53.937 回答
1

我在新安装的 Ubuntu 16.04 上遇到了同样的问题。为了解决这个问题,我安装了这样的 SciPy: sudo pip install scipy

我也必须像这样安装tkinterlib: sudo apt-get install python-tk

我猜在过去的某个地方,matplotlib 有scipy依赖,但现在它只有 numpy,而mprof plotrequires scipy.

于 2017-10-16T11:18:20.000 回答
0

我有同样的问题。我使用 memory_profiler lib 键入此命令(mprof run runner.pymprof plot)并收到一条消息“绘图需要 matplotlib”

我试图按照我在这里找到的建议解决这个问题,但结果我完成了这些简单的步骤解决了这个问题:

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

  2. 用于sudo yum-builddep python-matplotlib安装连接的包(安装了222个包,如果你需要apt-get,你需要这个命令sudo apt-get build-dep python-matplotlib:)

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

我相信这会有所帮助。

于 2017-07-31T08:15:42.457 回答