使用极座标系统
有别于常见的直角座标标系统 ( 笛卡儿座标、xy 座标 ),matplotlib 也可将座标系统设置为“极座标系统”,设置为极座标后,图表也会采用对应的显示方式显示,这篇教学将会介绍如何使用极座标系统。
快速导览
本篇使用的 Python 版本为 3.7.12,所有范例可使用 Google Colab 实作,不用安装任何软件 ( 参考:使用 Google Colab )
import matplotlib
要进行本篇的范例,必须先加载 matplotlib 函数库的 pyplot 模组,范例将其独立命名为 plt。
import matplotlib.pyplot as plt
什么是极座标系统?
极座标系统 ( Polar coordinate system ) 是一个二维座标系统。座标系统中的任意位置,均可由一个夹角和一段相对原点的距离来表示。极座标的应用领域十分广泛,包括数学、物理、工程、航海、航空、电脑以及机器人领域。
极座标系统中的角度通常表示为角度或者弧度,使用公式 2π rad= 360° 进行转换,至于要使用哪一种单位,基本都是由使用场合而定。航海方面经常使用角度来进行测量,而物理学的某些领域则更倾向使用弧度。
显示极座标系统
如果使用 fig = plt.figure() 的方式建立图表,只要额外设定 projection='polar' 或 polar=True,就能将座标系统设定为极座标系统。
import matplotlib.pyplot as plt
x = range(5)
y = [7,8,5,12,6]
fig = plt.figure()
plt.subplot(projection='polar') # 設定 projection='polar'
# plt.subplot(polar=True) # 設定 polar=True 也可以
plt.bar(x,y)
plt.show()
这种做法支援不同的子图表,采用不同的座标系统,下方的程序码执行后,左边的图表使用极座标,右边的图表使用直角坐标。
import matplotlib.pyplot as plt
x = range(0,360,72)
y1 = [7,8,5,12,6]
y2 = [12,8,7,11,4]
fig = plt.figure()
fig.set_size_inches(10,6)
plt.subplot(121,projection='polar') # 設定 projection='polar'
# plt.subplot(121,polar=True) # 設定 polar=True 也可以
plt.bar(x,y1)
plt.subplot(122)
plt.bar(x,y2)
plt.show()
如果是使用 fig, ax = plt.subpolts() 的方式建立图表,则需要透过 subplot_kw 参数将 projection 提供给每个座标轴。
import matplotlib.pyplot as plt
x = range(5)
y1 = [7,8,5,12,6]
y2 = [12,8,7,11,4]
fig, ax = plt.subplots(1,2,subplot_kw={'projection': 'polar'}) # 設定 subplot_kw 參數
fig.set_size_inches(10,6)
ax[0].bar(x,y1)
ax[1].bar(x,y2)
plt.show()
极座标使用的单位
极座标系统使用“弧度”作为单位,因此显示数值时,需要将“角度”转换成“弧度”,下方的程序码透过 Python 标准函数库 math 里的 radians 方法,进行角度弧度的转换。
import matplotlib.pyplot as plt
import math
x = [math.radians(i) for i in range(0,360,72)] # 轉換成弧度後變成串列,每 72 度為一單位
y = [7,8,5,12,6]
fig = plt.figure()
fig.set_size_inches(6,6)
plt.subplot(projection='polar')
plt.bar(x,y)
plt.show()
极座标的显示设定
下方列出设定座标轴 ax 的常用方法,透过这些方法修改极座标系统的格线、最大值、最小值...等显示设定:
| 方法 | 参数 | 说明 |
|---|---|---|
| set_thetagrids | angles, labels | 设定放射状隔线,间隔单位角度,angles 和 labels 使用 tuple 格式。 |
| set_rgrids | list | 设定圆周隔线,使用串列格式。 |
| set_theta_direction | direction | 旋转方向,默认 1 为逆时针,设定 -1 为顺时针。 |
| set_theta_zero_location | loc | 设定 0 度的位置,默认 E ( 东边、右边 ),可设定 N、NW、W、SW、S、SE、E、NE。 |
| set_rlim | min, max | 数值范围。 |
| set_thetalim | min, max | 极座标显示范围,直接使用 ( min, max ) 单位是弧度,使用 (thetamin=min, thetamax=max) 单位则是角度。 |
| set_thetamin | min | 极座标显示范围的最小值,单位角度。 |
| set_thetamax | max | 极座标显示范围的最大值,单位角度。 |
import matplotlib.pyplot as plt
import math
x = [math.radians(i) for i in range(0,360,72)] # 角度轉成弧度
y = [7,8,5,12,6]
fig = plt.figure()
fig.set_size_inches(14,8)
ax1 = plt.subplot(131,projection='polar') # 套用預設值的圖表
ax1.set_title('ax1',pad=15,fontsize=20)
ax1.bar(x,y)
ax2 = plt.subplot(132,projection='polar') # 修改旋轉方向的圖表
ax2.set_title('ax2',pad=15,fontsize=20)
ax2.set_theta_direction(-1) # 改成順時針
ax2.bar(x,y)
ax3 = plt.subplot(133,projection='polar') # 調整顯示樣式的圖表
ax3.set_title('ax3',pad=15,fontsize=20)
ax3.bar(x,y)
ax3.set_rlim(0,15) # 設定數值顯示範圍
ax3.set_theta_zero_location('N') # 設定 0 度的位置
ax3.set_thetamin(0) # 設定顯示最小角度
ax3.set_thetamax(270) # 設定顯示最大角度
ax3.set_thetagrids(range(0,360,72)) # 設定放射狀隔線
ax3.set_rgrids(range(0,20,5)) # 設定圓周隔線
plt.show()
微信扫码关注
抖音扫码关注