Unable to “import matplotlib.pyplot as plt” in virtualenv

If you see the same issue (See the error message below) when you try to import matplotlib.pyplot in the Virtualenv, the following solution could be used as workaround

>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "", line 1, in 
  File "/Users/xxxxx/Development/ML/finance/lib/python3.6/site-packages/matplotlib/pyplot.py", line 115, in 
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/Users/xxxxx/Development/ML/finance/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/Users/xxxxx/Development/ML/finance/lib/python3.6/site-packages/matplotlib/backends/backend_macosx.py", line 19, 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 using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.
>>> 

This solution worked for me. If you already installed matplotlib using pip on your virtual environment, you can just type the following:

$ cd ~/.matplotlib
$ vi matplotlibrc

Edit the file “matplotlibrc with content -> “backend: TkAgg”

Advertisements

About whoismikechan

I am just a guy who has a family, working daily with my daily work, but always dream about to have my own startup company.
This entry was posted in Python. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s