512

I have issue after installing the matplotlib package unable to import matplotlib.pyplot as plt. Any suggestion will be greatly appreciate.

>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "//anaconda/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.5-x86_64.egg/matplotlib/pyplot.py", line 98, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "//anaconda/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.5-x86_64.egg/matplotlib/backends/__init__.py", line 28, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "//anaconda/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.5-x86_64.egg/matplotlib/backends/backend_macosx.py", line 21, in <module>
    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.
J4cK
  • 30,459
  • 8
  • 42
  • 54
  • This [link](http://stackoverflow.com/questions/4130355/python-matplotlib-framework-under-macosx) reports to the same issue you are presenting here. – Jack Feb 14 '14 at 20:12
  • 3
    Running pythonw instead of python worked for me – phoenix Nov 23 '17 at 19:45

1 Answers1

1346

Problem Cause

In mac os image rendering back end of matplotlib (what-is-a-backend to render using the API of Cocoa by default). There are Qt4Agg and GTKAgg and as a back-end is not the default. Set the back end of macosx that is differ compare with other windows or linux os.

Solution

  • I assume you have installed the pip matplotlib, there is a directory in your root called ~/.matplotlib.
  • Create a file ~/.matplotlib/matplotlibrc there and add the following code: backend: TkAgg

From this link you can try different diagrams.

J4cK
  • 30,459
  • 8
  • 42
  • 54
  • 11
    Yep, this works! If anyone wants to understand what happened, see http://matplotlib.org/faq/usage_faq.html#what-is-a-backend – Andrey Shokhin Dec 09 '14 at 11:25
  • 276
    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: include the statement `import matplotlib as mpl` then `mpl.use('TkAgg')` right after that, then do the import for pyplot. – Iron Pillow Jan 04 '16 at 02:25
  • 20
    As a follow up question, why is there a folder .matplotlib in my root folder instead of in the virtual environment where I installed it? – PDN Feb 19 '16 at 03:33
  • 1
    I am using python 3.4 and got the error in this question. I then got `Class TKApplication is implemented in both /Users/dhruv/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.` and errors like this. How can I adjust the answer to solve this? – Dhruv Ghulati Feb 22 '16 at 19:25
  • 7
    Python 3.3.6, I get this: `File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 40, in import _tkinter # If this fails your Python may not be configured for Tk ImportError: No module named '_tkinter'` – Josh Hansen Feb 27 '16 at 00:30
  • moreover, for .virtualenvs you need to copy tcl/ tk into .virtualenvs/lib/ folder – longchuan May 05 '17 at 10:48
  • @longchuan where and what is the tcl file? – Patrick Jun 30 '17 at 21:38
  • 2
    Thanks for this :D I used virtualenv with your fix and it worked for me! – Dhruv Batheja Sep 29 '17 at 12:46
  • Precisely, it does not work when using virtualenv and put this configuration at root of your project. – UnixAgain Jan 10 '18 at 08:19
  • Works for VirtualEnv. – Kai Wang Aug 17 '18 at 19:25
  • Amazed by how simple this thing was to solve. Even more amazed by how I just can't understand what is happening. Who can explain in simple words? In 50 words or less? Thanks – Tiago Bruno Oct 03 '18 at 02:23
  • 1
    I added `backend: Agg` to *matplotlibrc* and it worked too. – Tapper Jun 04 '19 at 01:30
  • I don't give a fun, what is going on here. But it's really works in 2019. – GensaGames Oct 31 '19 at 21:42