Matplotlib Installation
Learn how to install Matplotlib in Python, understand installation requirements, and verify that the library is ready to create data visualizations.
1. Learning Objectives
By the end of this lesson, you will be able to:
- Understand the requirements for installing Matplotlib.
- Install Python packages using pip.
- Install Matplotlib using the command prompt or terminal.
- Install Matplotlib using Jupyter Notebook.
- Verify the Matplotlib installation.
- Check the installed Matplotlib version.
- Understand common installation errors and their solutions.
2. Introduction to Matplotlib Installation
Before creating charts and graphs, we need to install Matplotlib in our Python environment. Matplotlib is a third-party Python library, so it must be installed before we can import and use it in our programs.
The easiest way to install Matplotlib is by using Python's package installer, called pip.
What Is pip?
pip is the standard package installer used to install and manage Python packages from the Python Package Index (PyPI).
Using pip, we can install Matplotlib and other Python libraries with simple terminal commands.
3. System Requirements
Before installing Matplotlib, check that your system has the required Python environment and package management tools.
| Requirement | Description |
|---|---|
| Python | A supported Python version installed on your system. |
| pip | Python package installer used to install Matplotlib. |
| Internet Connection | Required to download the package from PyPI. |
| Code Editor | VS Code, PyCharm, Jupyter Notebook, or another Python IDE. |
| Terminal | Command Prompt, PowerShell, or another command-line terminal. |
4. Check Python Installation
First, open Command Prompt or PowerShell on Windows. On macOS or Linux, open the terminal.
Run the following command:
python --versionIf Python is installed correctly, the terminal displays the installed Python version.
Example Output
Python 3.12.5The version number displayed on your computer may be different. The above output is only an example.
Check pip Version
Now execute:
python -m pip --versionThis command checks whether pip is available for the Python interpreter being used.
python -m pip helps ensure that pip
runs through the selected Python interpreter.
5. Install Matplotlib Using pip
The recommended simple method is to install Matplotlib using pip from your terminal.
Step 1: Open Terminal
Open Command Prompt, PowerShell, or the terminal available in your code editor.
Step 2: Execute Installation Command
python -m pip install matplotlibStep 3: Wait for Installation
pip downloads Matplotlib and any required dependencies, then installs them into the selected Python environment.
Step 4: Upgrade pip if Required
If pip is outdated, you can upgrade it using:
python -m pip install --upgrade pipAfter upgrading pip, execute the Matplotlib installation command again if necessary.
6. Install Matplotlib Using Jupyter Notebook
If you are working with Jupyter Notebook, you can install Matplotlib directly from a notebook cell.
Installation Command
%pip install matplotlib
The %pip command installs the package
in the Python environment associated with the current
Jupyter kernel.
Installation Steps
- Open Jupyter Notebook.
- Create a new Python notebook.
- Enter
%pip install matplotlibin a cell. - Run the cell.
- Wait until the installation finishes.
- Import Matplotlib in another cell to verify it.
7. Verify Matplotlib Installation
After installation, we should verify that Matplotlib can be imported successfully.
Step 1: Import Matplotlib
import matplotlibStep 2: Check the Installed Version
import matplotlib
print(matplotlib.__version__)Example Output
3.x.xThe exact version depends on the package installed in your Python environment.
Step 3: Test pyplot
import matplotlib.pyplot as plt
print("Matplotlib is installed successfully!")8. Install Matplotlib Using Conda
Another method is to use Conda, which is a package and environment manager commonly used in data science.
If Anaconda or Miniconda is installed, open Anaconda Prompt or a terminal where Conda is available.
Conda Installation Command
conda install matplotlibUsing Conda-Forge
conda install -c conda-forge matplotlibConda installs Matplotlib and resolves compatible dependencies for the selected Conda environment.
| Method | Command | Environment |
|---|---|---|
| pip | python -m pip install matplotlib |
Python environment |
| Jupyter | %pip install matplotlib |
Notebook kernel environment |
| Conda | conda install matplotlib |
Conda environment |
9. Common Installation Errors and Solutions
Sometimes installation may fail because of environment, package, or configuration issues.
| Error | Possible Reason | Solution |
|---|---|---|
| Python not recognized | Python is not installed or its PATH is not configured. | Install Python or configure the PATH variable. |
| No module named matplotlib | Matplotlib is not installed in the active environment. | Install Matplotlib using the active Python interpreter. |
| pip not recognized | pip is not available through the command. | Try python -m pip. |
| Permission denied | The current user cannot write to the selected installation location. | Use a virtual environment or an appropriate user installation. |
| Network error | Internet connection or package index access problem. | Check the network and try again. |
10. Classroom Practical Activity
Activity: Install and Verify Matplotlib
At CIIT Training Institute, students can complete this activity to prepare their Python environment.
- Open Command Prompt or PowerShell.
- Check the Python version.
- Check the pip version.
- Install Matplotlib using pip.
- Open a Python editor or IDE.
- Import
matplotlib. - Print the installed Matplotlib version.
- Import
matplotlib.pyplot. - Confirm that no import error occurs.
Practice Questions
- What is pip in Python?
- Which command is used to install Matplotlib?
- How can you check the installed Python version?
- How can you verify the Matplotlib installation?
- What is the purpose of the
__version__attribute? - How do you install Matplotlib in Jupyter Notebook?
- What is the difference between pip and Conda?
- Why should we use the correct Python environment?
Summary
In this lesson, you learned how to install Matplotlib in a Python environment. You understood the requirements for installation, checked Python and pip versions, and installed Matplotlib using the pip package installer.
You also explored installation through Jupyter Notebook and Conda. You learned how to verify the installation using import statements and the Matplotlib version attribute.
Finally, you explored common installation problems, their possible causes, and solutions. In the next lesson, you will learn how to prepare your Python environment for Matplotlib development.