How to Fix: No module named matplotlib


One common error you may encounter when using Python is:

no module named 'matplotlib'

This error occurs when Python does not detect the matplotlib library in your current environment.

This tutorial shares the exact steps you can use to troubleshoot this error.

Step 1: pip install matplotlib

Since matplotlib doesn’t come installed automatically with Python, you’ll need to install it yourself. The easiest way to do so is by using pip, which is a package manager for Python.

You can run the following pip command to install matplotlib:

pip install matplotlib

In most cases, this will fix the error.

Step 2: Install pip

If you’re still getting an error, you may need to install pip. Use these steps to do so.

You can also use these steps to upgrade pip to the latest version to ensure that it works.

You can then run the same pip command as earlier to install matplotlib:

pip install matplotlib

At this point, the error should be resolved.

Step 3: Check matplotlib and pip Versions

If you’re still running into errors, you may be using a different version of matplotlib and pip.

You can use the following commands to check if your matplotlib and pip versions match:

which python
python --version
which pip

If the two versions don’t match, you need to either install an older version of matplotlib or upgrade your Python version.

Step 4: Check matplotlib Version

Once you’ve successfully installed matplotlib, you can use the following command to display the matplotlib version in your environment:

pip show matplotlib

Name: matplotlib
Version: 3.1.3
Summary: Python plotting package
Home-page: https://matplotlib.org
Author: John D. Hunter, Michael Droettboom
Author-email: matplotlib-users@python.org
License: PSF
Location: /srv/conda/envs/notebook/lib/python3.7/site-packages
Requires: cycler, numpy, kiwisolver, python-dateutil, pyparsing
Required-by: seaborn, scikit-image
Note: you may need to restart the kernel to use updated packages.

Note: The easiest way to avoid errors with matplotlib and Python versions is to simply install Anaconda, which is a toolkit that comes pre-installed with Python and matplotlib and is free to use.

Additional Resources

The following tutorials explain how to fix other common problems in Python:

How to Fix: No module named numpy
How to Fix: No module named plotly
How to Fix: NameError name ‘pd’ is not defined
How to Fix: NameError name ‘np’ is not defined

Leave a Reply

Your email address will not be published.