Modulenotfounderror: no module named ‘matplotlib’ ( Solved )

Matplotlib is a python package for plotting charts. Most developers use it for plotting figures like line charts, histograms, bar charts, scatterplots, etc quickly. But you may get error sometime while using it like Modulenotfounderror: no module named ‘matplotlib’ error. In this entire tutorial, you will learn how to solve this error.

What is Modulenotfounderror?

Many developers get this error when they have not installed the specific package or they have not set the path for that package. For example, if you are using the python package “P” and you are getting the error modulenotfounderror: no module named ‘P’ then it means module P is not installed in your system. To remove this error you have to install the P module.

Why modulenotfounderror: no module named ‘matplotlib’ occurs?

If you import or use the module, you may see the “ModuleNotFoundError: No module named ‘matplotlib'” error. This error is raised when the matplotlib module is not found in your system. And it can happen for a number of reasons.

You will get the error when you will run the below code.

import matplotlib.pyplot as plt

Output

Modulenotfounderror no module named matplotlib error
Modulenotfounderror no module named matplotlib error

Solve modulenotfounderror: no module named matplotlib Error

Solution 1: Install matplotlib using pip

The most common reason for this error is that the matplotlib module is not installed. This can be easily fixed by installing the module using pip. But you have to also check the version of python. If the version of the python is 2. xx then use pip and if it is python 3. xx then use pip3.

Python 2. xx

pip install matplotlib

Python 3. xx

pip3 install matplotlib

Solution 2: Install module using conda

If you’re using conda, you can install matplotlib with the following command:

conda install matplotlib

Solution3: Add module to the system path

Once you have matplotlib installed in your system, you should be able to import it into your Python code without any problems. If you’re still seeing the “ModuleNotFoundError: No module named ‘matplotlib‘” error, it’s possible that your matplotlib installation is not in your Python path. Run the below command to check the python path.

python -c "import sys; print(sys.path)"

Output

Check the path of python
Check the path of python

I am seeing the python path for my system. If you don’t see the directory where your matplotlib installation is located in the output of this command, you’ll need to add it to your Python path. Open your .bashrc or .bash_profile file for editing and add the following line if you are using Linux.

You can do the same in your terminal or command prompt.

export PYTHONPATH=/path/to/matplotlib/installation:$PYTHONPATH

After restarting the terminal you will not get the error while importing the module.

Conclusion

Matplotlib is a powerful Python package for plotting charts. However, you may occasionally get errors when using it, like the “Modulenotfounderror: no module named ‘matplotlib'” error.  You can use the above methods to solve this error easily.

We hope you may have liked this tutorial. If you have any queries then contact us for more help.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link