“ModuleNotFoundError: No Module Named ‘Matplotlib'” In Python

ModuleNotFoundError: No module named 'matplotlib' in Python

In this article, we will fix the “ModuleNotFoundError: No module named ‘matplotlib'” in Python and explain how to avoid this error in the future. Continue reading to know more. 

First of all, what is Matplotlib?

Matplotlib helps us visualize our data. It is a useful graphing library for people working with Python and NumPy.

When does the “ModuleNotFoundError: No module named ‘matplotlib'” in Python appear? And how to solve it?

Matplotlib library not installed

If we have not installed the Matplotlib library before importing, we will face the ModuleNotFoundError error. See the example below to understand this cause.

# Importing matplotlib but not installed
import matplotlib.pyplot as plt
import numpy as np

# Create coordinate points
x = [1, 2, 3, 4]
y = [5, 6, 7, 8]

# Create a simple chart
plt.plot(x, y, color="red")
plt.show()

Error:

ModuleNotFoundError: No module named 'matplotlib'

In this case, we need to install Matplotlib with a command like this:

pip install matplotlib

If the following message appears:

Successfully installed matplotlib-3.6.1

That means you have fixed the error. Now, import it into the app and use it.

import matplotlib.pyplot as plt
import numpy as np

# Create coordinate points
x = [1, 2, 3, 4]
y = [5, 6, 7, 8]

# Create a simple chart
plt.plot(x, y, color="red")
plt.show()

Output:

The library name is incorrect when imported

The library name and the name used to import will not be the same, so when we use the Matplotlib library name to import, it will lead to an error like the one below:

# Importing the wrong name
import Matplotlib.pyplot as plt

Error:

ModuleNotFoundError: No module named 'Matplotlib'

This error is considered a common typo when we work with Python. To avoid this error, you must carefully check that the name of the library you want to import has been written in lowercase. Like this:

# Imported the correct name
import matplotlib.pyplot as plt
import numpy as np

# Create coordinate points
x = [1, 2, 3, 4]
y = [5, 6, 7, 8]

# Create a simple chart
plt.plot(x, y, color="green")
plt.show()

Output:

Install manually using the installation file

If you have tried the above 2 ways but still get the ModuleNotFoundError error, do the following:

This time, we will show you how to install Matplotlib manually.

You will first have to install the package here.

After downloading, extract the file. The result will look like the image below.

Go to the extracted folder, open a terminal, and type:

python setup.py install

If an error occurs:

Error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools"

Download Microsoft C++ Build Tools here and install the appropriate version of Microsoft Visual C++ as below.

Below is the message line after successful installation:

Now you can rerun the command:

python setup.py install

To check if the installation is successful, type:

pip show matplotlib

If the information on matplotlib appears like the one below, you have successfully installed it.

Name: matplotlib
Version: 3.6.1

Now, all you need to do is import and use.

import matplotlib.pyplot as plt
import numpy as np

# Create coordinate points
x = [1, 2, 3, 4]
y = [5, 6, 7, 8]

# Create a simple chart
plt.plot(x, y, color="blue")
plt.show()

Output:

Summary

Above are some causes and solutions to understand better the ModuleNotFoundError: No module named ‘matplotlib’ in Python. We hope with the knowledge in this article. You can avoid similar errors in the future.

Happy coding!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *