Unable to “import matplotlib.pyplot as plt” in virtualenv

前端 未结 7 1472
粉色の甜心
粉色の甜心 2020-12-04 07:39

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

相关标签:
7条回答
  • 2020-12-04 07:52

    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.

    0 讨论(0)
  • 2020-12-04 07:55

    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.

    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2020-12-04 07:56

    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.

    0 讨论(0)
  • 2020-12-04 07:58

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

    0 讨论(0)
  • 2020-12-04 08:08

    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.

    0 讨论(0)
提交回复
热议问题