python matplotlib framework under macosx?

Question:

I am getting this error:

/sw/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py:235:
UserWarning: Python is not installed as a framework. The MacOSX
backend may not work correctly if Python is not installed as a
framework. Please see the Python documentation for more information on
installing Python as a framework on Mac OS X

I installed python27 using fink and it’s using the default matplotlib is using macosx framework.

Asked By: Neil G

||

Answers:

There are two ways Python can be built and installed on Mac OS X. One is as a traditional flat Unix-y shared library. The other is known as a framework install, a file layout similar to other frameworks on OS X where all of the component directories (include, lib, bin) for the product are installed as subdirectories under the main framework directory. The Fink project installs Pythons using the Unix shared library method. Most other distributors, including the Apple-supplied Pythons in OS X, the python.org installers, and the MacPorts project, install framework versions of Python. One of the advantages of a framework installation is that it will work properly with various OS X API calls that require a window manager connection (generally GUI-related interfaces) because the Python interpreter is packaged as an app bundle within the framework.

If you do need the functions in matplotlib that require the GUI functions, the simplest approach may be to switch to MacPorts which also packages matplotlib (port py27-matplotlib) and its dependencies. If so, be careful not to mix packages between Fink and MacPorts. It’s best to stick with one or the other unless you are really careful. Adjust your shell path accordingly; it would be safest to remove all of the Fink packages and install MacPorts versions.

Answered By: Ned Deily

This shows up for me when switching to OSX 10.10 (Yosemite). I fixed it by switching to the WXAgg backend.

fink install wxpython300-py27
mkdir ~/.matplotlib
echo "backend : WXAgg" >>~/.matplotlib/matplotlibrc

http://matplotlib.org/1.3.0/faq/usage_faq.html#what-is-a-backend

If the matplotlibrc file is not found there, put matplotlibrc and check the location with:

import matplotlib as mpl
mpl.get_configdir()
Answered By: David M. Rogers

Optionally you could use the Agg backend which requires no extra installation of anything. Just put backend : Agg into ~/.matplotlib/matplotlibrc

Answered By: Jonathan

Some users may not want to change the backend for all of their scripts. This page — http://matplotlib.org/faq/usage_faq.html#what-is-a-backend — tells another way:

import matplotlib
matplotlib.use('TkAgg')

You have to do this before importing a subpackage of matplotlib or a third-party package that depends on matplotlib.

Answered By: Iron Pillow

I hit this problem using the Anaconda distribution of Python on my computer and a virtual conda environment with Python 3.4.

After doing “pip install matplotlib”, I could import matplotlit in my code fine, but it would give an error when I tried to plot images.

So I went back to the Terminal and used the recommended conda command for Anaconda Python (“conda install matplotlib”), and the issue resolved itself (keep in mind this was all within an environment that I had created using the conda command tool at http://conda.pydata.org/docs/using/envs.html). My system is running Mac OSX 10.10.5.

Answered By: Wendy Jan

Check out the official FAQ on for Working with Matplotlib on OSX. It has several solutions depending on your situation. The one that worked for me is:

  • Step 1: Open terminal
  • Step 2: Execute conda install python.app to install python.app
  • Step 3: Run python script with pythonw e.g. pythonw my_script.py
Answered By: Jonny Brooks

If you are facing the same problem even after adding "backend: TkAgg" in ~/.matplotlib/matplotlibrc their might a problem with your virtual environment.

So if your are on python3 use venv instead of virtualenv.

To use venv with python3 use python -m venv my-virtualenv to make a virtual environment and use source my-virtualenv/bin/activate to activate the virtualenv.

Rest is same as virtualenv. Otherwise use
PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install x.x.x if you want to use virtualenv as your virtual environment.

Answered By: Sachin

Generally, I’ve been using

import matplotlib
matplotlib.use('Agg')

to solve this. Recently however I got this framework error running tests on a codebase I should only be making very limited changes to. In that case, following matplotlib’s OSX documentation’s advice on how I set up my virtual environment worked for me:

If you are on Python 3, use venv instead of virtualenv:

python -m venv my-virtualenv
source my-virtualenv/bin/activate

from: https://matplotlib.org/faq/osx_framework.html

Answered By: Max Power

Import the library by following code sequences works for me. With this does not need to create new folder:

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
Answered By: after aver

Simply aliasing a new command to launch python in ~/.bash_profile will do the trick.

alias vpython3=/Library/Frameworks/Python.framework/Versions/3.6(replace with your own python version)/bin/python3

then ‘source ~/.bash_profile’ and use vpython3 to launch python3.

Explanation: Python is actually by default installed as framework on mac, but using virtualenv would link your python3 command under the created virtual environment, instead of the above framework directory (‘which python3’ in terminal and you’ll see that). Perhaps Matplotlib has to find the bin/ include/ lib/,etc in the python framework.

Answered By: Alchanic Liu

I’m running OSX 10.14 with Python 3.7.1 installed with pyenv.

I could not get any of the backends listed in the other answers to work for me (TkAgg, WXAgg, macosx).

Instead what worked is the newer Qt5Agg backend, installed with pip install pyqt5, and then creating a ~/.matplotlib/matplotlibrc file containing backend: Qt5Agg (per the other answers).

Answered By: Derek Hill
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.