11

I am getting this error when I am trying to import "matplotlib.pyplot". I cant even install matplotlib.pyplot through conda install.

It shows this:

import matplotlib.pyplot Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'matplotlib.pyplot'

Bhoomi Patel
  • 777
  • 10
  • 32
rita123
  • 121
  • 1
  • 1
  • 4

7 Answers7

5

As reported here, when you use Anaconda, install the packet using conda. In this case, the right instruction to use (on Ubuntu 18.04) is:

conda install -c conda-forge matplotlib

This will solve the problem.

If you use pip (you can), you will mess up all the dependencies (and for instance, the probability that other scripts/programs do not work anymore is not null: if you use Spyder, you will have big dependencies problem to face).

Optional:

In order to always avoid problem like this, I recommend you to use Virtual Enviroment:

Whats is it?

Geeksforgeeks explains it clearly.

How?

A step-by-step guide is always useful.

Leos313
  • 5,152
  • 6
  • 40
  • 69
4

It could be that it's running your default Python installation instead of the one installed with Anaconda. Try prepending this to the top of your script:

#!/usr/bin/env python

If that does not work, try installing matplotlib with pip, then try again:

pip install matplotlib

Let me know if that works for you.

Robert Valencia
  • 1,752
  • 4
  • 20
  • 36
4

I had the same issue for days, just solved it by adding "%matplotlib inline" on top of "import matplotlib.pyplot as plt"

So enter this to import mathplotlib.pylot:

%matplotlib inline
import matplotlib.pyplot as plt
Chikage
  • 41
  • 3
2

Just open anaconda prompt and use either of the below command to install the package. This solved my issue.

  1. conda install -c plotly chart-studio

or

  1. conda install -c plotly/label/test chart-studio
akash s
  • 59
  • 3
1

Make sure Matplotlib is accessed in the Same Conda Environment as it was installed.
In the case below, Matplotlib was installed in pytorch environment and not the base environment.
Hence when run in the pytorch environment it gets imported, but doesn't get imported in base environment.

TERMINAL

#Installation  
(pytorch) F:\Script\Ai\Pytorch>conda install -c conda-forge matplotlib 

#Check installation  in pytorch Environment  
(pytorch) F:\Script\Ai\Pytorch>python  
>>> import matplotlib
>>> print('matplotlib: {}'.format(matplotlib.__version__))
matplotlib: 3.3.4  


#Import Error in base Environment
(base) F:\Script\Ai\Pytorch>python
>>> import matplotlib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'matplotlib'
srt111
  • 1,079
  • 11
  • 20
1

Switch to the correct environment before installing.

conda activate environment_name_here
conda install matplotlib

In my case, I had an environment for a jupyter notebook, but installed it initially in a new terminal window which defaulted to the base environment.

Moo
  • 3,369
  • 4
  • 22
  • 41
1

Check if the ...../python3.x/site-packages is listed within sys.path. If not append it with sys.path.append('.....python3.8/site-packages')

Rajarshi Ghosh
  • 452
  • 1
  • 9