Mac Python Matplotlib
This is a short tutorial on how to install Matplotlib in macOS (10.4/Mojave).
- I'm running Python 3.8.2 on a Mac. I installed numpy, scipy, and matplotlib using pip. From the command line (in Terminal), if I type% Python3 then import matplotlib, it works. (same for scipy and numpy) But in IDLE, I get this error: ModuleNotFoundError: No module named 'matplotlib' (same for scipy and numpy).
- Install Python, NumPy, SciPy, and matplotlib on Mac OS X – Double Click March 1, 2012 jiffyclub mac os x, matplotlib, numpy, python, scipy Update: These instructions are over a year old, though they may still work for you.
Google matplotlib library drawing generate Chinese messy code problem, the most popular answer is the following lines of code. Import numpy as np import matplotlib.pyplot as plt plt.rcParams'font.sans-serif' = 'SimHei' Obviously, this is because there is no SimHei font library on the MAC, so most tutorials tell you how to download SimHei fonts and how How To Solve Mac System Python.
We will use the pip
package installer for Python.
First, we securely download get-pip.py, a bootstrapping script which enables users to install pip, setuptools and wheel in the Python environment.
Next run the below command to install pip
.
If the installation is successful, you will see the following message in the console (version may vary).
Lastly, install Matplotlib and all its dependencies
You can check the Matplolib version just installed straight from the termianal. Start the interactive shell for the Python interpreter.
You will find the interpreter in interactive mode, when the prompt consists of three greater-than signs (>>>), known as the Python REPL prompt. Run the below two commands.
Please report any issues with these instructions here.
This page was originally designed for Astronomers, but most of the instructions are generic, and apply to anyone wanting to use Python via MacPorts.
Installing MacPorts
Ensure that XCode 3 or 4 is installed (should be on the install DVDs for your Mac). If you like to live on the bleeding edge, you can purchase XCode 4 from the Mac App Store. To check if XCode is installed properly, you can type
gcc
in the command-line. If you getgcc: command not found
, then XCode is not properly installed.Download the DMG image for MacPorts from here - be sure to pick the correct one for your MacOS X version. Mount the disk image and run the installer. Note that if needed, it is possible to install MacPorts without root/admin privileges - see here for more details.
Installing Packages
- Go to the terminal and update the package index:
sudo port selfupdate
To install most of the basic Python packages, run:
sudo port install py27-matplotlib py27-numpy py27-scipy py27-ipython
Note that this will probably take several hours, and is best done overnight.
To install Astronomy packages, use for example:
sudo port install py27-pyfits py27-pywcs py27-atpy py27-aplpy
MacPorts will automatically take care of any dependencies! (by default SQL dependencies are not installed for
py27-atpy
, and the Montage dependency is not installed forpy27-aplpy
. If you want to install these, usesudo port install py27-atpy +sql
) andsudo port install py27-aplpy +montage
respectively).Note: if for any reason the installation gets interrupted (for example if you get disconnected) and you end up with an error, try running the installation command again. If this does not work, try cleaning the latest port that MacPorts was trying to install, e.g.:
sudo port clean --all -f py27-aplpy
then try running the installation again. If you are still having issues, please let me know!
Configuration
If it does not already exist, create a folder called
.matplotlib
in your home directory and copy the defaultmatplotlibrc
file to it:cp /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc ~/.matplotlib/
Edit
.matplotlib/matplotlibrc
and change the following linebackend : Agg
to
backend : MacOSX
This ensures that when you use Matplotlib interactively, a window will pop up. Optionally, you can uncomment and set the
image.origin
andimage.interpolation
lines toimage.origin : lower
image.interpolation : nearest
These settings are likely to be the most useful for plotting astronomical data.
Make this Python installation the default:
sudo port select --set python python27
sudo port select --set ipython ipython27
Note that this is optional - you can also invoke this Python installation by using
python2.7
andipython-2.7
.
Searching for packages
To check if a package is available through MacPorts, you can do:
Be sure to install the one for the correct Python version (
py27-*
if you've been using the above instructions).Before installing the package, you can check what variants are available:
Variants listed with
[+]
are installed by default. To remove a default variant, use-variant
. To include a variant not installed by default, use+variant
. For example, the following will install APLpy with support for Montage, but not for RGB images:sudo port install py27-aplpy +montage -rgb
Installing packages not in macports
When installing packages not in MacPorts, first make sure that you remove your
.pydistutils
file if you have one, then simply use:python setup.py install --user
(do not omit
--user
and do not usesudo
). This will place user installed packages in~/Library/Python/2.7/lib/python/site-packages
where they will automatically be picked up by the Python installation, without messing up the MacPorts file structure. Note that you can also install packages in a similar way usingeasy_install-2.7
andpip-2.7
by specifying the--user
option.The reason for installing packages with --user is that if instead you install packages using
sudo python setup.py install
, the packages will be installed inside the MacPorts tree, but MacPorts won't be aware of it, so this could cause issues in future if the package is installed via MacPorts. As a rule of thumb, don't ever install anything into/opt/local/
other than via theport
command. If you did mistakenly install packages to the MacPorts directory, just go to/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
and remove the files relating to the package you installed.
Appendix: Available Python Astronomy Packages
Copyright 2011, Thomas Robitaille