在平面內取一個定點O,叫極點,引一條射線Ox,叫作極軸,再選定一個長度單位和角度的正方向(一般取逆時針方向)。對於平面內任何一點M,用ρ表示線段OM的長度(有時也用r表示),θ表示從Ox到OM的角度,ρ叫作點M的極徑,θ叫作點M的極角,有序數對 (ρ,θ)就叫點M的極座標,這樣創建的座標系叫作極座標系。一般狀況下,M的極徑座標單位爲1(長度單位),極角座標單位爲rad(或°)
python
matplotlib的pyplot子庫提供了繪製極座標圖的方法,在調用subplot()建立子圖時經過設置projection='polar',即可建立一個極座標子圖,而後調用plot()在極座標子圖中繪圖。
下面就建立一個極座標子圖和一個直角座標子圖進行對比。微信
import matplotlib.pyplot as plt ax1 = plt.subplot(121, projection='polar') ax2 = plt.subplot(122) ax1.plot(theta,theta/6,'--',lw=2) ax2.plot(theta,theta/6,'--',lw=2) plt.show()
dir()
命令能夠獲得一個對象的全部方法屬性,經過比較ax1
與ax2
的方法屬性即可知道極座標有哪些設置方法。3d
>>> print(sorted(set(dir(ax1))-set(dir(ax2)))) ['InvertedPolarTransform', 'PolarAffine', 'PolarTransform', 'RadialLocator', 'ThetaFormatter', '_default_rlabel_position', '_default_theta_direction', '_default_theta_offset', '_direction', '_r_label_position', '_theta_label1_position', '_theta_label2_position', '_theta_offset', '_xaxis_text1_transform', '_xaxis_text2_transform', '_yaxis_text_transform', 'get_rlabel_position', 'get_rmax', 'get_rmin', 'get_theta_direction', 'get_theta_offset', 'resolution', 'set_rgrids', 'set_rlabel_position', 'set_rlim', 'set_rmax', 'set_rmin', 'set_rscale', 'set_rticks', 'set_theta_direction', 'set_theta_offset', 'set_theta_zero_location', 'set_thetagrids', 'transProjection', 'transProjectionAffine', 'transPureProjection']
set_theta_direction
方法用於設置極座標的正方向code
set_theta_direction
的參數值爲1,'counterclockwise'或者是'anticlockwise'的時候,正方向爲逆時針;set_theta_direction
的參數值爲-1或者是'clockwise'的時候,正方向爲順時針;import matplotlib.pyplot as plt import numpy as np theta=np.arange(0,2*np.pi,0.02) ax1= plt.subplot(121, projection='polar') ax2= plt.subplot(122, projection='polar') ax2.set_theta_direction(-1) ax1.plot(theta,theta/6,'--',lw=2) ax2.plot(theta,theta/6,'--',lw=2) plt.show()
set_theta_zero_location
方法用於設置極座標0°位置orm
set_theta_zero_location
的參數值爲'N','NW','W','SW','S','SE','E','NE'時,0°分別對應的位置爲方位N, NW, W, SW, S, SE, E, NE;import matplotlib.pyplot as plt import numpy as np theta=np.arange(0,2*np.pi,0.02) ax1= plt.subplot(121, projection='polar') ax2= plt.subplot(122, projection='polar') ax2.set_theta_zero_location('N') ax1.plot(theta,theta/6,'--',lw=2) ax2.plot(theta,theta/6,'--',lw=2) plt.show()
set_thetagrids
方法用於設置極座標角度網格線顯示視頻
import matplotlib.pyplot as plt import numpy as np theta=np.arange(0,2*np.pi,0.02) ax1= plt.subplot(121, projection='polar') ax2= plt.subplot(122, projection='polar') ax2.set_thetagrids(np.arange(0.0, 360.0, 30.0)) ax1.plot(theta,theta/6,'--',lw=2) ax2.plot(theta,theta/6,'--',lw=2) plt.show()
set_theta_offset
方法用於設置角度偏離對象
import matplotlib.pyplot as plt import numpy as np theta=np.arange(0,2*np.pi,0.02) ax1= plt.subplot(121, projection='polar') ax2= plt.subplot(122, projection='polar') ax2.set_theta_offset(np.pi) ax1.plot(theta,theta/6,'--',lw=2) ax2.plot(theta,theta/6,'--',lw=2) plt.show()
set_rgrids
方法用於設置極徑網格線顯示blog
import matplotlib.pyplot as plt import numpy as np theta=np.arange(0,2*np.pi,0.02) ax1= plt.subplot(121, projection='polar') ax2= plt.subplot(122, projection='polar') ax2.set_rgrids(np.arange(0.2,1.0,0.4)) ax1.plot(theta,theta/6,'--',lw=2) ax2.plot(theta,theta/6,'--',lw=2) plt.show()
set_rlabel_position
方法用於設置極徑標籤顯示位置get
import matplotlib.pyplot as plt import numpy as np theta=np.arange(0,2*np.pi,0.02) ax1= plt.subplot(121, projection='polar') ax2= plt.subplot(122, projection='polar') ax2.set_rlabel_position('90') ax1.plot(theta,theta/6,'--',lw=2) ax2.plot(theta,theta/6,'--',lw=2) plt.show()
set_rlim
方法用於設置顯示的極徑範圍it
import matplotlib.pyplot as plt import numpy as np theta=np.arange(0,2*np.pi,0.02) ax1= plt.subplot(121, projection='polar') ax2= plt.subplot(122, projection='polar') ax2.set_rlim(0.6,1.2) ax1.plot(theta,theta/6,'--',lw=2) ax2.plot(theta,theta/6,'--',lw=2) plt.show()
set_rmax
方法用於設置顯示的極徑最大值
import matplotlib.pyplot as plt import numpy as np theta=np.arange(0,2*np.pi,0.02) ax1= plt.subplot(121, projection='polar') ax2= plt.subplot(122, projection='polar') ax1.plot(theta,theta/6,'--',lw=2) ax2.plot(theta,theta/6,'--',lw=2) ax2.set_rmax(0.6) plt.show()
set_rmin
方法用於設置顯示的極徑最小值
import matplotlib.pyplot as plt import numpy as np theta=np.arange(0,2*np.pi,0.02) ax1= plt.subplot(121, projection='polar') ax2= plt.subplot(122, projection='polar') ax1.plot(theta,theta/6,'--',lw=2) ax2.plot(theta,theta/6,'--',lw=2) ax2.set_rmin(0.6) plt.show()
set_rscale
方法用於設置極徑對數座標
import matplotlib.pyplot as plt import numpy as np theta=np.arange(0,2*np.pi,0.02) ax1= plt.subplot(121, projection='polar') ax2= plt.subplot(122, projection='polar') ax1.plot(theta,theta/6,'--',lw=2) ax2.plot(theta,theta/6,'--',lw=2) ax2.set_rlim(math.pow(10,-1),math.pow(10,0)) ax1.set_rscale('linear') ax2.set_rscale('symlog') plt.show()
set_rticks
方法用於設置極徑網格線的顯示範圍
import matplotlib.pyplot as plt import numpy as np theta=np.arange(0,2*np.pi,0.02) ax1= plt.subplot(121, projection='polar') ax2= plt.subplot(122, projection='polar') ax2.set_rticks(np.arange(0.1, 0.9, 0.2)) ax1.plot(theta,theta/6,'--',lw=2) ax2.plot(theta,theta/6,'--',lw=2) plt.show()
想觀看Matplotlib教學視頻,瞭解更多Matplotlib實用技巧可關注
微信公衆帳號: MatplotlibClass
今日頭條號:Matplotlib小講堂