PyPlot on macOS Monterey with M1

While trying out Fatou.jl, I had difficulty getting PyPlot to work on an M1 Mac. Here is the environement and steps that got things working.

Python

I am not using Conda. I did follow directions here to set things up on the M1 with pyenv, followed by brew install python (non-Rosetta), pipx, and Poetry.

My .zshrc
export PATH="/opt/homebrew/bin:/Users/foo/Library/Python/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:$PATH"
export SDKROOT="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk"

export PATH="$HOME/.pyenv/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

alias python="python3"

export PATH="~/.local/bin:$PATH"

export PIP_REQUIRE_VIRTUALENV=true
# escape hatches to override above stipulation;
# had to use gpip3 below to install numpy and matplotlib
gpip(){
   PIP_REQUIRE_VIRTUALENV="" pip "$@"
}

gpip3(){
   PIP_REQUIRE_VIRTUALENV="" pip3 "$@"
}

export MPLBACKEND=qt5agg ## this is crucial for Julia PyPlot package build
gpip3 install numpy  ## see .zshrc
gpip3 install matplotlib

brew install pyqt@5
## please see also .zshrc MPLBACKEND variable

Julia

In the project, directory…

julia > ENV["PYTHON"]="python3"

] add PyCall, PyPlot
] build PyCall, PyPlot

After this, all was well with PyPlot on the M1.

2 Likes