十一、极坐标图

 

 

In [ ]:
'''
极坐标图

调用subplot()建立子图时经过设置projection='polar',即可建立一个极坐标子图,而后调用plot()在极坐标子图中绘图

'''
In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [9]:
# 建立极坐标轴

s = pd.Series(np.arange(20))
theta=np.arange(0,2*np.pi,0.02)
print(s.head())
print(theta[:10])
# 建立数据

fig = plt.figure(figsize=(8,4))
ax1 = plt.subplot(121, projection = 'polar')
ax2 = plt.subplot(122)
# 建立极坐标子图
# 还能够写:ax = fig.add_subplot(111,polar=True)

ax1.plot(theta,theta*3,linestyle = '--',lw=1)  
ax1.plot(s, linestyle = '--', marker = '.',lw=2)
# 折现--->曲线 ,点要多
ax2.plot(theta,theta*3,linestyle = '--',lw=1)
ax2.plot(s)
print(len(theta))#315
print(len(s))# 30

plt.grid()
# 建立极坐标图,参数1为角度(弧度制),参数2为value
# lw → 线宽
 
0    0
1    1
2    2
3    3
4    4
dtype: int32
[0.   0.02 0.04 0.06 0.08 0.1  0.12 0.14 0.16 0.18]
315
20
 
In [13]:
# 极坐标参数设置

theta=np.arange(0,2*np.pi,0.02)
plt.figure(figsize=(8,4))

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)


# 建立极坐标子图ax

ax2.set_theta_direction(-1)
# set_theta_direction():坐标轴正方向,默认逆时针

ax2.set_thetagrids(np.arange(0.0, 360.0, 90),['a','b','c','d'])# 尽可能数量一致
ax2.set_rgrids(np.arange(0.2,2,0.4))
# set_thetagrids():设置极坐标角度网格线显示及标签 → 网格和标签数量一致
# set_rgrids():设置极径网格线显示,其中参数必须是正数

ax2.set_theta_offset(np.pi/2)
# set_theta_offset():设置角度偏移,逆时针,弧度制

ax2.set_rlim(0.2,1.2)
ax2.set_rmax(2)
ax2.set_rticks(np.arange(0.1, 1.5, 0.2))
# set_rlim():设置显示的极径范围
# set_rmax():设置显示的极径最大值
# set_rticks():设置极径网格线的显示范围
Out[13]:
[<matplotlib.projections.polar.RadialTick at 0x177416e6438>,
 <matplotlib.projections.polar.RadialTick at 0x177416e6ac8>,
 <matplotlib.projections.polar.RadialTick at 0x17741632320>,
 <matplotlib.projections.polar.RadialTick at 0x17741bda7f0>,
 <matplotlib.projections.polar.RadialTick at 0x17741bda518>,
 <matplotlib.projections.polar.RadialTick at 0x177416f8208>,
 <matplotlib.projections.polar.RadialTick at 0x17741a13320>]
 
In [17]:
# 雷达图1 - 极坐标的折线图/填图 - plt.plot()

plt.figure(figsize=(8,4))

ax1= plt.subplot(111, projection='polar')
ax1.set_title('radar map\n')  # 建立标题
ax1.set_rlim(0,12)

data1 = np.random.randint(1,10,10)
data3 = np.random.randint(1,10,10)
theta=np.arange(0,2*np.pi,2*np.pi/10)
# 建立数据
ax1.set_thetagrids(np.arange(0.0, 360.0, 90))# 标签;尽可能数量一致


ax1.plot(theta,data1,'.--',label='data1')
ax1.fill(theta,data1,alpha=0.2)

ax1.plot(theta,data3,'.--',label='data3')
ax1.fill(theta,data3,alpha=0.2)
# 绘制雷达线
Out[17]:
[<matplotlib.patches.Polygon at 0x17741065400>]
 
In [18]:
# 雷达图2 - 极坐标的折线图/填图 - plt.polar()
# 首尾闭合

labels = np.array(['a','b','c','d','e','f']) # 标签
dataLenth = 6 # 数据长度
data1 = np.random.randint(0,10,6) 
data2 = np.random.randint(0,10,6) # 数据

angles = np.linspace(0, 2*np.pi, dataLenth, endpoint=False) # 分割圆周长
data1 = np.concatenate((data1, [data1[0]])) # 闭合, 把首元素,往末尾添加一份。使其闭合
angles = np.concatenate((angles, [angles[0]])) # 闭合


plt.polar(angles, data1, 'o-', linewidth=1) #作极坐标系
plt.fill(angles, data1, alpha=0.25)# 填充


plt.thetagrids(angles * 180/np.pi, labels) # 设置网格、标签
plt.ylim(0,10)  # polar的极值设置为ylim
Out[18]:
(0, 10)
 
In [25]:
# 极轴图 - 极坐标的柱状图 # 横轴变成一个点,宽度变成角度,起始角度按x的值

plt.figure(figsize=(8,4))

ax1= plt.subplot(111, projection='polar')
ax1.set_title('radar map\n')  # 建立标题
ax1.set_rlim(0,12)

data = np.random.randint(1,10,10)
theta=np.arange(0,2*np.pi,2*np.pi/10)
# 建立数据

bar = ax1.bar(theta,data,alpha=0.5)      # theta ,data 数量要一致
print(bar)
for r,bar in zip(data, bar):
    bar.set_facecolor(plt.cm.jet(r/10.))  # 设置颜色
plt.thetagrids(np.arange(0.0, 360.0, 90), []) # 设置网格、标签(这里是空标签,则不显示内容)ax2.set_thetagrids
 
<BarContainer object of 10 artists>
Out[25]:
(<a list of 8 Line2D thetagridline objects>,
 <a list of 4 Text thetagridlabel objects>)
 
In [ ]:
 
In [ ]:
相关文章
相关标签/搜索