简体   繁体   English

“将 matplotlib.pyplot 导入为 plt”时出错

[英]Error when "import matplotlib.pyplot as plt"

I am very new to python.我对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.我使用“plt”没有任何问题,但是当我导入它时它突然显示错误消息并且不起作用。 Please see the below.请参阅以下内容。

    >>> 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.非常感谢您提前。

Try to type sudo apt-get install python-matplotlib in a terminal.尝试在终端中输入sudo apt-get install python-matplotlib In your message you typed it in ipython console.在您的消息中,您在 ipython 控制台中输入了它。 This is a bash command, not a python one.这是一个 bash 命令,而不是 python 命令。

You need to quit ipython before.您需要先退出 ipython。 For that use Ctrl + D为此,请使用Ctrl + D

I have this problem before.我以前有这个问题。

What the conditions of mine is that I have install multiply versions of python and numpy in my OS.我的条件是我在我的操作系统中安装了多个版本的 python 和 numpy That's why the python can't find the right numpy to support the matplotlib.这就是为什么python找不到正确的numpy来支持matplotlib的原因。

The method I choose to solved this problem is the modify the $PYTHONPATH by editing the file .bashrc in 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!!祝你好运!!

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的前面。

for terminal: if you are using Python 2, then write this: pip install matplotlib对于终端:如果你使用的是 Python 2,那么写这个:pip install matplotlib

if you are using Python 3, then write this: pip3 install matplotlib如果你使用的是 Python 3,那么写这个: pip3 install matplotlib

尝试这样做

python3 -m pip install matplotlib

The correct answer to resolve this issue was found at the link below: https://githubmemory.com/repo/microsoft/pylance-release/issues/1656 via a comment by jakebailey.在以下链接中找到了解决此问题的正确答案: https : //githubmemory.com/repo/microsoft/pylance-release/issues/1656通过 jakebailey 的评论。 He says, "Did you install matplotlib into the environment while VS Code was running? If you reload, does this message go away?"他说,“你有没有在 VS Code 运行时将 matplotlib 安装到环境中?如果你重新加载,这个消息会消失吗?”

VSCode doesn't refresh anything. VSCode 不会刷新任何东西。 So, after you install anything via pip or pip3, it is a good idea to:因此,在通过 pip 或 pip3 安装任何内容后,最好执行以下操作:

  1. Save your work.保存您的工作。
  2. Close VScode.关闭 VScode。
  3. Restart VScode.重新启动 VScode。

This fixed it for me.这为我修好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM