Solarian Programmer

My programming ramblings

Install Python 3 with NumPy, SciPy and Matplotlib on macOS Mojave

Posted on October 4, 2016 by Paul

Updated 17 February 2019

In this article, I will show you how to install Python 3 with NumPy, SciPy and Matplotlib on macOS Mojave.

MacOS comes by default with Python 2.7 which, at this point, receives only bug fixes and will be EOL by 2020. Python 3.x is the future and it is supported by all major Python libraries. In this tutorial, we’ll use the latest stable Python 3 release, Python 3.7.2.

There is more than one way to install Python 3 on macOS. In the previous version of this article I used to recommend Miniconda, unfortunately this approach doesn’t seem to work anymore, at least for installing Matplotlib. At the time of this update, 2019, what I recommend is to install Python 3 with Homebrew and using pip from the above to install the libraries.

Start by installing the Command Line Tools for macOS. Please note, that you will need the Command Line Tools even if you’ve already installed Xcode. Open a Terminal and write:

1 xcode-select --install

Once the Command Line Tools are installed, we can install Homebrew:

1 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Next, we’ll use the brew command to install the latest stable version of Python:

1 brew install python

After the above, you can invoke Python3 using the python3 command. This is what I see if I run python3 on my machine:

1 ~ $ python3
2 Python 3.7.2 (default, Feb 12 2019, 08:15:36)
3 [Clang 10.0.0 (clang-1000.11.45.5)] on darwin
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>> quit()
6 ~ $

Next, let’s follow best practices and create a new Python environment in which we can install NumPy, SciPy and Matplotlib:

1 python3 -m venv work
2 source work/bin/activate

At this point, your prompt should indicate that you are using the work environment. You can read more about Python environment in the documentation.

Once an environment is activated, all the install commands will apply only to the current environment. By default, if you close your Terminal, the environment is deactivated. If you want to be able to use it, use the source work/bin/activate command.

We can install NumPy, SciPy and Matplotlib with:

1 pip install numpy
2 pip install scipy
3 pip install matplotlib

As a side note, when you are in a new environment you can use python to invoke the Python interpreter, no need to use the version number.

Fire up Python, import scipy and print the version of the installed library. This is what I see on my machine:

1 (work) ~ $ python
2 Python 3.7.2 (default, Feb 12 2019, 08:15:36)
3 [Clang 10.0.0 (clang-1000.11.45.5)] on darwin
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>> import scipy as sp
6 >>> sp.version.version
7 '1.2.1'
8 >>> quit()
9 (work) ~ $

Let’s try something a bit more interesting now, let’s plot a simple function with Matplotlib. First, we’ll import SciPy and Matplotlib with:

1 import scipy as sp
2 import matplotlib.pylab as plt

Next, we can define some points on the (0, 1) interval with:

1 t = sp.linspace(0, 1, 100)

Now, let’s plot a parabola defined by the above interval:

1 plt.plot(t, t**2)
2 plt.show()

You should see something like this:

Matplotlib parabola

As you’ve probably noticed, plt.show() is a blocking command. You won’t be able to use the interpreter until you close Figure 1.

There is also an interactive mode in which you can plot functions. Close Figure 1 and write:

1 plt.ion()
2 plt.plot(t, t**2)
3 plt.title("Interactive plot test")
4 plt.plot(t, t**3)
5 plt.xlabel("t")

This is what you should see:

Matplotlib interactive mode draw two curves, add title and axis labels

At any point you can disable the interactive plot mode with:

1 plt.ioff()

after which you will need to use the plt.show() function in order to actually see the result of the plt.plot function.

If you want to learn more about Python and Matplotlib, I recommend reading Python Crash Course by Eric Matthes. The book is intended for beginners, but has a nice Data Visualization intro to Matplotlib chapter:

Another good Python book, for more advanced users, which also uses Matplotlib for some of the book projects is Python Playground by Mahesh Venkitachalam:


Show Comments