47
votes

This question has been asked before, in here, also here. However, the solution didn't fix the problem for my case.

The original error is, when I try to import matplotlib.pyplot, I got:

Traceback (most recent call last): File "", line 1, in File "/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 114, in _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup() File "/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/init.py", line 32, in pylab_setup globals(),locals(),[backend_name],0) File "/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 24, in from matplotlib.backends import _macosx RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ

I followed the solutions to add a ~/.matplotlib/matplotlibrc file with the code: backend: TkAgg. After doing that, my error changed to:

/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment. warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.') objc[25120]: Class TKApplication is implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined. objc[25120]: Class TKMenu is implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined. objc[25120]: Class TKContentView is implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined. objc[25120]: Class TKWindow is implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.

I have no idea how to fix that. I'm not using a virtual machine. Could you help me? Thank you!

PS: I found out that by adding:

import matplotlib
matplotlib.use('TkAgg')

before import matplotlib.pyplot, it seems to work. But adding those two lines of codes every time is annoying... Does anyone know what's going on and how I can fix it? Thank you!

6
I am in a similar condition with you. (I am on Mac OS X, using anaconda python, "TkAgg" backend, the error message is the same)When I run the script, it just crashed.Pei Guo

6 Answers

90
votes

I run my script in virtualenv. Python version is 3.5.

Add a line:

backend: TkAgg

in file:

~/.matplotlib/matplotlibrc

This solved the problem.

If you want to know more about why adding this solves the problem, you can read about customizing matplotlib's backend. And TkAgg solves this issue because of it's dependency with Tkinter.

37
votes

Below worked for me:

import matplotlib  
matplotlib.use('TkAgg')   
import matplotlib.pyplot as plt  

Reference: https://github.com/tensorflow/tensorflow/issues/2375

5
votes

I installed Jupyter Notebook in virtualenv and below worked for me:

At Console:

echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc
pip install matplotlib==2.1.0

On Notebook:

import matplotlib

Restart the kernel:

dataframe.plot.hist()
...

In my opinion, it will fix the error.

2
votes

I had the same issues in python 2.7 in virtualenv and I managed to fix those by simply downgrading matplotlib to version 2.1.0

0
votes

I was also having the same error. What I have done is to install miniconda packages and using the pythonw. Python app was already installed in my mac. Installation is as simple as breaking an egg. Just bash the .sh file in the terminal.

0
votes

I was using pyenv so the matplotlibrc path wasn't in the home directory, so I created this script to figure the path out and change the backend to Tkagg:

vim $(python -c "import os,matplotlib; print(os.path.join(os.path.dirname(matplotlib.__file__), 'mpl-data/matplotlibrc'));")

This script should work with any python on your system though.