如何解决导入matplotlib.pyplot作为plt错误?

时间:2017-04-23 15:49:03

标签: python python-2.7 matplotlib

我无法让matplotlib库工作。我在一个虚拟的环境中运行它,我已经安装了numpy和matplotlib。我首先打开我的虚拟环境,激活python,然后进入,

import matplotlib 

一切顺利,但我进入,

import matplotlib.pyplot as plt

我收到了错误,

    Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
    from six.moves import tkinter as Tk
  File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/six.py", line 203, in load_module
    mod = mod._resolve()
  File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/six.py", line 82, in _import_module
    __import__(name)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

我尝试了许多不同的解决方案无济于事。可能是什么问题呢?

1 个答案:

答案 0 :(得分:0)

使用以下命令:

import matplotlib
matplotlib.use(‘TkAgg’)
import matplotlib.pyplot as plt

这对我有用。我希望它有所帮助。