How Can We Help?
Before we can start working with Matplotlib, we need to install the Matplotlib library on our local machine. In this article, we are going to understand how to install Matplotlib.
Before installing Matplotlib, check whether you have Matplotlib installed on your machine.
Run the following commands in your terminal:
python3 -c 'import matplotlib; print(matplotlib.__version__, matplotlib.__file__)'
If you get a message something like this, then you already have the Matplotlib installed in your system:
3.5.1 /usr/lib/python3/dist-packages/matplotlib/init.py
Note that the above message will differ from system to system. The only thing to note is that if you do not encounter any error message, then Matplotlib is already installed in your system. If it is not installed, you will get something like this:
Traceback (most recent call last):
File “\<string>”, line 1, in \<module>
ImportError: No module named matplotlib
Installing from the official release
Installing from the official release is the most commonly used technique to install Matplotlib. We only need to open our terminal and run either of the following commands:
python -m pip install -U pip
python -m pip install -U matplotlib
pip install matplotlib
Installing from the source
Python is an open-source language; hence its source code is available to all users over the internet. We can therefore download and install python from GitHub using the following commands:
git clone https://github.com/matplotlib/matplotlib.git
git clone git@github.com:matplotlib/matplotlib.git
Now enter into the matplotlib directory:
cd matplotlib
Next, run the following commands to install matplotlib if you are developing:
python -m pip install -e
However, if you are not developing, then use the following commands instead:
python -m pip install
To update the Matplotlibs version, run the following command:
git pull
pip install -e
Third-party installation
We can also install Matplotlib from other third-party sources like the Conda package.
We can use any of the following commands in the terminal to install matplotlib:
conda install matplotlib
conda install matplotlib
Linux package managers
If you are a Linux user, then you can use the following commands
- For the Debian or Ubuntu distribution: sudo apt-get install python3-matplotlib
- For Fedora distribution: sudo dnf install python3-matplotlib
- For Red Hat distribution: sudo yum install python3-matplotlib
- For Arch Linux distribution: sudo pacman -S python-matplotlib
Installing Python Matplotlib on OSX
We can also install the binaries of Matplotlib in the form of Wheels.
Use the following commands:
sudo port install py38-pip
The above step was first to install pip. Next, run the following commands:
python3 -m pip install matplotlib
How to check the installation:
After performing all the steps, checking whether Matplotlib is successfully installed in our system is a good practice.
Run the following commands
python3 -c 'import matplotlib; print(matplotlib.__version__, matplotlib.__file__)'
If you see something like this, your installation is successful:
3.6.1 /home/asifr/.local/lib/python3.10/site-packages/matplotlib/__init__.py