Matplotlib Introduction
Matplotlib is a feature-rich module for producing a wide array of graphs, plots, charts, images, and animations.
Since Matplotlib is not part of the Python core libraries (like the math
and csv
libraries we’ve seen so far), we need to install Matplotlib before we can use it.
The installation process for third-party Python modules is unlike installing an app on your phone or desktop. We’ll present two ways—one using Thonny (which is easier) and another, which is the more common approach, but which has more steps, and isn’t quite as friendly (but it’s not too bad). You’ll need to use the latter approach if you’re using IDLE.
Here are some examples of plots made with Matplotlib (from the Matplotlib gallery at matplotlib.org):
Installation with Thonny
One of the nice things about Thonny is that it comes bundled with its own version of Python and its own virtual environment. With Thonny, you can install Matplotlib (and thousands of other modules) using Thonny’s interface (with menus, buttons, etc.).
Installation without Thonny
Without Thonny, there’s a little more work involved. First, we create a virtual environment and then we install Matplotlib (and other modules) to that virtual environment.
A virtual environment is used to isolate a separate installation of Python on your computer. Once created, we can install various modules—including Matplotlib—in this isolated environment, so as to avoid disrupting the system-installed Python. While this approach does require extra steps, it is preferred over installing modules to your system without the virtual environment.
A virtual environment isn’t anything magical. It’s just a directory (folder) which contains (among other things) its own version of Python, installed modules, and some utility scripts.
In order to use a virtual environment it must be activated. Once activated, any installations of modules will install the modules into the virtual environment. Python, when running in this virtual environment will have access to all installed modules. When you’re done using a virtual environment you can deactivate it.
Installation of a virtual environment involves running Python’s venv
module from the command line, e.g.,
-m venv ~/path/to/your/virtual/environment python
where the path (as shown above) depends on where you’d like to keep your virtual environment(s).
Installation of Python modules is done with pip
. pip
is an acronym for package installer for Python. When you ask pip
to install a module, it will fetch the necessary files from PyPI.org—the public repository of Python packages—then build and install to whatever environment is active.
Instructions for creating a virtual environment will vary by platform (e.g., macOS, Windows, Linux). Here we will present recipes for macOS and Window.
To install, follow these instructions:
Note: Images from Matplotlib.org are excluded from the license for this material. They are subject to Matplotlib’s license at https://matplotlib.org/stable/users/project/license.html
Copyright © 2023–2025 Clayton Cafiero
No generative AI was used in producing this material. This was written the old-fashioned way.