Python

No Module Named Matplotlib

This guide will discuss the “No module named matplotlib” error in Python: why it occurs, and how to resolve it in different environments.

Error Causes

The “ModuleNotFoundError: No module named ‘matplotlib'” error occurs when you attempt to import the matplotlib package in an environment that does not have matplotlib installed.

Matplotlib, although a popular and versatile package, is not included in the default Python installation.

Hence, you need to install it in your system before using it.

Installing Matplotlib In Windows

On Windows, you can install matplotlib by calling pip as shown in the command:

$ pip install matplotlib

In some cases, pip may not be available globally in your system. In such a case, you can call it directly as a Python module as shown:

$ python -m pip install matplotlib

Installing Matplotlib In Linux

On Linux systems, you can use pip or pip3 (depending on your python version) to install matplotlib.

The commands are as shown:

$ sudo pip install matplotlib
$ sudo pip3 install matplotlib

Another alternative for installing matplotlib in Linux is easy_install. For example, you can run the command:

$ sudo easy_install -U matplotlib

If you would prefer to use your package manager, run:

$ sudo apt-get install python3-matplotlib -y
$ sudo apt-get install python-matplotlib
$ sudo yum install python-matplotlib -y

Installing Matplotlib in OSX

On macOS, you can use pip to install matplotlib as shown:

$ sudo pip3 install matplotlib

Anaconda/Miniconda Install Matplotlib

Suppose you are using Anaconda or Miniconda as your Python environment. Then, you can install matplotlib with the command:

$ conda install matplotlib

Check Matplotlib Version

You can check the installed matplotlib version by running the command:

$ pip3 show matplotlib

Output:

Terminating

Through this guide, you discovered several methods of fixing the “ModuleNotFoundError: No module named ‘matplotlib'” error when importing matplotlib in your Python environment.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list