python--matplotlib库使用2,,功能:python画


功能:python画2D图

直接上代码,注释很详细!

 1 #-*- coding:utf-8 -*- 2  3 from numpy import * 4 import matplotlib.pyplot as plt 5  6 A=array([0,0]);B=array([[1,0],[0,1]]) 7 #生成以A为均值,以B为协方差矩阵,并满足正态分布的随机数 8 C1=random.multivariate_normal(A,B,10)          #10*2 9 C2=random.multivariate_normal(A+0.1,B+0.1,20)  #20*210 C2=mat(C2)    #这样做只是为了说明ndarray和matrix型数据的不同11 12 fig=plt.figure(figsize=(5,5))  #制定画布大小13 ax=fig.add_subplot(111)14 15 #ndarray型数据C1,shpe(C1)得到(10L,);matrix型C=mat(C1),shpe(C[:,1])得到(10L,1L)16 ax.scatter(C1[:,0],C1[:,1],s=50,c=‘r‘,marker=‘o‘,alpha=0.5,label=‘C1‘)17 ax.scatter(C2[:,0].flatten().A[0],C2[:,1].flatten().A[0],s=30,c=‘b‘,marker=‘^‘,alpha=0.5,label=‘C2‘)18 ‘‘‘19 scatter(x,y,x=None,c=None,marker=None)20 x,y必须是1-D21 marker为标记,s控制标记的大小,c控制标记的颜色22 flatten()函数 :a.flatten(),得到一个将a转换为1-D的copy23 另外,matrix(也就是mat(a))才有A属性,可以使用mat(a).flatten().A[0],得到一个1-D的“array”24 ‘‘‘25 plt.title(‘scatter plot‘)26 plt.xlabel(‘the Variable X‘)27 plt.ylabel(‘the Variable Y‘)28 29 ax.legend(loc=‘upper left‘)      #确定方框位置30 plt.show()

关于官方pyplot.scatter()函数详解见:http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter

  

python--matplotlib库使用2

评论关闭