Python利用Matplotlib畫曲線和柱狀圖

首先補充一下:兩種體系7種顏色 r g b y m c k (紅,綠,藍,黃,品紅,青,黑)


在科研的過程中,座標系中的XY不一定就是等尺度的。例如在聲波中對Y軸取對數。肆意我們也必須知道這種座標系如何畫出來的。


 1:對數座標圖

    有3個函數可以實現這種功能,分別是:semilogx(),semilogy(),loglog()。它們分別表示對X軸,Y軸,XY軸取對數。下面在一個2*2的figure裏面來比較這四個子圖(還有plot())。

[python] view plain copy
  1. def drawsemilogx():  
  2.     w=np.linspace(0.1,1000,1000)    
  3.     p=np.abs(1/(1+0.1j*w))    
  4.            
  5.     plt.subplot(221)    
  6.     plt.plot(w,p,lw=2)    
  7.     plt.xlabel('X')    
  8.     plt.ylabel('y');  
  9.           
  10.     plt.subplot(222)    
  11.     plt.semilogx(w,p,lw=2)    
  12.     plt.ylim(0,1.5)    
  13.     plt.xlabel('log(X)')    
  14.     plt.ylabel('y')    
  15.           
  16.     plt.subplot(223)    
  17.     plt.semilogy(w,p,lw=2)    
  18.     plt.ylim(0,1.5)    
  19.     plt.xlabel('x')    
  20.     plt.xlabel('log(y)')    
  21.           
  22.     plt.subplot(224)    
  23.     plt.loglog(w,p,lw=2)    
  24.     plt.ylim(0,1.5)    
  25.     plt.xlabel('log(x)')    
  26.     plt.xlabel('log(y)')    
  27.     plt.show()   

如上面的代碼所示,對一個低通濾波器函數繪圖。得到四個不同座標尺度的圖像。如下圖所示:


2,極座標圖像
    極座標系中的點由一個夾角和一段相對於中心位置的距離來表示。其實在plot()函數裏面本來就有一個polar的屬性,讓他爲True就行了。下面繪製一個極座標圖像:

[python] view plain copy
  1. def drawEightFlower():  
  2.       
  3.     theta=np.arange(0,2*np.pi,0.02)    
  4.     plt.subplot(121,polar=True)    
  5.     plt.plot(theta,2*np.ones_like(theta),lw=2)    
  6.     plt.plot(theta,theta/6,'--',lw=2)    
  7.            
  8.     plt.subplot(122,polar=True)    
  9.     plt.plot(theta,np.cos(5*theta),'--',lw=2)    
  10.     plt.plot(theta,2*np.cos(4*theta),lw=2)    
  11.     plt.rgrids(np.arange(0.5,2,0.5),angle=45)    
  12.     plt.thetagrids([0,45,90]);  
  13.       
  14.     plt.show();    

整個代碼很好理解,在後面的13,14行沒見過。第一個plt.rgrids(np.arange(0.5,2,0.5),angle=45) 表示繪製半徑爲0.5 1.0 1.5的三個同心圓,同時將這些半徑的值標記在45度位置的那個直徑上面。plt.thetagrids([0,45,90]) 表示的是在theta爲0,45,90度的位置上標記上度數。得到的圖像是:



3,柱狀圖:

核心代碼matplotlib.pyplot.bar(leftheightwidth=0.8bottom=Nonehold=None**kwargs)裏面重要的參數是左邊起點,高度,寬度。下面例子:

[python] view plain copy
  1. def drawPillar():     
  2.     n_groups = 5;       
  3.     means_men = (2035303527)    
  4.     means_women = (2532342025)    
  5.          
  6.     fig, ax = plt.subplots()    
  7.     index = np.arange(n_groups)    
  8.     bar_width = 0.35    
  9.          
  10.     opacity = 0.4    
  11.     rects1 = plt.bar(index, means_men, bar_width,alpha=opacity, color='b',label=    'Men')    
  12.     rects2 = plt.bar(index + bar_width, means_women, bar_width,alpha=opacity,color='r',label='Women')    
  13.          
  14.     plt.xlabel('Group')    
  15.     plt.ylabel('Scores')    
  16.     plt.title('Scores by group and gender')    
  17.     plt.xticks(index + bar_width, ('A''B''C''D''E'))    
  18.     plt.ylim(0,40);    
  19.     plt.legend();    
  20.       
  21.     plt.tight_layout();   
  22.     plt.show();    


得到的圖像是:



再貼一圖:

這是我關於pose識別率的實驗結果,感覺結果真是令人不可思議!(非博主原文!)

