python画图

#coding=utf-8
'''
Created on Nov 22, 2010

@author: Peter
'''

from numpy import *
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.patches import Circle

xcord0 = []
ycord0 = []
xcord1 = []
ycord1 = []
markers =[]
colors =[]
fr = open('testSet.txt')  #this file was generated by 2normalGen.py
for line in fr.readlines():
    lineSplit = line.strip().split('\t')  #得到第一行数据,并为list格式
    xPt = float(lineSplit[0])
    yPt = float(lineSplit[1])
    label = int(lineSplit[2])  #这三个变量依次为第一,第二,第三列数据
    if (label == -1):  #如果为负例,则将x,y坐标赋值给cord0否则赋值给cord1
        xcord0.append(xPt)
        ycord0.append(yPt)
    else:
        xcord1.append(xPt)
        ycord1.append(yPt)

fr.close()#关闭打开的文件
fig = plt.figure()#生成一个640*420的画布
ax = fig.add_subplot(111)#画布分成一行一列,图片在第一块
ax.scatter(xcord0,ycord0, marker='s', s=90)#在坐标系中画点,前两个参数为点坐标,marker=s为方形,s=90为大小,c=red为坐标颜色
ax.scatter(xcord1,ycord1, marker='o', s=50, c='red')
plt.title('Support Vectors Circled')
#坐标表示要画圈的地方,第二个参数表示圈的大小,faceclolor表示,edgecolor表示圈的颜色值,linewidth表示圈的线条粗细,alpha表示颜色深浅
circle = Circle((4.6581910000000004, 3.507396), 0.5, facecolor='none', edgecolor=(0,0.8,0.8), linewidth=3, alpha=0.5)
ax.add_patch(circle)
circle = Circle((3.4570959999999999, -0.082215999999999997), 0.5, facecolor='none', edgecolor=(0,0.8,0.8), linewidth=3, alpha=0.5)
ax.add_patch(circle)
circle = Circle((6.0805730000000002, 0.41888599999999998), 0.5, facecolor='none', edgecolor=(0,0.8,0.8), linewidth=3, alpha=0.5)
ax.add_patch(circle)
#plt.plot([2.3,8.5], [-6,6]) #seperating hyperplane
b = -3.75567; w0=0.8065; w1=-0.2761#参数已经训练好了
x = arange(-2.0, 12.0, 0.1)
y = (-w0*x - b)/w1
ax.plot(x,y)#画出直线
ax.axis([-2,12,-8,6])#设置坐标轴,-2~12位横轴;-8~6位纵轴
plt.show()
posted @ 2016-07-15 22:22  YC_Yuan  阅读(822)  评论(0编辑  收藏  举报