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

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


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.


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 matplotlibmatplotlib.use('TkAgg')import matplotlib.pyplot as pltplt.plot([1, 2, 3], [0, 3, 7])plt.show()


I had similar problem when I used pip to install matplotlib. By default, it installed the latest version which was 1.5.0. However, I had another virtual environment with Python 3.4 and matplotlib 1.4.3 and this environment worked fine when I imported matplotlib.pyplot. Therefore, I installed the earlier version of matplotlib using the following:

cd path_to_virtual_environment    # assume directory is called env3env3/bin/pip install matplotlib==1.4.3

I know this is only a work-around, but it worked for me as a short-term fix.