Installing Matplotlib (macOS)

Published

2023-03-22

What is Matplotlib? It’s the de facto standard for creating visualizations—charts, plots, graphs, even animations—with Python.

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Matplotlib makes easy things easy and hard things possible.

(from the Matplotlib website)

Matplotlib has great documentation, and I encourage you to dive right in. See: Getting started.

However, JIC you want a brief recipe for installing Matplotlib on a Mac, you’ve come to the right place.

If you’re using Thonny…

Note

Installing modules like Matplotlib is easier with Thonny than it is with IDLE. Right out of the box, Thonny provides a package installer interface, its own version of Python, and its own virtual environment. Many details are hidden from the user (which is often better for beginners). Installing modules with IDLE is more of a DIY (do it yourself) process, with a few extra steps. If you have difficulty with the instructions for IDLE, you may wish to try Thonny.

Thonny make installation of Matplotlib pretty easy, since Thonny provides its own version of Python and its own virtual environment.

1. Launch Thonny and open Thonny’s package manager

Open Thonny, and open the Tools menu. Choose “Manage packages…”

2. Seach for and select matplotlib

You’ll see a search form. Enter “matplotlib” and click the button labeled “Search on PyPI.” (PyPI is the official repository for installable modules for Python.)

You’ll see “matplotlib” listed among the search results. Click on this.

3. Confirming your selection and installing

You should see some information about matplotlib. Click the button labeled “Install.”

Once this is done, you should see a screen indicating that matplotlib has been installed. Click the button labeled “Close.”

4. Verify matplotlib is installed and available

In the Python shell (within Thonny) type import matplotlib at the prompt and hit return. If you arrive at another prompt without any errors displayed you should be good to go!

5. Hello Matplotlib

Try it out. Create a new Python file and enter the little bit of code shown here. Click the run button.

You should see a plot in a new window!

Congratulations! You have Matplotlib installed and working!

If you’re using IDLE…

Note

Here we present instructions which involve using a virtual environment. A virtual environment includes its own version of Python, and whatever modules you install to the environment. Virtual environments are a way of isolating dependencies for projects using Python. Creating and using a virtual environment is the preferred approach, though it requires additional steps.

You may be able to install Matplotlib to your system without a virtual environment. However, there are risks associated with altering the OS-provided installation of Python. By using a virtual environment, you can install modules (like Matplotlib), leaving your system-supplied Python unaltered.

1. Launch the macOS terminal

You may have already added the macOS terminal to the dock. If not, you can open Terminal in a number of ways.

  1. Open Terminal by finding it with Spotlight. Click on the Spotlight icon on the upper right of your menu bar, then enter “Terminal”, and click on “Terminal”.

Alternatively, you can open Terminal by opening the Finder, clicking Applications (on the left hand side vertical nav bar), then clicking “Utilities”, then clicking “Terminal”.

Tip

You can navigate your file system (if you wish) using the cd command. cd is the command for changing directory (hence the abbreviation cd).

2. Create a virtual environment

A virtual environment includes a copy of the Python interpreter, a few scripts for “activating” and “deactivating” the environment, a few utility programs (e.g., pip) and directories containing modules you’ve installed to use with Python.

Use the command below to create a virtual environment. Consider what you want to call your virtual environment. In the examples shown here, we’ll call it demo. Note that $ here indicates a command prompt in the terminal. Your prompt may differ (as it does in the screenshots) and you don’t need to type the prompt; just type what follows the prompt.

$ python -m venv ~/.venvs/demo

and press the return key. Wait for this command to complete before continuing.

If you get an error complaining that there is no python or pip

On some systems, python might be named python3. If you find yourself in that situation, just substitute python3 for python and pip3 for pip wherever they appear in the instructions.

4. Activate your virtual environment

We activate the virtual environment with the following command (assuming your virtual environment is in ~/.venvs/demo:

$ . ~/.venvs/demo/bin/activate

Notice the command starts with a period and a space. Don’t skip these!

When your virtual environment is activated, the name of the virtual environment should appear before your prompt. This indicates that the virtual environment is active. Now, when you install modules and run IDLE, the version of Python installed to your virtual environment will be used (rather than the system-installed Python).

5. Install Matplotlib

$ pip install matplotlib

This step might take a few seconds to complete.

At this point, you should have Matplotlib installed to your virtual environment.

You can verify this by running Python, and attempting to import matplotlib from within the Python shell.

(demo) $ python
>>> import matplotlib
>>>

6. Launch IDLE

Now, you can launch IDLE, so that it runs Python and has access to the matplotlib module in your virtual environment.

(demo) $ python -m idlelib

This should launch IDLE. (If it does not, you may not have TkInter properly configured for your system. If this is the case, you may wish to give Thonny a try. Configuring TkInter can get a little complicated.)

In the IDLE shell window, try importing matplotlib.

7. Using your virtual environment with IDLE

Whenever you wish to use IDLE with your virtual environment (i.e. so you can use modules you’ve installed there), follow these steps:

  • Launch Terminal (follow instructions above)

  • Activate your virtual environment

  • Verify your virtual environment is active

  • Launch IDLE

python -m idlelib

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

This material is for free use under either the GNU Free Documentation License or the Creative Commons Attribution-ShareAlike 3.0 United States License (take your pick).