Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when "import matplotlib.pyplot as plt"

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.

    >>> 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.

    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.

like image 884
user2928318 Avatar asked Jun 19 '14 08:06

user2928318


3 Answers

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.

You need to quit ipython before. For that use Ctrl + D

like image 145
Fritzip Avatar answered Nov 16 '22 19:11

Fritzip


I have this problem before.

What the conditions of mine is that I have install multiply versions of python and numpy in my OS. That's why the python can't find the right numpy to support the matplotlib.

The method I choose to solved this problem is the modify the $PYTHONPATH by editing the file .bashrc in my home path.

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.

Maybe you can find something amazing. Good luck for you!!

like image 4
Brook Wong Avatar answered Nov 16 '22 19:11

Brook Wong


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. He says, "Did you install matplotlib into the environment while VS Code was running? If you reload, does this message go away?"

VSCode doesn't refresh anything. So, after you install anything via pip or pip3, it is a good idea to:

  1. Save your work.
  2. Close VScode.
  3. Restart VScode.

This fixed it for me.

like image 1
stdntuvmath Avatar answered Nov 16 '22 20:11

stdntuvmath