[python] view plain copy
  1. def drawBarChartPoseRatio():  
  2.     n_groups = 5      
  3.     means_VotexF36 = (0.844720496894410.9724770642201831.00.96551724137931040.970970970970971)    
  4.     means_VotexF50 = (1.0,              0.9929929929929931.00.99923488905891360.9717125382262997)  
  5.     means_VFH36    = (0.708538587848930.5697310819262040.89029003783102150.86386386386386380.5803008248423096)  
  6.     means_VFH50    = (0.907869481765830.7961225766103810.84751203852327450.88737623762376240.5803008248423096)    
  7.       
  8.     fig, ax = plt.subplots()    
  9.     index = np.arange(n_groups)    
  10.     bar_width = 0.3    
  11.     opacity   = 0.4    
  12.       
  13.     rects1 = plt.bar(index,             means_VFH36,    bar_width/2, alpha=opacity, color='r', label='VFH36'   )    
  14.     rects2 = plt.bar(index+ bar_width/2,  means_VFH50,  bar_width/2, alpha=opacity, color='g', label='VFH50'   )    
  15.      
  16.     rects3 = plt.bar(index+bar_width, means_VotexF36,     bar_width/2, alpha=opacity, color='c', label='VotexF36')    
  17.     rects4 = plt.bar(index+1.5*bar_width, means_VotexF50, bar_width/2, alpha=opacity, color='m', label='VotexF50')    
  18.       
  19.     plt.xlabel('Category')    
  20.     plt.ylabel('Scores')    
  21.     plt.title('Scores by group and Category')    
  22.       
  23.     #plt.xticks(index - 0.2+ 2*bar_width, ('balde', 'bunny', 'dragon', 'happy', 'pillow'))  
  24.     plt.xticks(index - 0.22*bar_width, ('balde''bunny''dragon''happy''pillow'),fontsize =18)  
  25.   
  26.     plt.yticks(fontsize =18)  #change the num axis size  
  27.   
  28.     plt.ylim(0,1.5)  #The ceil  
  29.     plt.legend()    
  30.     plt.tight_layout()    
  31.     plt.show()  

柱狀圖顯示:



4:散列圖,由離散的點構成的。

函數是:

matplotlib.pyplot.scatter(xys=20c='b'marker='o'cmap=Nonenorm=Nonevmin=Nonevmax=Nonealpha=Nonelinewidths=Noneverts=Nonehold=None,**kwargs),其中,xy是點的座標,s點的大小,maker是形狀可以maker=(5,1)5表示形狀是5邊型,1表示是星型(0表示多邊形,2放射型,3圓形);alpha表示透明度;facecolor=‘none’表示不填充。例子如下:

[python] view plain copy
  1. def drawStar():  
  2.     plt.figure(figsize=(8,4))    
  3.     x=np.random.random(100)    
  4.     y=np.random.random(100)    
  5.     plt.scatter(x,y,s=x*1000,c='y',marker=(5,1),alpha=0.5,lw=2,facecolors='none')    
  6.     plt.xlim(0,1)    
  7.     plt.ylim(0,1)    
  8.          
  9.     plt.show()   


上面代碼的facecolors參數使得前面的c=‘y’不起作用了。圖像:



5,3D圖像,主要是調用3D圖像庫。看下面的例子:

[python] view plain copy
  1. def draw3Dgrid():  
  2.       
  3.     x,y=np.mgrid[-2:2:20j,-2:2:20j]    
  4.     z=x*np.exp(-x**2-y**2)     
  5.     ax=plt.subplot(111,projection='3d')    
  6.     ax.plot_surface(x,y,z,rstride=2,cstride=1,cmap=plt.cm.coolwarm,alpha=0.8)    
  7.     ax.set_xlabel('x')    
  8.     ax.set_ylabel('y')    
  9.     ax.set_zlabel('z')    
  10.          
  11.     plt.show()  

得到的圖像如下圖所示:



到此,matplotlib基本操作的學習結束了,相信大家也可以基本完成自己的科研任務了。下面將繼續學習python的相關課程,請繼續關注。

參考書目:

《python科學計算》

《matplotlib手冊》


原文鏈接:http://blog.csdn.net/ikerpeng/article/details/20523679

參考資料:http://matplotlib.org/gallery.html   matplotlib畫廊

有少量修改,如有疑問,請訪問原作者!


轉自:http://blog.csdn.net/ikerpeng/article/details/20523679

發佈了53 篇原創文章 · 獲贊 55 · 訪問量 26萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章