Python “将 matplotlib.pyplot 作为 plt 导入”时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24302091/
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
Error when "import matplotlib.pyplot as plt"
提问by user2928318
I am very new to python. So, my problem might be too simple to be solved. But I cannot. Please help me! I did not have any problem to use "plt", but it suddenly shows error message and does not work, when I import it. Please see the below.
我对python很陌生。所以,我的问题可能太简单而无法解决。但是我不能。请帮我!我使用“plt”没有任何问题,但是当我导入它时它突然显示错误消息并且不起作用。请参阅以下内容。
>>> import matplotlib
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line 6, in <module>
from matplotlib.figure import Figure, figaspect
File "/usr/lib64/python2.6/site-packages/matplotlib/figure.py", line 18, in <module>
from axes import Axes, SubplotBase, subplot_class_factory
File "/usr/lib64/python2.6/site-packages/matplotlib/axes.py", line 2, in <module>
import math, sys, warnings, datetime, new
File "new.py", line 12, in <module>
import matplotlib.pyplot as plt
AttributeError: 'module' object has no attribute 'pyplot'
This package is suddenly corrupted? So, I tried to install as below.I use ubuntu.
这个包突然损坏了?所以,我尝试如下安装。我使用ubuntu。
In [1]: sudo apt-get install python-matplotlib
File "<ipython-input-1-2400ac877ebd>", line 1
sudo apt-get install python-matplotlib
^
SyntaxError: invalid syntax
If I need to reinstall, please give me very detailed instruction how to do it. Thank you very much in advance.
如果我需要重新安装,请给我非常详细的操作说明。非常感谢您提前。
回答by Fritzip
Try to type sudo apt-get install python-matplotlib
in a terminal. In your message you typed it in ipython console. This is a bash command, not a python one.
尝试输入sudo apt-get install python-matplotlib
终端。在您的消息中,您在 ipython 控制台中输入了它。这是一个 bash 命令,而不是 python 命令。
You need to quit ipython before. For that use Ctrl + D
您需要先退出 ipython。为了那个用途Ctrl + D
回答by Brook Wong
I have this problem before.
我以前有这个问题。
What the conditions of mine is that I have install multiply versions of python and numpyin my OS. That's why the python can't find the right numpy to support the matplotlib.
我的条件是我在我的操作系统中安装了多个版本的 python 和 numpy。这就是为什么python找不到正确的numpy来支持matplotlib的原因。
The method I choose to solved this problem is the modify the $PYTHONPATHby editing the file .bashrcin my home path.
我选择解决这个问题的方法是通过在我的主路径中编辑文件.bashrc来修改$PYTHONPATH。
Change the order of the python install path shown, like putting the path "/usr/lib/python2.7/dist-packages"in the front of the $PYTHONPATH.
更改显示的 python 安装路径的顺序,例如将路径“/usr/lib/python2.7/dist-packages”放在$PYTHONPATH的前面。
Maybe you can find something amazing. Good luck for you!!
也许你会发现一些惊人的东西。祝你好运!!
回答by Chen Zhang
something wrong with tkinter I guess, try adding:
我猜 tkinter 有问题,请尝试添加:
import matplotlib as mpl
mpl.use('TkAgg')
in the front of import matplotlib.pyplot as plt
.
在前面import matplotlib.pyplot as plt
。