分享

Python 使用matplotlib 画数学公式图与散点图

 imelee 2017-03-14


        

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. import numpy as np  
  2. import matplotlib.pyplot as plt  
  3.   
  4. x=np.linspace(0,10,1000)  
  5. y=np.sin(x)  
  6. z=cos(x^2)  
  7.      
  8. plt.figure(figsize=(8,4))  
  9.    
  10. plt.plot(x,y,label='$sin(x)$',color='red',linewidth=3)  
  11.    
  12. plt.plot(x,z,'g--',label='$cos(x^2)$',lw=3)  
  13.   
  14. plt.xlabel('Time(s)')  
  15. plt.ylabel('volt')  
  16. plt.title('First python firgure')  
  17. plt.ylim(-1.2,1.2)  
  18. plt.legend()  
  19.    
  20. plt.show()  

我们调用numpy的方法sin() 和 cos() 

 用linspace()得到1000个点。

 linspace (起点,终点,元素个数)

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">plt.plot(x,y,label='$sin(x)$',color='red',linewidth=3)</span>  
plot() 传入点,标签 和颜色

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. plt.xlabel('Time(s)')  
  2. plt.ylabel('volt')  

传入xy轴标签


[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. plt.title('First python firgure')  
  2. plt.ylim(-1.2,1.2)  
设置图片标题,和y轴范围。

我们得到这样的图

            

画散点图是我们使用scatter()

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. #-*-coding:utf-8-*  
  2. import matplotlib  
  3. import matplotlib.pyplot as plt  
  4. import numpy as np  
  5. def file2matrix(filename):    
  6.       
  7.     fr = open(filename)    
  8.     arrayOLines = fr.readlines()    
  9.     numberOfLines = len(arrayOLines)    
  10.       
  11.     returnMat = np.zeros((numberOfLines,2))    
  12.     classLabelVector = []    
  13.     index =0    
  14.     for line in arrayOLines:    
  15.         line = line.strip()    
  16.         listFormLine = line.split(' ')    
  17.         returnMat[index,:] = listFormLine[0:2]    
  18.         classLabelVector.append(int(listFormLine[-1]))    
  19.         index += 1    
  20.     return returnMat, classLabelVector    
  21. matrix, labels = file2matrix('Train.txt')   
  22. print matrix   
  23. print labels   
  24.   
  25. plt.figure(figsize=(8, 5), dpi=80)   
  26. axes = plt.subplot(111)   
  27. type1_x = []  
  28. type1_y = []  
  29. type2_x = []  
  30. type2_y = []   
  31. print 'range(len(labels)):'   
  32. print range(len(labels))   
  33. for i in range(len(labels)):   
  34.     if labels[i] == 0:   
  35.         type1_x.append(matrix[i][0])   
  36.         type1_y.append(matrix[i][1])   
  37.     if labels[i] == 1:   
  38.         type2_x.append(matrix[i][0])   
  39.         type2_y.append(matrix[i][1])   
  40.         #print i, ':', labels[i], ':', type(labels[i])   
  41. type1 = axes.scatter(type1_x, type1_y,s=40, c='red' )   
  42. type2 = axes.scatter(type2_x, type2_y, s=40, c='green')  
  43. W1 = 1.23924482  
  44. W2 = 1.59913719  
  45. B = -6.67130613  
  46. x = np.linspace(-4,10,200)  
  47. y = (-W1/W2)*x+(-B/W2)  
  48. axes.plot(x,y,'b',lw=3)  
  49. #plt.scatter(matrix[:, 0], matrix[:, 1], s=20 * numpy.array(labels),   
  50. #             c=50 * numpy.array(labels), marker='o',   
  51. #             label='test')   
  52. plt.xlabel('x1')   
  53. plt.ylabel('x2')   
  54. axes.legend((type1, type2), ('0', '1'),loc=1)   
  55. plt.show()  

我们从Train.txt得到数据。我们得到了这样的图



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约