import numpy import matplotlib.pyplot as plt #初始化numpy.array数组 x = numpy.array([[0, 0], [-1, 0.1], [0.3, -0.05], [0.7, 0.3], [-0.2, -0.6], [-0.15, -0.63], [-0.25, 0.55], [-0.28, 0.67]]) y = numpy.array([0, 0, 0, 0, 1, 1, 2, 2]) #Python3使用scatter画散点图,定义一个小函数 def plot_data(features, labels): x0,x1,x2 = features[labels==0], features[labels==1], features[labels==2] plt.scatter(x0[:,0], x0[:,1], c='red', s=40) plt.scatter(x1[:,0], x1[:,1], c='green', s=40) plt.scatter(x2[:,0], x2[:,1], c= 'blue', s=40) plt.axis([-1.5,1.5, -1.5,1.5])#设置横纵坐标 plt.figure(1)#画第一张图 plt.subplot(221)#一块画布分为2行2列共4份,此时占用第一份 plt.title("scatter points",fontsize=24)#设置title x y plt.xlabel("Features",fontsize=14) plt.ylabel("Labels",fontsize=14) plot_data(x,y)#调用函数 plt.show()#show plt.figure(2) plt.title("scatter points",fontsize=24) plt.xlabel("Features",fontsize=14) plt.ylabel("Labels",fontsize=14) x0, x1, x2 = x[y == 0], x[y == 1], x[y == 2] plt.scatter(x0[:, 0], x0[:, 1], marker='*', c='red', label=0, s=40)#marker定义形状,label与plt.legend画出右上角图例 plt.scatter(x1[:, 0], x1[:, 1], marker='+', c='green', label=1, s=40)# * + o x s是方块 d是菱形 plt.scatter(x2[:, 0], x2[:, 1], marker='o', c='blue', label=2, s=40) plt.axis([-1.5, 1.5, -1.5, 1.5]) plt.legend(loc='upper right')#center left lower right right upper center lower center center right lower left best..... plt.show()
Figure1
Figure2