How to Install Matplotlib in Python?
Overview
Matplotlib is an open-source python library that can be installed using python packages like pip and conda on different operating systems like Windows, Linux, and macOS.
- It displays numeric data in the form of plots, graphs, and charts in Python.
- It provides a wide variety of functionality for plotting.
- It is a very useful library that helps to plot different figures on the graph, like line plot, bar graphs, contour plot, histograms, scatter plots, etc.
Prerequisites
- It is mandatory to install python from the official website before installing matplotlib.
- During the installation of matplotlib, some files are automatically installed. The following list is mainly for reference:
- Python (>= 3.7)
- NumPy (>= 1.17)
- setup tools
- cycler (>= 0.10.0)
- dateutil (>= 2.7)
- kiwisolver (>= 1.0.1)
- Pillow (>= 6.2)
- pyparsing (>=2.2.1)
- fontTools (>=4.22.0)
Installing Matplotlib
Let's discuss installation of matplotlib on different operating systems, below.
Installing Matplotlib on MacOS
- Check if pip3 and python3 are installed correctly by running the following command:
- Install matplotlib by using the pip command:
Installing Matplotlib in Windows
Steps for installing Matplotlib on Windows:
- Download python from the official website according to your operating system and install it.
- Next, install Matplotlib using the following PIP command in the command prompt terminal:
The installation will start automatically, and Matplotlib will be successfully installed with its latest version.
You must have noticed that some files are automatically installed with matplotlib, like python-dateutil, packaging, pyparsing, numpy, kiwisolver, etc. These files are the mandatory dependencies of the matplotlib module.
Matplotlib Installation On Ubuntu
Python is pre-installed on Ubuntu computers. Pip, on the other hand, is not installed. There are two ways to install pip:
- Install the complete package by downloading python from python.org, then use the apt install command for installing on the ubuntu system.
- Another way is to directly install pip first on Ubuntu and then install matplotlib by performing the following steps:
Type command
- Type the command
for installing matplotlib in python3, use
For installing matplotlib in python2. Matplotlib is successfully installed on Ubuntu using the above commands.
Installing From the Source
First, we must install the dependencies (as discussed in the 'prerequisites' section). A C compiler is required for this.
- GCC on Linux
- XCode on macOS
- Visual Studio 2015 on Windows
To run the tests, you will need to install some additional dependencies:
Advanced Usage
Installing Matplotlib Using Conda
Conda is an open-source environment and package system.
- It offers prebuilt packages and can install and update.
- We do not need to install the system and python packages separately since everything is already included in the .yml(a text file that contains a list of dependencies, etc., in a single file). Anaconda must be commanded below in cmd /conda prompt/ terminal:
Above command install matplotlib from anaconda main channel. Matplotlib can also be installed from the anaconda community channel.
Matplotlib Installation Using PIP
Comparing Python2 and Python3
For Python2 Users:
Python2 users must use pip for the installation of matplotlib.
For macOS, windows, and ubuntu users:
For Python3 Users:
Python3 users must use pip3 for the installation of matplotlib.
For macOS, windows, and ubuntu users:
How to Install Matplotlib Python venv?
How to Install Matplotlib Python venv in MacOS?
How to Install Matplotlib Python venv in Windows
- Creating a virtual environment:
python -m venv <directory_path>
- Activate the environment created:
- Retrieve the latest version of matplotlib from the git source, and install matplotlib in editable mode.
How to Install Matplotlib Python venv in Ubuntu?
- Creating a virtual environment:
- Activate the environment created:
- Retrieve the latest version of matplotlib from the git source, and install matplotlib in editable mode.
Verify Matplotlib Installation
Verifying Installation of Matplotlib on MacOS
Verifying the installation of matplotlib on macOS can be done using the command:
- We set up the python environment by typing python3 in the terminal.
- Successful import of matplotlib verifies that matplotlib is installed on the system. The command for importing matplotlib is import matplotlib. Set of commands to verify installation
Verifying Installation of Matplotlib on Windows
Verifying matplotlib installation can be done in two different ways.
- By using the
In the command prompt.
- Another verification method is setting up a python environment, importing matplotlib, and then checking its version.
Verifying Installation of Matplotlib on Ubuntu
Verifying the Installation of matplotlib on Ubuntu can be done using the command:
- We set the python environment by typing python3 in the terminal.
- Successful import of matplotlib verifies that matplotlib is installed on the system. The command for importing matplotlib is import matplotlib.
- Checking the version of the matplotlib using the command matplotlib.__version__
Set of commands to verify installation
What Happens If Matplotlib is not Installed Correctly?
The Python "ModuleNotFoundError: No module named 'matplotlib'" occurs when we forget to install the matplotlib module before importing it or installing it in an incorrect environment. Check if pip is installed by simply writing pip in the python console. If you do not have pip, get a python script call from the internet and save it to your local system.
Run the script to install pip by writing the following code in cmd (command prompt) to install pip:
Now in cmd, type the following code to install matplotlib with its dependencies:
Conclusion
- Python and compilers are prerequisites for installing matplotlib.
- Matplotlib can be installed using conda and pip python packages. Pip contains python libraries. On the other hand, conda has python libraries, c libraries, and executables like c compilers, etc.
- Matplotlib can be installed from the GitHub repository of matplotlib.
- Matplotlib installation verification can be done in two ways.
- Importing matplotlib
- Checking matplotlib version