小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Python 使用matplotlib 畫(huà)數(shù)學(xué)公式圖與散點(diǎn)圖

 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()  

我們調(diào)用numpy的方法sin() 和 cos() 

 用linspace()得到1000個(gè)點(diǎn)。

 linspace (起點(diǎn),終點(diǎn),元素個(gè)數(shù))

[python] view plain copy
在CODE上查看代碼片派生到我的代碼片
  1. <span style="font-size:14px;">plt.plot(x,y,label='$sin(x)$',color='red',linewidth=3)</span>  
plot() 傳入點(diǎn),標(biāo)簽 和顏色

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

傳入xy軸標(biāo)簽


[python] view plain copy
在CODE上查看代碼片派生到我的代碼片
  1. plt.title('First python firgure')  
  2. plt.ylim(-1.2,1.2)  
設(shè)置圖片標(biāo)題,和y軸范圍。

我們得到這樣的圖

            

畫(huà)散點(diǎn)圖是我們使用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得到數(shù)據(jù)。我們得到了這樣的圖



    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多