极坐标是个比较复制的东西,下面是实现的效果
没有设置随机数种子,所以以下代码在你的电脑上可能生成的和我的图不是一模一样的。
下面的代码是一个实现极坐标绘制的代码,
import numpy as np
import matplotlib.pyplot as plt
N=20
theta=np.linspace(0.0,2*np.pi,N,endpoint=False)
radii=10*np.random.rand(N)
width=np.pi/4*np.random.rand(N)
ax=plt.subplot(111,projection = 'polar')
bars=ax.bar(theta,radii,width=width,bottom=1.0)
for r,bar in zip(radii,bars):
bar.set_facecolor(plt.cm.viridis(r/10.))
bar.set_alpha(0.5)
plt.show()
注解
theta=np.linspace(0.0,2*np.pi,N,endpoint=False)
上面这行代码表示从0.0度到2
π
\pi
</span><span class="katex-html"><span class="base"><span class="strut" style="height: 0.43056em; vertical-align: 0em;"></span><span class="mord mathdefault" style="margin-right: 0.03588em;">π</span></span></span></span></span>(即360度)均分为N 份</p>
width=np.pi/4*np.random.rand(N)
π
\pi
</span><span class="katex-html"><span class="base"><span class="strut" style="height: 0.43056em; vertical-align: 0em;"></span><span class="mord mathdefault" style="margin-right: 0.03588em;">π</span></span></span></span></span>/4生成的数,形成密度值</p>
ax=plt.subplot(111,projection = 'polar')
111表示1,1,1将区域分为一行一列取第一个子区域作图
projection表示画的是极坐标图
这里ax是个对象
bars=ax.bar(theta,radii,width=width,bottom=1.0)
这行代码可以改改里面的参数,一改就知道哪里变动了
bar(left,height,width,bottom)
left为绘图时候从哪里开始
heigh为中心点向边缘绘制的长度
weight每个绘制区域角度辐射的范围
bottom表示离原点的距离
最后的for循环是添加颜色