首页 > 代码库 > 机器学习(2)—— matplotlib

机器学习(2)—— matplotlib

  python34 中绘图基本是matplotlib库,基于matplotlib库可以绘制基本的图形。

  1、基本绘图

  (1)点图(data:http://pan.baidu.com/s/1i3L0UDB)

 1 import matplotlib.pyplot as plt 2 import math 3 from numpy import * 4 def file2matrix(filename): 5     fr = open(filename) 6     numberOfLines = len(fr.readlines())         #get the number of lines in the file 7     returnMat = zeros((numberOfLines,3))        #prepare matrix to return 8     classLabelVector = []                       #prepare labels return    9     fr = open(filename)10     index = 011     for line in fr.readlines():12         line = line.strip()13         listFromLine = line.split(\t)14         returnMat[index,:] = listFromLine[0:3]15         classLabelVector.append(int(listFromLine[-1]))16         index += 117     return returnMat,classLabelVector18 #    return returnMat19 20 21 22 datingDataMat,datingLabels = file2matrix(datingTestSet2.txt)23 fig = plt.figure()24 ax = fig.add_subplot(111)25 ax.scatter(datingDataMat[:,1],datingDataMat[:,2],15.0*array(datingLabels),15.0*array(datingLabels))26 plt.show()

 

  

机器学习(2)—— matplotlib