246

I am currently practicing matplotlib. This is the first example I practice.

#!/usr/bin/python

import matplotlib.pyplot as plt

radius = [1.0, 2.0, 3.0, 4.0]
area = [3.14159, 12.56636, 28.27431, 50.26544]

plt.plot(radius, area)
plt.show()

When I run this script with python ./plot_test.py, it shows plot correctly. However, I run it by itself, ./plot_test.py, it throws the followings:

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

Does python look for matplotlib in different locations?

The environment is:

  • Mac OS X 10.8.4 64bit
  • built-in python 2.7

numpy, scipy, matplotlib is installed with:

sudo port install py27-numpy py27-scipy py27-matplotlib \
py27-ipython +notebook py27-pandas py27-sympy py27-nose
wjandrea
  • 28,235
  • 9
  • 60
  • 81

21 Answers21

238

pip will make your life easy!

Step 1: Install pip - Check if you have pip already simply by writing pip in the python console. If you don't have pip, get a python script called get-pip.py , via here: https://pip.pypa.io/en/latest/installing.html or directly here: https://bootstrap.pypa.io/get-pip.py (You may have to use Save As ..)

Step 2: Take note of where the file got saved and cd the directory from command prompt. Run the get-pip.py script to install pip. You can write in cmd this line within quotes: "python .\get-pip.py"

Step 3: Now in cmd type: pip install matplotlib

And you should be through.

the
  • 21,007
  • 11
  • 68
  • 101
Sheetal Kaul
  • 3,029
  • 2
  • 13
  • 6
  • 37
    In the question, it is specified that the library is already installed – tashuhka Jul 16 '15 at 13:24
  • 9
    There are too many upvotes for this answer as currently written. Instead of downloading pip, since they (being on macosx) already have macports, the OP should run `sudo port install pip`. Second, for `pip` to be useful beyond their existing setup, they should also do `sudo port install virtualenv`, for local python "virtual environments" (maybe called `python3-virtualenv`). Then, after setting up and entering (activating) a new virtualenv, run `pip install ...` inside the virtualenv. Lastly, the python script still needs to be invoked using `#!/usr/bin/env python` for any of this to work. – michael Jul 08 '16 at 04:01
  • on ubuntu I used *python ./get-pip.py --user* and that worked for me. thx – LeleMarieC Feb 10 '19 at 02:39
  • How do I tell pip to install the library for python3? I have py2 and py3 installed and by default pip chooses py2. – Ε Г И І И О Sep 14 '20 at 12:56
  • 1
    Thanks for the 'pip install matplotlib' command! really helpful!! Saved my time!!! – Dev Oct 11 '20 at 21:03
  • @LeleMarieC Have you tried `pip3`? – AnonymousUser Jan 08 '22 at 04:33
  • 2
    I had to run `python3 -m pip install matplotlib` for step 3. – Austin Jenkins Jul 12 '23 at 14:57
64

You have two pythons installed on your machine, one is the standard python that comes with Mac OSX and the second is the one you installed with ports (this is the one that has matplotlib installed in its library, the one that comes with macosx does not).

/usr/bin/python

Is the standard mac python and since it doesn't have matplotlib you should always start your script with the one installed with ports.

If python your_script.py works then change the #! to:

#!/usr/bin/env python

Or put the full path to the python interpreter that has the matplotlib installed in its library.

fpt
  • 358
  • 3
  • 11
Viktor Kerkez
  • 45,070
  • 12
  • 104
  • 85
46

If you are using Python 2, just run

sudo apt-get install python-matplotlib

The best way to get matplotlib is :

pip install matplotlib

cause the previous way may give you a old version of matplotlib

k.biao
  • 509
  • 4
  • 3
42

This worked for me, inspired by Sheetal Kaul

pip uninstall matplotlib
python3 -m pip install matplotlib

I knew it installed in the wrong place when this worked:

python2.7
import matplotlib
Punnerud
  • 7,195
  • 2
  • 54
  • 44
30

use pip3 install matplotlib to install matlplot lib. By default, pip will install those package for 2.7 as it the native one. using pip3 makes it specific for python 3, and make sure you have only one version of python 3

Eliethesaiyan
  • 2,327
  • 1
  • 22
  • 35
19

First check the version of Python

For python2:

sudo apt-get install python-matplotlib

For python3:

sudo apt-get install python3-matplotlib

If you mismatch the Matplotlib installation and the Python version you will get the no-module-error because no module for that version exits.

XavierStuvw
  • 1,294
  • 2
  • 15
  • 30
