在使用matplotlib.pyplot的subplot()函数创建图形区域时,可以设定参数projection='polar',或者 polar=True,就可以在极坐标下绘制图形。
代码语言:javascript复制import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"] = ["SimHei"]
pi = np.pi
theta=np.arange(0,2*pi,0.02)#极角,弧度制
r1 = theta /6.0 #半径
r2 = 1*(1 -np.sin(theta))
r3 = 3 np.cos(7* theta)
e = 0.5
p= 1.0
r4 = e*p / (1 - e*np.cos(theta))
ax1= plt.subplot(221, projection='polar')#使用极坐标
ax1.plot(theta, r1, "g-")#螺旋线
ax2= plt.subplot(222, polar =True)
ax2.plot(theta, r2,'r-',lw=2)#心形曲线
#ax2.set_rlim(0.6,1.2) # 可设定半径范围
#ax2.set_thetalim(pi, 2*pi) # 可设定极角范围
ax3= plt.subplot(223, polar =True)#使用极坐标
ax3.plot(theta, r3,'b-')
ax4= plt.subplot(224, polar =True)#使用极坐标
ax4.plot(theta, r4,'m-') #椭圆
plt.tight_layout()
plt.suptitle("使用极坐标绘图",fontsize =14, color ='b')
plt.show()
也可以在极坐标系下绘制散点图:
代码语言:javascript复制import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"] = ["SimHei"]
pi = np.pi
N = 80
theta= 2*pi* np.random.rand(N)
r = 30*pi* np.random.rand(N)
marker_size = 3*r
colors = np.random.rand(N)
ax= plt.subplot(111, polar =True)
ax.scatter(theta, r, s = marker_size, c = colors, cmap ="jet", marker ="*")
ax.set_title("极坐标下散点图",color ='b')
plt.show()