Centos 7+python3安装matplot

Centos 7+python3安装matplot

  • Centos 7+python3安装matplot
    • 安装matplot
    • 问题解决
    • 重新编译python3
    • 测试matplot做图

Centos 7+python3安装matplot

安装matplot

首先使用pip3安装matplolib

[root@VM_0_14_centos ~]# pip3 install matplotlib
Collecting matplotlib
  Downloading http://mirrors.tencentyun.com/pypi/packages/71/07/16d781df15be30df4acfd536c479268f1208b2dfbc91e9ca5d92c9caf673/matplotlib-3.0.2-cp36-cp36m-manylinux1_x86_64.whl (12.9MB)
    100% |████████████████████████████████| 12.9MB 41.4MB/s 
Collecting cycler>=0.10 (from matplotlib)
  Downloading http://mirrors.tencentyun.com/pypi/packages/f7/d2/e07d3ebb2bd7af696440ce7e754c59dd546ffe1bbe732c8ab68b9c834e61/cycler-0.10.0-py2.py3-none-any.whl
Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 (from matplotlib)
  Downloading http://mirrors.tencentyun.com/pypi/packages/71/e8/6777f6624681c8b9701a8a0a5654f3eb56919a01a78e12bf3c73f5a3c714/pyparsing-2.3.0-py2.py3-none-any.whl (59kB)
    100% |████████████████████████████████| 61kB 23.3MB/s 
Requirement already satisfied: numpy>=1.10.0 in /usr/local/python3Dir/lib/python3.6/site-packages (from matplotlib)
Collecting kiwisolver>=1.0.1 (from matplotlib)
  Downloading http://mirrors.tencentyun.com/pypi/packages/69/a7/88719d132b18300b4369fbffa741841cfd36d1e637e1990f27929945b538/kiwisolver-1.0.1-cp36-cp36m-manylinux1_x86_64.whl (949kB)
    100% |████████████████████████████████| 952kB 34.4MB/s 
Collecting python-dateutil>=2.1 (from matplotlib)
  Downloading http://mirrors.tencentyun.com/pypi/packages/74/68/d87d9b36af36f44254a8d512cbfc48369103a3b9e474be9bdfe536abfc45/python_dateutil-2.7.5-py2.py3-none-

问题解决

使用matplot做图报错:

import _tkinter # If this fails your Python may not be configured for Tk
No module named '_tkinter'

tkinter其实是Python调用tcl程序的标准Python程序,可以通过这个interface调用tcl的程序,因为在大多数的unix系统中都内置了很多的tcl程序和命令。

Tcl 是“工具控制语言(Tool Command Language)”的缩写,其面向对象为otcl语言。Tk 是 Tcl“图形工具箱”的扩展,它提供各种标准的 GUI 接口项,以利于迅速进行高级应用程序开发。

解决办法是:
先安装tk和tcl

[root@VM_0_14_centos ~]# yum install python3-tk

安装tk时报错

[root@VM_0_14_centos ~]# yum install python3-tk
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel                                                     | 4.7 kB     00:00     
extras                                                   | 3.4 kB     00:00     
mysql-connectors-community                               | 2.5 kB     00:00     
mysql-tools-community                                    | 2.5 kB     00:00     
mysql56-community                                        | 2.5 kB     00:00     
os                                                       | 3.6 kB     00:00     
updates                                                  | 3.4 kB     00:00     
  File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:
                  ^
SyntaxError: invalid syntax
  File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:
                  ^
SyntaxError: invalid syntax


Exiting on user cancel

原因:

这是因为yum采用Python作为命令解释器,这可以从/usr/bin/yum文件中第一行#!/usr/bin/python发现。而python版本之间兼容性不太好,使得2.X版本与3.0版本之间存在语法不一致问题。而CentOS 7自带的yum采用的是python2.7,当系统将python升级到3.6.2后,出现语法解释错误。
解决方法,修改urlgrabber-ext-down文件

vi /usr/libexec/urlgrabber-ext-down
#! /usr/bin/python2.7
#  A very simple external downloader
#  Copyright 2011-2012 Zdenek Pavlas

#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public
#   License as published by the Free Software Foundation; either
#   version 2.1 of the License, or (at your option) any later version.
#
#   This library is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public
#   License along with this library; if not, write to the
#      Free Software Foundation, Inc.,
#      59 Temple Place, Suite 330,
#      Boston, MA  02111-1307  USA

import time, os, errno, sys
from urlgrabber.grabber import \
    _readlines, URLGrabberOptions, _loads, \

继续安装tk和tcl

[root@VM_0_14_centos ~]# yum install python3-tk^C
[root@VM_0_14_centos ~]# yum insatll tk-devel

重新编译python3

cd /usr/local/python3
./configure --prefix=/usr/local/python3Dir --with-ssl
make && make install

编译成功!

测试matplot做图

import matplotlib.lines as mlines

Centos 7+python3安装matplot_第1张图片

你可能感兴趣的:(Python学习)