Python:Matplotlib 画曲线和柱状图(Code)
原文鏈接:http://blog.csdn.net/ikerpeng/article/details/20523679
參考資料:http://matplotlib.org/gallery.html ? matplotlib畫廊
有少量修改,如有疑問(wèn),請(qǐng)?jiān)L問(wèn)原作者!
首先補(bǔ)充一下:兩種體系7種顏色 r g b y m c k (紅,綠,藍(lán),黃,品紅,青,黑)
?
在科研的過(guò)程中,坐標(biāo)系中的XY不一定就是等尺度的。例如在聲波中對(duì)Y軸取對(duì)數(shù)。肆意我們也必須知道這種坐標(biāo)系如何畫出來(lái)的。
?
?1:對(duì)數(shù)坐標(biāo)圖
? ? 有3個(gè)函數(shù)可以實(shí)現(xiàn)這種功能,分別是:semilogx(),semilogy(),loglog()。它們分別表示對(duì)X軸,Y軸,XY軸取對(duì)數(shù)。下面在一個(gè)2*2的figure里面來(lái)比較這四個(gè)子圖(還有plot())。
?
def drawsemilogx():w=np.linspace(0.1,1000,1000) p=np.abs(1/(1+0.1j*w)) plt.subplot(221) plt.plot(w,p,lw=2) plt.xlabel('X') plt.ylabel('y');plt.subplot(222) plt.semilogx(w,p,lw=2) plt.ylim(0,1.5) plt.xlabel('log(X)') plt.ylabel('y') plt.subplot(223) plt.semilogy(w,p,lw=2) plt.ylim(0,1.5) plt.xlabel('x') plt.xlabel('log(y)') plt.subplot(224) plt.loglog(w,p,lw=2) plt.ylim(0,1.5) plt.xlabel('log(x)') plt.xlabel('log(y)') plt.show()?
?
如上面的代碼所示,對(duì)一個(gè)低通濾波器函數(shù)繪圖。得到四個(gè)不同坐標(biāo)尺度的圖像。如下圖所示:
?
2,極坐標(biāo)圖像
? ? 極坐標(biāo)系中的點(diǎn)由一個(gè)夾角和一段相對(duì)于中心位置的距離來(lái)表示。其實(shí)在plot()函數(shù)里面本來(lái)就有一個(gè)polar的屬性,讓他為True就行了。下面繪制一個(gè)極坐標(biāo)圖像:
?
def drawEightFlower():theta=np.arange(0,2*np.pi,0.02) plt.subplot(121,polar=True) plt.plot(theta,2*np.ones_like(theta),lw=2) plt.plot(theta,theta/6,'--',lw=2) plt.subplot(122,polar=True) plt.plot(theta,np.cos(5*theta),'--',lw=2) plt.plot(theta,2*np.cos(4*theta),lw=2) plt.rgrids(np.arange(0.5,2,0.5),angle=45) plt.thetagrids([0,45,90]);plt.show();?
?
?
?
整個(gè)代碼很好理解,在后面的13,14行沒(méi)見過(guò)。第一個(gè)plt.rgrids(np.arange(0.5,2,0.5),angle=45) 表示繪制半徑為0.5 1.0 1.5的三個(gè)同心圓,同時(shí)將這些半徑的值標(biāo)記在45度位置的那個(gè)直徑上面。plt.thetagrids([0,45,90]) 表示的是在theta為0,45,90度的位置上標(biāo)記上度數(shù)。得到的圖像是:
?
3,柱狀圖:
核心代碼matplotlib.pyplot.bar(left,?height,?width=0.8,?bottom=None,?hold=None,?**kwargs)里面重要的參數(shù)是左邊起點(diǎn),高度,寬度。下面例子:
?
def drawPillar(): n_groups = 5; means_men = (20, 35, 30, 35, 27) means_women = (25, 32, 34, 20, 25) fig, ax = plt.subplots() index = np.arange(n_groups) bar_width = 0.35 opacity = 0.4 rects1 = plt.bar(index, means_men, bar_width,alpha=opacity, color='b',label= 'Men') rects2 = plt.bar(index + bar_width, means_women, bar_width,alpha=opacity,color='r',label='Women') plt.xlabel('Group') plt.ylabel('Scores') plt.title('Scores by group and gender') plt.xticks(index + bar_width, ('A', 'B', 'C', 'D', 'E')) plt.ylim(0,40); plt.legend(); plt.tight_layout(); plt.show();?
?
?
?
?
?
得到的圖像是:
?
再貼一圖:
這是我關(guān)于pose識(shí)別率的實(shí)驗(yàn)結(jié)果,感覺(jué)結(jié)果真是令人不可思議!(非博主原文!)
def drawBarChartPoseRatio():n_groups = 5 means_VotexF36 = (0.84472049689441, 0.972477064220183, 1.0, 0.9655172413793104, 0.970970970970971) means_VotexF50 = (1.0, 0.992992992992993, 1.0, 0.9992348890589136, 0.9717125382262997)means_VFH36 = (0.70853858784893, 0.569731081926204, 0.8902900378310215, 0.8638638638638638, 0.5803008248423096)means_VFH50 = (0.90786948176583, 0.796122576610381, 0.8475120385232745, 0.8873762376237624, 0.5803008248423096) fig, ax = plt.subplots() index = np.arange(n_groups) bar_width = 0.3 opacity = 0.4 rects1 = plt.bar(index, means_VFH36, bar_width/2, alpha=opacity, color='r', label='VFH36' ) rects2 = plt.bar(index+ bar_width/2, means_VFH50, bar_width/2, alpha=opacity, color='g', label='VFH50' ) rects3 = plt.bar(index+bar_width, means_VotexF36, bar_width/2, alpha=opacity, color='c', label='VotexF36') rects4 = plt.bar(index+1.5*bar_width, means_VotexF50, bar_width/2, alpha=opacity, color='m', label='VotexF50') plt.xlabel('Category') plt.ylabel('Scores') plt.title('Scores by group and Category') #plt.xticks(index - 0.2+ 2*bar_width, ('balde', 'bunny', 'dragon', 'happy', 'pillow'))plt.xticks(index - 0.2+ 2*bar_width, ('balde', 'bunny', 'dragon', 'happy', 'pillow'),fontsize =18)plt.yticks(fontsize =18) #change the num axis sizeplt.ylim(0,1.5) #The ceilplt.legend() plt.tight_layout() plt.show()柱狀圖顯示:
?
4:散列圖,由離散的點(diǎn)構(gòu)成的。
函數(shù)是:
matplotlib.pyplot.scatter(x,?y,?s=20,?c='b',?marker='o',?cmap=None,?norm=None,?vmin=None,?vmax=None,?alpha=None,?linewidths=None,?verts=None,?hold=None,**kwargs),其中,xy是點(diǎn)的坐標(biāo),s點(diǎn)的大小,maker是形狀可以maker=(5,1)5表示形狀是5邊型,1表示是星型(0表示多邊形,2放射型,3圓形);alpha表示透明度;facecolor=‘none’表示不填充。例子如下:
?
def drawStar():plt.figure(figsize=(8,4)) x=np.random.random(100) y=np.random.random(100) plt.scatter(x,y,s=x*1000,c='y',marker=(5,1),alpha=0.5,lw=2,facecolors='none') plt.xlim(0,1) plt.ylim(0,1) plt.show()?
?
?
?
?
上面代碼的facecolors參數(shù)使得前面的c=‘y’不起作用了。圖像:
?
5,3D圖像,主要是調(diào)用3D圖像庫(kù)。看下面的例子:
?
def draw3Dgrid():x,y=np.mgrid[-2:2:20j,-2:2:20j] z=x*np.exp(-x**2-y**2) ax=plt.subplot(111,projection='3d') ax.plot_surface(x,y,z,rstride=2,cstride=1,cmap=plt.cm.coolwarm,alpha=0.8) ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') plt.show()?
?
得到的圖像如下圖所示:
?
?
到此,matplotlib基本操作的學(xué)習(xí)結(jié)束了,相信大家也可以基本完成自己的科研任務(wù)了。下面將繼續(xù)學(xué)習(xí)python的相關(guān)課程,請(qǐng)繼續(xù)關(guān)注。
參考書目:
《python科學(xué)計(jì)算》
《matplotlib手冊(cè)》
?
總結(jié)
以上是生活随笔為你收集整理的Python:Matplotlib 画曲线和柱状图(Code)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 简单好听的英文个性签名最新176个
- 下一篇: angular6父子组件之间的引入报错