Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to "import matplotlib.pyplot as plt" in virtualenv

I am working with flask in a virtual environment. I was able to install matplotlib with pip, and I can import matplotlib in a Python session. However, when I import it as

matplotlib.pyplot as plt 

I get the following error:

>>> import matplotlib.pyplot as plt  Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 109, in <module>     _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()   File "//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup     globals(),locals(),[backend_name],0)   File "//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 24, 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. 

I am confused about why it asks me to install Python as framework. Doesn't it already exists? What does it mean to "install Python as framework", and how do I install it?

like image 902
Rohit Avatar asked Apr 03 '15 14:04

Rohit


People also ask

Why is matplotlib not importing?

Matplotlib is not a built-in module (it doesn't come with the default python installation) in Python, you need to install it explicitly using the pip installer and then use it. If you looking at how to install pip or if you are getting an error installing pip checkout pip: command not found to resolve the issue.

What does it mean import matplotlib Pyplot as PLT?

import matplotlib. pyplot as plt gives an unfamiliar reader a hint that pyplot is a module, rather than a function which could be incorrectly assumed from the first form.

What is Pyplot CLF ()?

The clf() function in pyplot module of matplotlib library is used to clear the current figure.


2 Answers

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

$ cd ~/.matplotlib $ nano matplotlibrc 

And then, write backend: TkAgg in there. If you need more information, just go to the solution link.

like image 56
Hercules Avatar answered Oct 12 '22 05:10

Hercules


I got the same error, and tried Jonathan's answer:

You can fix this issue by using the backend Agg

Go to User/yourname/.matplotlib and open/create matplotlibrc and add the following line backend : Agg and it should work for you.

I run the program, no error, but also no plots, and I tried backend: Qt4Agg, it prints out that I haven't got PyQt4 installed.

Then I tried another backend: backend: TkAgg, it works!

So maybe we can try difference backends and some may work or install the requeired packages like PyQt4.

Here is a sample python snippet that you can try and test matplotlib.

import matplotlib  matplotlib.use('TkAgg') import matplotlib.pyplot as plt  plt.plot([1, 2, 3], [0, 3, 7]) plt.show() 
like image 29
JasonWayne Avatar answered Oct 12 '22 04:10

JasonWayne