negi
  • 408
  • 7
  • 13
12

So I used python3 -m pip install matplotlib then import matplotlib.pyplot as plt and it worked.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Arif
  • 309
  • 3
  • 10
10

If you using Anaconda3

Just put

conda install -c conda-forge matplotlib
Ashiq Imran
  • 2,077
  • 19
  • 17
9

If you have pip installed and Python configured to the path variables, Just run this command in the terminal.

pip install matplotlib
Kimanthi K.
  • 595
  • 6
  • 13
7

You can install the matplotlib package in python 3 by doing this

python3 -m pip install matplotlib --user

It's working for me.

5

I had a similar problem, using pip3 and all these things worked for installing matplotlib but not pyplot. This solved it for me:

import matplotlib as plt
from matplotlib import pyplot as pllt
dumbchild
  • 275
  • 4
  • 11
2

I had a similar issue that I resolved and here is my issue:

I set everything up on python3 but I was using python to call my file for example: I was typing "python mnist.py" ...since I have everything on python3 it was thinking I was trying to use python 2.7

The correction: "python3 mnist.py" - the 3 made all the difference

I'm by no means an expert in python or pip, but there is definitely a difference between pip and pip3 (pip is tied to python 2.7) (pip3 is tied to python 3.6)

so when installing for 2.7 do: pip install when installing for 3.6 do: pip3 install

and when running your code for 2.7 do: python when running your code for 3.6 do: python3

I hope this helps someone!

Dinob661
  • 21
  • 1
2

For me, with PyCharm and Python 3.8 on WIN10 environment, the solution was inside the IDE of PyCharm itself.

From the IDE and Project Setting (the gear on the top right corner), I've chosen Settings -->Project:Python-->Python Interpreter and you can see all the packages installed (and matplotlib wasn't present in the Package list).

Click on the + icon in the upper left corner for Package installation, and next will be opening a new window with the available packages for PyCharm. In the find box was sufficient to type matplotlib, select it and click on Install Package.

Here a screen-shot of PyCharm environment (as requested on 21/08/2021) : matplotlib inside PyCharm environment

2

I solved by conda. Once installed miniconda or anaconda, type

conda install matplotlib

then, when prompted, type

y
Lucas
  • 458
  • 4
  • 6
1

I bashed my head on this for hours until I thought about checking my .bash_profile. I didn't have a path listed for python3 so I added the following code:

# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH

And then re-installed matplotlib with sudo pip3 install matplotlib. All is working beautifully now.

Alex M
  • 2,756
  • 7
  • 29
  • 35
David
  • 31
  • 4
1

The file permissions on my virtual environment directory and my project directory were not correct and, thus, would not allow me to install the proper packages. I upadated them by running:

sudo chown user:user -R [project folder]
sudo chown user:user -R [environment folder]

In the above your should use your own usernames in place of "user". The -R recurses through all subfolders and files.

Scott
  • 526
  • 6
  • 10
1

Working on the django project and faced same problem, This is what I did. Check if you have matplotlib already simply by writing pip show matplotlib in the python terminal.

  1. If dont, run the command pip install matplotlib.(make sure you have pip downloaded)
  2. Run the command pip show matplotlib again.
  3. If the library is successfully downloaded, you can run your project (py manage.py runserver)
Kalana Mihiranga
  • 488
  • 7
  • 18
1

If you are in a venv, don't install these libs within the venv. Installing them from outside helped me.

ADJ
  • 1,182
  • 2
  • 14
  • 26
0

If you experienced this error ModuleNotFoundError: No module named 'matplotlib' while using venv and running pytest like me, you can check by comparing the path type pytest shows and the path type python shows. In my case pytest wasn't under venv/bin directory like python, I installed pytest by pip and reactivated venv. See https://stackoverflow.com/a/54597424/3622415

kangkyu
  • 5,252
  • 3
  • 34
  • 36
0

I have installed matplotlib using both pip and conda but experienced this error ModuleNotFoundError: No module named 'matplotlib' .

I fix by following.

Because It might have an old version of Jupyter notebook, so i try this command to install it in the current kernel.

import sys
!{sys.executable} -m pip install seaborn

In the New Jupyter version (2019) can be installed simply as:

%pip install matplotlib
cng.buff
  • 405
  • 4
  • 5
0

in sublime text you can set

"cmd": ["python3", "-u", "$file"]

in tools > buildsystem > new build system

it worked for me

morteza mortezaie
  • 1,002
  • 14
  • 37