4

I have been through the process of installing matplotlib on my Ubuntu 12.04 computer. However, when I try to run a test program, I get the following error:

Traceback (most recent call last):
  File "../py33/test.py", line 2, in <module>
    import matplotlib.pyplot as plt
ImportError: No module named 'matplotlib'

I am trying a sample program from matplotlib's website (basic_example). I just don't understand why it says there is not module matplotlib, even though I have definitely installed it (using sudo apt-get install python-matplotlib, and have done sudo apt-get build-dep python-matplotlib; I also tried pip install matplotlib, as I was told to do that, after installing Python itself, if I wanted to install other binaries).

Clint
  • 45

3 Answers3

8

If you want to install Matplotlib for Python 3 through the APT package manager, you need the package python3-matplotlib:

sudo apt-get install python3-matplotlib

If you want to install it with Pip for Python 3, you need to use pip3:

sudo pip3 install matplotlib
David Foerster
  • 36,890
  • 56
  • 97
  • 151
2

I believe you are using python3. So in order to run your script you need either to use python2.x or to make sure you have matplotlib v1.2 or higher. This is because matplotlib does not support python3 before v1.2.

I am using matplotlib v1.1 with python2.7 and I have no problem. I tried to do a simple import matplotlib with python3 and it gives the same error as you.

Buddyshot
  • 1,378
2

Cite:

...even though I have definitely installed it (using sudo apt-get install python-matplotlib, and have done sudo apt-get build-dep python-matplotlib; I also tried pip install matplotlib, as I was told to do that, after installing Python itself, if I wanted to install other binaries).

Possible solution:

Taken from the official web page (https://matplotlib.org/users/installing.html ), try this:

python -mpip install -U pip

python -mpip install -U matplotlib

(first line installs pip, a manager installer for libraries, second line install matplotlib itself).

Commentaries:

I had same problem at my Ubuntu 16.04, this works very well for that version.

Best regards,