Tips--解决安装matplotlib无法使用pyplot的问题

解决安装matplotlib无法使用pyplot的问题

      • 问题描述
      • 解决方法

问题描述

在安装matplolib的python2版本之后,发现在导入matplolib的时候是成功的:

import matplolib

但是在引入其中的pyplot包时是失败的:

import matplolib.pyplot as plt

报错信息为:

Traceback (most recent call last):
  File "", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/__init__.py", line 63, in pylab_setup
    [backend_name], 0)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 4, in <module>
    from . import tkagg  # Paint image to Tk photo blitter extension.
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/tkagg.py", line 5, in <module>
    from six.moves import tkinter as Tk
  File "/usr/local/lib/python2.7/dist-packages/six.py", line 203, in load_module
    mod = mod._resolve()
  File "/usr/local/lib/python2.7/dist-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/usr/local/lib/python2.7/dist-packages/six.py", line 82, in _import_module
    __import__(name)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 42, in <module>
    raise ImportError, str(msg) + ', please install the python-tk package'
ImportError: No module named _tkinter, please install the python-tk package

这是由于缺少python-tk的原因

解决方法

安装python-tk即可,注意是apt安装而不是pip进行安装:
Python2安装命令:

sudo apt-get install python-tk

Python3安装命令:

sudo apt-get install python3-tk

你可能感兴趣的:(•Tips)