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 im
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 env3
env3/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.
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 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/creatematplotlibrc
and add the following linebackend : 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()
A clean and easy solution is to create a kernel that sets PYTHONHOME
to VIRTUAL_ENV
and then uses the system Python executable (instead of the one in the virtualenv).
If you want to automate the creation of such a kernel, you can use the jupyter-virtualenv-osx script.
If you do not want to set a .matplotib/matplotlibrc
configuration file, you can circumvent this issue by setting the 'Agg'
backend at runtime right after importing matplotlib
and before importing matplotlib.pyplot
:
In [1]: import matplotlib
In [2]: matplotlib.use('Agg')
In [3]: import matplotlib.pyplot as plt
In [4]: fig, ax = plt.subplots(1, 1)
In [5]: import numpy as np
In [6]: x = np.linspace(-1., 1.)
In [7]: y = np.sin(x)
In [8]: ax.plot(x, y)
Out[8]: [<matplotlib.lines.Line2D at 0x1057ecf10>]
In [9=]: fig.savefig('myplot.png')
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.