简体   繁体   English

如何解决 import matplotlib.pyplot as plt 错误?

[英]How to resolve import matplotlib.pyplot as plt error?

I am having trouble getting my matplotlib library to work.我无法让我的 matplotlib 库正常工作。 I am running it on a virual environment, and I have installed numpy, and matplotlib.我在虚拟环境中运行它,并且安装了 numpy 和 matplotlib。 I first open my virtual environment, activate python, then I enter,我先打开我的虚拟环境,激活python,然后我进入,

import matplotlib 

everything goes fine, but then I enter,一切顺利,但我进入,

import matplotlib.pyplot as plt

I get the error,我得到错误,

    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

I have tried a number of different solutions to no avail.我尝试了许多不同的解决方案都无济于事。 What could be the problem?可能是什么问题呢?

Use the below mentioned commands:使用下面提到的命令:

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

This worked for me.这对我有用。 I hope it helps.我希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM