Python 2.7:无法导入 matplotlib.pyplot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31066496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:25:59  来源:igfitidea点击:

Python 2.7: Unable to import matplotlib.pyplot

pythonpython-2.7matplotlibimportspyder

提问by Nero

When I tried to

当我试图

import matplotlib.pyplot as plt

I was given the following error:

我收到以下错误:

runfile('/Users/Zhengnan/Documents/Python/PS 2/230I_PS_2.py', wdir='/Users/Zhengnan/Documents/Python/PS 2')
Traceback (most recent call last):

File "<ipython-input-2-9001323bba15>", line 1, in <module>
runfile('/Users/Zhengnan/Documents/Python/PS 2/230I_PS_2.py', wdir='/Users/Zhengnan/Documents/Python/PS 2')

File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)

File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
builtins.execfile(filename, *where)

File "/Users/Zhengnan/Documents/Python/PS 2/230I_PS_2.py", line 4, in <module>
import matplotlib.pyplot as plt

File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 27, in <module>
import matplotlib.colorbar

File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/colorbar.py", line 34, in <module>
import matplotlib.collections as collections

File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/collections.py", line 27, in <module>
import matplotlib.backend_bases as backend_bases

File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 56, in <module>
import matplotlib.textpath as textpath

File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/textpath.py", line 22, in <module>
from matplotlib.mathtext import MathTextParser

File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/mathtext.py", line 63, in <module>
import matplotlib._png as _png

ImportError: dlopen(/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/_png.so, 2): Library not loaded: @loader_path/../../../libpng15.15.dylib
Referenced from: /Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/_png.so
Reason: image not found

Can anyone please help me? I'm a newbie and not really familiar with paths. A step-by-step instruction would be greatly appreciated. Thanks in advance.

谁能帮帮我吗?我是新手,对路径不是很熟悉。分步说明将不胜感激。提前致谢。

BTW, I'm using spyder as an IDE.

顺便说一句,我使用 spyder 作为 IDE。

回答by Nero

It's likely your library path is not set up correctly, and doesn't point to wherever the file libpng15.15.dylibis located.

很可能您的库路径设置不正确,并且没有指向文件libpng15.15.dylib所在的位置。

Since this file should probably come with the anaconda installation (provided this is how you have installed Python and matplotlib), check if you can find this file in /Users/Zhangnan/anaconda/lib/.

由于该文件可能应该与 anaconda 安装一起提供(前提是您安装 Python 和 matplotlib 的方式),请检查是否可以在/Users/Zhangnan/anaconda/lib/.

If you find that file there, set your LD_LIBRARY_PATH:

如果您在那里找到该文件,请设置您的LD_LIBRARY_PATH

export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/Users/Zhangnan/anaconda/lib/

You may also want to add that line to your ${HOME}/.bashrcfile.

您可能还想将该行添加到您的${HOME}/.bashrc文件中。

回答by aquagremlin

as presented here: https://askubuntu.com/questions/636937/python-2-7-matplotlib-provides-errorpyplot

如此处所示:https: //askubuntu.com/questions/636937/python-2-7-matplotlib-provides-errorpyplot

this command will fix it: conda install matplotlib

此命令将修复它: conda install matplotlib

and indeed it helped me.

它确实帮助了我。

回答by LuanMinh

I use Kali Linux,I use: pip install matplotlib

我使用 Kali Linux,我使用:pip install matplotlib