1. 程式人生 > >python科學計算學習二:matplotlib繪圖(極座標 3D繪圖等)(3)

python科學計算學習二:matplotlib繪圖(極座標 3D繪圖等)(3)

    部落格地址:http://blog.csdn.net/ikerpeng/article/details/20523679

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

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

    1,對數座標圖

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

  1 import numpy as np
  2 import matplotlib.pyplot as plt
  3 w=np.linspace(0.1,1000,1000)
  4 p=np.abs(1/(1+0.1j*w))
  5 
  6 plt.subplot(221)
  7 plt.plot(w,p,lw=2)
  8 plt.xlabel('X')
  9 plt.ylabel('y')
 10 
 11 
 12 plt.subplot(222)
 13 plt.semilogx(w,p,lw=2)
 14 plt.ylim(0,1.5)
 15 plt.xlabel('log(X)')
 16 plt.ylabel('y')
 17 
 18 plt.subplot(223)
 19 plt.semilogy(w,p,lw=2)
 20 plt.ylim(0,1.5)
 21 plt.xlabel('x')
 22 plt.xlabel('log(y)')
 23 
 24 plt.subplot(224)
 25 plt.loglog(w,p,lw=2)
 26 plt.ylim(0,1.5)
 27 plt.xlabel('log(x)')
 28 plt.xlabel('log(y)')
 29 plt.show()
如上面的程式碼所示,對一個低通濾波器函式繪圖。得到四個不同座標尺度的影象。如下圖所示:

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

  1 import numpy as np
  2 import matplotlib.pyplot as plt
  3 
  4 theta=np.arange(0,2*np.pi,0.02)
  5 
  6 plt.subplot(121,polar=True)
  7 plt.plot(theta,2*np.ones_like(theta),lw=2)
  8 plt.plot(theta,theta/6,'--',lw=2)
  9 
 10 plt.subplot(122,polar=True)
 11 plt.plot(theta,np.cos(5*theta),'--',lw=2)
 12 plt.plot(theta,2*np.cos(4*theta),lw=2)
 13 plt.rgrids(np.arange(0.5,2,0.5),angle=45)
 14 plt.thetagrids([0,45,90])
 15 
 16 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)裡面重要的引數是左邊起點,高度,寬度。下面例子:

  1 import numpy as np
  2 import matplotlib.pyplot as plt
  3 
  4 
  5 n_groups = 5
  6 
  7 means_men = (20, 35, 30, 35, 27)
  8 means_women = (25, 32, 34, 20, 25)
  9 
 10 fig, ax = plt.subplots()
 11 index = np.arange(n_groups)
 12 bar_width = 0.35
 13 
 14 opacity = 0.4
 15 rects1 = plt.bar(index, means_men, bar_width,alpha=opacity, color='b',label=    'Men')
 16 rects2 = plt.bar(index + bar_width, means_women, bar_width,alpha=opacity,col    or='r',label='Women')
 17 
 18 plt.xlabel('Group')
 19 plt.ylabel('Scores')
 20 plt.title('Scores by group and gender')
 21 plt.xticks(index + bar_width, ('A', 'B', 'C', 'D', 'E'))
 22 plt.ylim(0,40)
 23 plt.legend()
 24 
 25 plt.tight_layout()
 26 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’表示不填充。例子如下:

  1 import numpy as np
  2 import matplotlib.pyplot as plt
  3 
  4 plt.figure(figsize=(8,4))
  5 x=np.random.random(100)
  6 y=np.random.random(100)
  7 plt.scatter(x,y,s=x*1000,c='y',marker=(5,1),alpha=0.5,lw=2,facecolors='none')
  8 plt.xlim(0,1)
  9 plt.ylim(0,1)
 10 
 11 plt.show()
上面程式碼的facecolors引數使得前面的c=‘y’不起作用了。影象:



5,3D影象,主要是呼叫3D影象庫。看下面的例子:

  1 import numpy as np
  2 import matplotlib.pyplot as plt
  3 import mpl_toolkits.mplot3d
  4 
  5 x,y=np.mgrid[-2:2:20j,-2:2:20j]
  6 z=x*np.exp(-x**2-y**2)
  7 
  8 ax=plt.subplot(111,projection='3d')
  9 ax.plot_surface(x,y,z,rstride=2,cstride=1,cmap=plt.cm.coolwarm,alpha=0.8)
 10 ax.set_xlabel('x')
 11 ax.set_ylabel('y')
 12 ax.set_zlabel('z')
 13 
 14 plt.show()

得到的影象如下圖所示:


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

參考書目:

《python科學計算》

《matplotlib手冊》