国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Python 使用matplotlib 畫數(shù)學(xué)公式圖與散點圖


        

[python] view plain copy
  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個點。

 linspace (起點,終點,元素個數(shù))

[python] view plain copy
  1. <span style="font-size:14px;">plt.plot(x,y,label='$sin(x)$',color='red',linewidth=3)</span>  
plot() 傳入點,標(biāo)簽 和顏色

[python] view plain copy
  1. plt.xlabel('Time(s)')  
  2. plt.ylabel('volt')  

傳入xy軸標(biāo)簽


[python] view plain copy
  1. plt.title('First python firgure')  
  2. plt.ylim(-1.2,1.2)  
設(shè)置圖片標(biāo)題,和y軸范圍。

我們得到這樣的圖

            

畫散點圖是我們使用scatter()

[python] view plain copy
  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ù)。我們得到了這樣的圖



本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
[Python從零到壹] 十七.可視化分析之Matplotlib、Pandas、Echarts入門萬...
【數(shù)字的可視化:python畫圖之散點圖sactter函數(shù)詳解】
python數(shù)據(jù)分析工具之 matplotlib詳解
斯坦福CS231n深度學(xué)習(xí)課程筆記翻譯(一)
3 個用于數(shù)據(jù)科學(xué)的頂級 Python 庫
Python中基礎(chǔ)使用及Numpy、Scipy、Matplotlib 使用教程
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服