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

Posted

技术标签:

【中文标题】如何解决 import matplotlib.pyplot as plt 错误?【英文标题】:How to resolve import matplotlib.pyplot as plt error? 【发布时间】:2017-09-20 06:12:53 【问题描述】:

我无法让我的 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

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

【问题讨论】:

for python pip install tkinter - 你可能想检查***.com/q/20044559/797495 试试这个:import matplotlibmatplotlib.use('Agg')import matplotlib.pyplot as plt 【参考方案1】:

使用下面提到的命令:

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

这对我有用。希望对你有帮助。

【讨论】:

以上是关于如何解决 import matplotlib.pyplot as plt 错误?的主要内容,如果未能解决你的问题,请参考以下文章

python ipython_matplotlib_img.py

Python可视化31|matplotlib-图添加文本(text)及注释(annotate)

如何解决此错误:找不到模块'import-local'

如何解决import org.apache.http.util 无法解决?

2022-07-07 如何解决node用require报错但用import又报错

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