How to Install Matplotlib in Python

Matplotlib is a Python library used for creating charts, graphs, and visualizations, including line graphs, bar charts, scatter plots, and histograms. A missing or broken Matplotlib install usually comes down to the wrong pip, an inactive environment, or a PATH issue rather than the library itself. This article covers every method to install Matplotlib, pip, conda, and virtual environments, with separate steps for Windows, macOS, and Linux, plus fixes for the errors that show up most often during installation.

install matplotlib in python

Before You Install

A few quick checks prevent most installation problems before they start.

  • Python version: Run python --version or python3 --version in the terminal. Matplotlib requires Python 3.9 or higher.
  • pip availability: Run pip --version. A missing pip installs with python -m ensurepip --upgrade.
  • Active environment: A virtual environment or Conda environment needs to be activated before installing, otherwise Matplotlib installs into the wrong Python.

Method 1: Install Matplotlib Using pip

The pip command installs Matplotlib directly from the Python Package Index.

Run the following command in a terminal or command prompt.

pip install matplotlib

Systems with Python 3 mapped to pip3 instead of pip use this command.

pip3 install matplotlib

Systems with multiple Python versions installed benefit from calling pip through the interpreter directly, since this installs into the exact Python environment in use.

python -m pip install matplotlib
python3 -m pip install matplotlib

Method 2: Install Matplotlib on Windows

Windows 10 and 11 use the same install process through Command Prompt.

  1. Open Command Prompt by searching for cmd in the Start menu.
  2. Check the installed Python version with python --version.
  3. Upgrade pip with python.exe -m pip install --upgrade pip.
  4. Install Matplotlib with python -m pip install matplotlib.
  5. Verify the install with python -c "import matplotlib; print(matplotlib.__version__)".

A python is not recognized error means Python was not added to PATH during installation. Reinstalling Python and checking the Add Python to PATH box on the first installer screen fixes this.

Method 3: Install Matplotlib on macOS

macOS ships with a system Python that most projects avoid using directly, since a separate Python from python.org or Homebrew keeps project dependencies isolated from the OS.

python3 -m pip install matplotlib

Verify the install and confirm which Python installation it uses.

python3 -c "import matplotlib; print(matplotlib.__version__, matplotlib.__file__)"

Running which python3 and seeing /usr/bin/python3 in the output confirms the system Python is active, which works for quick installs but a virtual environment remains the better option for real projects.

Method 4: Install Matplotlib on Linux

Linux systems install Matplotlib through pip or the system package manager.

Pip installs the latest version and works the same way across distributions.

python3 -m pip install matplotlib

The system package manager installs an OS-tested version that sometimes lags behind the latest Matplotlib release.

  • Debian / Ubuntu: sudo apt-get install python3-matplotlib
  • Fedora: sudo dnf install python3-matplotlib
  • Red Hat: sudo yum install python3-matplotlib
  • Arch Linux: sudo pacman -S python-matplotlib

Fix the Externally Managed Environment Error on Linux

Newer Ubuntu and Debian releases block system-wide pip installs to protect OS packages, which triggers this error during a direct install attempt.

Adding the --break-system-packages flag bypasses the restriction for a single package.

pip install matplotlib --break-system-packages

A virtual environment remains the safer long-term fix, since it avoids touching system packages entirely.

Method 5: Install Matplotlib in a Virtual Environment

A virtual environment keeps Matplotlib and its dependencies isolated from the system-wide Python installation, which real projects benefit from.

Create and activate the environment first.

python3 -m venv venv

Linux and macOS activate the environment with this command.

source venv/bin/activate

Windows activates the environment with this command instead.

venv\Scripts\activate

Run the standard install command once the environment is active.

pip install matplotlib

Method 6: Install Matplotlib with Conda

Anaconda and Miniconda users install Matplotlib through conda so dependencies stay managed together.

conda install matplotlib

The community-maintained conda-forge channel usually carries a more up-to-date release.

conda install -c conda-forge matplotlib

Creating a separate environment before installing avoids conflicts with the base environment.

conda create -n my-env
conda activate my-env
conda install matplotlib

Verify the Matplotlib Installation

A version check confirms Matplotlib installed correctly and is importable.

python -c "import matplotlib; print(matplotlib.__version__)"

A version number in the output, such as 3.10.1, confirms a working install. A ModuleNotFoundError means the install did not complete in the same environment used for verification, so the pip or conda command needs to be rerun there.

Run a First Plot

A short script confirms Matplotlib renders a chart correctly after installation.

Run a First Plot

A short script confirms Matplotlib renders a chart correctly after installation.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 100)
plt.plot(x, np.sin(x), label="sin(x)")
plt.plot(x, np.cos(x), label="cos(x)")
plt.legend()
plt.title("Trigonometric Functions")
plt.show()

A window with two curves confirms Matplotlib is working. Jupyter Notebook users often see the plot render without needing plt.show().

Upgrade or Uninstall Matplotlib

Matplotlib updates, downgrades, and removals all run through pip.

Upgrade to the latest version.

pip install --upgrade matplotlib

Check the currently installed version and install location before upgrading.

pip show matplotlib

Install a specific version for compatibility with an older project.

pip install matplotlib==3.8.0

Uninstall Matplotlib completely.

pip uninstall matplotlib

Common Matplotlib Installation Errors

Most Matplotlib install failures trace back to environment mismatches, PATH issues, or outdated tools.

ErrorCauseFix
ModuleNotFoundError: No module named 'matplotlib'Package not installed in the active environmentRun pip install matplotlib with the environment activated
Installed but still shows ModuleNotFoundErrorpip installed into a different Python than the one running the scriptUse python -m pip install matplotlib
pip: command not foundpip missing from PATHUse python -m pip install matplotlib or pip3
PermissionError: [Errno 13] Permission deniedNo write access to the system package directoryUse a virtual environment or add --user
SSL: CERTIFICATE_VERIFY_FAILEDCorporate proxy blocking certificate verificationRun pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org matplotlib
ERROR: Failed building wheelOutdated pip or missing wheel packageRun python -m pip install --upgrade pip wheel setuptools, then retry
Version conflict with NumPyIncompatible package versions installed togetherRun pip install matplotlib --upgrade or use a fresh virtual environment

Matplotlib installs through a single pip or conda command on most systems, with virtual environments, Windows PATH fixes, and the --break-system-packages flag covering the common edge cases across platforms. Running the version check and a first plot after installation confirms Matplotlib is ready to use in a Python script.

Frequently Asked Questions

How do I know if Matplotlib is already installed?

Running python -c "import matplotlib; print(matplotlib.__version__)" in the terminal prints a version number when Matplotlib is installed and raises a ModuleNotFoundError when it is not.

Can I install Matplotlib without pip?

Linux systems install Matplotlib through the system package manager, such as apt-get, dnf, or pacman. Anaconda and Miniconda install it through conda install matplotlib.

How do I install a specific version of Matplotlib?

Running pip install matplotlib==X.X.X with the exact version number installs that release, for example pip install matplotlib==3.7.2.

Why does my plot not show up?

A missing plt.show() at the end of the script keeps the plot window from appearing outside Jupyter Notebook. A persistent blank window points to a backend issue, which matplotlib.use('TkAgg') before the pyplot import usually resolves.

Does Matplotlib work with the latest Python versions?

Matplotlib supports Python 3.9 through 3.13, so installing the latest stable Python 3.x release keeps compatibility intact.

Read More

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply