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

42,536

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

Solution 3

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.

Solution 4

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

enter image description here

Solution 5

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.

Share:
42,536
Rohit
Author by

Rohit

An Astrophysicist who's getting away from IRAF &amp; IDL and into Python

Updated on April 19, 2020

Comments

  • Rohit
    Rohit about 4 years

    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?

  • Rudresh Ajgaonkar
    Rudresh Ajgaonkar almost 8 years
    Nice and Easy. Thanks a lot!
  • msanford
    msanford almost 8 years
    One-liner: echo "backend : TkAgg" > ~/.matplotlib/matplotlibrc or echo "backend : Agg" > ~/.matplotlib/matplotlibrc
  • aheigins
    aheigins about 7 years
    i get a 'no such file or directory error' when i try this even as root. Should this file have some sort of extension? (on mac 10.12.3)
  • nngeek
    nngeek over 6 years
    I am using macOS high sierra, When I use TkAgg, it shows I don't have tkinter package installed. When I use Qt4Agg or Qt5Agg, it shows I don't have PyQt installed. It does not help if I install PyQt using brew install pyqt. In the end, I have to install it in virtualenv, "activate the virtualenv, then pip install PyQt5". This works.
  • Hanfeng
    Hanfeng over 6 years
    Thanks! I like this answer!
  • Charls
    Charls over 6 years
    For me I had a python 2.7 pyenv environment with matplotlib==2.1.0 and python 3.6 with matplotlib==2.1.1 . I upgraded 2.7's matplotlib to 2.1.1 and problem was fixed.
  • Bostone
    Bostone about 6 years
    matplotlibrc is initially missing. Just create it if not found
  • Jordan
    Jordan almost 6 years
    What should the file name be?
  • ratiaris
    ratiaris over 5 years
    Worked for me as well. Saved my day :-) Thanks a lot!