matplotlib極坐標方法詳解


一、極坐標

在平面內取一個定點O,叫極點,引一條射線Ox,叫做極軸,再選定一個長度單位和角度的正方向(通常取逆時針方向)。對於平面內任何一點M,用ρ表示線段OM的長度(有時也用r表示),θ表示從Ox到OM的角度,ρ叫做點M的極徑,θ叫做點M的極角,有序數對 (ρ,θ)就叫點M的極坐標,這樣建立的坐標系叫做極坐標系。通常情況下,M的極徑坐標單位為1(長度單位),極角坐標單位為rad(或°)
image

二、matplotlib繪制極坐標圖

1.創建極坐標圖

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()

極坐標系

2.極坐標圖設置

dir()命令可以得到一個對象的所有方法屬性,通過比較ax1ax2的方法屬性便可知道極坐標有哪些設置方法。

>>> 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']

2.1 極坐標正方向

set_theta_direction方法用於設置極坐標的正方向

  • 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()

image

2.2 極坐標0°位置

set_theta_zero_location方法用於設置極坐標0°位置

  • 0°可設置在八個位置,分別為N, NW, W, SW, S, SE, E, NE
  • set_theta_zero_location的參數值為'N','NW','W','SW','S','SE','E','NE'時,0°分別對應的位置為方位N, NW, W, SW, S, SE, E, NE;
  • 默認情況下0°位於E方位
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()

image

2.3極坐標角度網格線顯示

set_thetagrids方法用於設置極坐標角度網格線顯示

  • 參數為所要顯示網格線的角度值列表
  • 默認顯示0°、45°、90°、135°、180°、225°、270°、315°的網格線
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()

image

2.4極坐標角度偏離

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()

image

2.5極坐標極徑網格線顯示

set_rgrids方法用於設置極徑網格線顯示

  • 參數值為所要顯示網格線的極徑值列表,最小值不能小於等於0
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()

image

2.6極坐標極徑標簽位置

set_rlabel_position方法用於設置極徑標簽顯示位置

  • 參數為標簽所要顯示在的角度
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()

image

2.7極坐標極徑范圍

set_rlim方法用於設置顯示的極徑范圍

  • 參數為極徑最小值,最大值
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()

image

2.8極坐標極徑最大值

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()

image

2.9極坐標極徑最小值

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()

image

2.10 極徑對數坐標

set_rscale方法用於設置極徑對數坐標

  • 參數值為'linear','log','symlog'
  • 默認值為'linear'
  • 該方法要在繪制完圖像后使用才有效
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()

image

2.11 極坐標極徑網格線顯示范圍

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()

image


想觀看Matplotlib教學視頻,了解更多Matplotlib實用技巧可關注

微信公眾賬號: MatplotlibClass

今日頭條號:Matplotlib小講堂


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM