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

Solution 1:

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.

Solution 2:

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()