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:
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:
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 pip3 install matplotlib
Another alternative for installing matplotlib in Linux is easy_install. For example, you can run the command:
If you would prefer to use your package manager, run:
$ 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:
Anaconda/Miniconda Install Matplotlib
Suppose you are using Anaconda or Miniconda as your Python environment. Then, you can install matplotlib with the command:
Check Matplotlib Version
You can check the installed matplotlib version by running the command:
Through this guide, you discovered several methods of fixing the “ModuleNotFoundError: No module named ‘matplotlib'” error when importing matplotlib in your Python environment.