我在一个虚拟环境中使用flask。我能够使用pip安装matplotlib,并且我可以在Python会话中使用import matplotlib
。但是,当我将其导入为
matplotlib.pyplot as plt
我得到以下错误:
>>> 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.
我不明白为什么它要求我安装Python作为框架。它不是已经存在了吗?“将Python安装为框架”是什么意思,如何安装?
发布于 2016-01-31 05:09:05
这个solution对我很有效。如果您已经使用pip在您的虚拟环境中安装了matplotlib,则只需键入以下内容:
$ cd ~/.matplotlib
$ nano matplotlibrc
然后,在那里编写backend: TkAgg
。如果您需要更多信息,只需转到解决方案链接。
发布于 2015-10-31 10:39:30
当我使用pip安装matplotlib时也遇到了类似的问题。默认情况下,它安装的最新版本是1.5.0。但是,我有另一个使用Python3.4和matplotlib1.4.3的虚拟环境,当我导入matplotlib.pyplot时,这个环境工作得很好。因此,我使用以下命令安装了matplotlib的早期版本:
cd path_to_virtual_environment # assume directory is called env3
env3/bin/pip install matplotlib==1.4.3
我知道这只是一种变通办法,但它对我来说是一种短期的解决办法。
发布于 2016-09-17 04:17:09
如果您不想设置.matplotib/matplotlibrc
配置文件,可以通过在导入matplotlib
之后、导入matplotlib.pyplot
之前在运行时设置'Agg'
后端来解决此问题
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')
https://stackoverflow.com/questions/29433824
复制相似问题