How to Install Matplotlib in Python?

Learn via video courses
Topics Covered

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

  1. Check if pip3 and python3 are installed correctly by running the following command:
  1. Install matplotlib by using the pip command:

Installing matplotlib

Installing Matplotlib in Windows

Steps for installing Matplotlib on Windows:

  1. Download python from the official website according to your operating system and install it.
  2. Next, install Matplotlib using the following PIP command in the command prompt terminal:

Installing matplotlib in Windows The installation will start automatically, and Matplotlib will be successfully installed with its latest version.

pip install matplotlib for installation 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:

  1. Install the complete package by downloading python from python.org, then use the apt install command for installing on the ubuntu system.
  2. Another way is to directly install pip first on Ubuntu and then install matplotlib by performing the following steps:

Type command

installing pip on ubuntu

  1. Type the command

for installing matplotlib in python3, use

For installing matplotlib in python2. using pip install matplotib 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.

advanced use of matplotlib

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

  1. Creating a virtual environment:
python -m venv <directory_path>
  1. Activate the environment created:

install matplotlib python venv in windows

Activating environment to install

  1. Retrieve the latest version of matplotlib from the git source, and install matplotlib in editable mode.

How to Install Matplotlib Python venv in Ubuntu?

  1. Creating a virtual environment:
  1. Activate the environment created:
  1. 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:

  1. We set up the python environment by typing python3 in the terminal.
  2. 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

Verify Matplotlib Installation

Verifying Installation of Matplotlib on Windows

Verifying matplotlib installation can be done in two different ways.

  1. By using the

In the command prompt. Verifying Installation of matplotlib on Windows

  1. Another verification method is setting up a python environment, importing matplotlib, and then checking its version.

Verification of installation

Verifying Installation of Matplotlib on Ubuntu

Verifying the Installation of matplotlib on Ubuntu can be done using the command:

  1. We set the python environment by typing python3 in the terminal.
  2. Successful import of matplotlib verifies that matplotlib is installed on the system. The command for importing matplotlib is import matplotlib.
  3. Checking the version of the matplotlib using the command matplotlib.__version__

Set of commands to verify installation

Verifying Installation of matplotlib on Ubuntu

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 getpip.pyget-pip.py from the internet and save it to your local system.

Run the getpip.pyget-pip.py 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

Learn More: