1.导入数据库
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
2.导入数据
path=r'path'
data=pd.read_excel(path,sheet_name='雷达图',index_col=0)
data
展示数据:
290m | 312m | |
---|---|---|
0° | 62.6 | 54.5 |
45° | 61.6 | 54.6 |
90° | 63.0 | 54.5 |
135° | 60.6 | 53.9 |
180° | 63.2 | 54.8 |
225° | 60.6 | 53.9 |
270° | 63.4 | 54.5 |
315° | 61.6 | 54.6 |
360° | 62.6 | 54.5 |
3.图纸设置
plt.rcParams['savefig.dpi'] = 300 # 图片像素
plt.rcParams['figure.dpi'] = 120 # 分辨率
plt.rcParams['font.sans-serif']=['SimHei'] #显示中文
plt.rcParams['axes.unicode_minus']=False #显示负号
4.划分角度
n=len(data.index)
theta=np.linspace(0,2*np.pi,n,endpoint=True) #获取8个方向的角度值
R1=data['290m']/data['290m'].min()
R2=data['312m']/data['312m'].min()
5.构造平滑曲线函数
x_new=np.linspace(theta[0],theta[8],100)
f=interpolate.interp1d(theta,R1,kind='slinear')
y_smooth=f(x_new)
f1=interpolate.interp1d(theta,R2,kind='slinear')
y_smooth1=f1(x_new)
6.设置不同方向
labels=list(['0','45°','90°','135°','180°','225°','270°','315°'])
7.绘图
fig,ax=plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta,R1,'o',color='blue',markersize=8,fillstyle='none',label='290m')
ax.plot(theta,R2,'D',color='orange',markersize=6,fillstyle='none',label='312m')
ax.plot(x_new,y_smooth,color='blue')
ax.plot(x_new,y_smooth1,color='orange')
ax.set_rmin(0.95) #设置刻度范围最小值
ax.set_rmax(1.08) #设置刻度范围最大值
ax.set_rticks([]) #隐藏刻度标签
ax.set_xticklabels(labels,fontsize=8)
ax.set_theta_zero_location('N') #设置0度正北方向
ax.set_theta_direction(-1) #设置逆时针方向绘图
ax.legend(loc=(0.82,0.92),ncol=1,fontsize=8) # 添加图例
输出结果:
完整代码
#(1)导入库
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
#(2)导入数据
path=r'path'
data=pd.read_excel(path,sheet_name='雷达图',index_col=0)
#(3)图纸设置
plt.rcParams['savefig.dpi'] = 300 # 图片像素
plt.rcParams['figure.dpi'] = 120 # 分辨率
plt.rcParams['font.sans-serif']=['SimHei'] #显示中文
plt.rcParams['axes.unicode_minus']=False #显示负号
#(4)划分角度
n=len(data.index)
theta=np.linspace(0,2*np.pi,n,endpoint=True) #获取8个方向的角度值
R1=data['290m']/data['290m'].min()
R2=data['312m']/data['312m'].min()
#(5)构造平滑曲线函数
x_new=np.linspace(theta[0],theta[8],100)
f=interpolate.interp1d(theta,R1,kind='slinear')
y_smooth=f(x_new)
f1=interpolate.interp1d(theta,R2,kind='slinear')
y_smooth1=f1(x_new)
#(6)设置不同方向
labels=list(['0','45°','90°','135°','180°','225°','270°','315°'])
#(7)绘图
fig,ax=plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta,R1,'o',color='blue',markersize=8,fillstyle='none',label='290m')
ax.plot(theta,R2,'D',color='orange',markersize=6,fillstyle='none',label='312m')
ax.plot(x_new,y_smooth,color='blue')
ax.plot(x_new,y_smooth1,color='orange')
ax.set_rmin(0.95) #设置刻度范围最小值
ax.set_rmax(1.08) #设置刻度范围最大值
ax.set_rticks([]) #隐藏刻度标签
ax.set_xticklabels(labels,fontsize=8)
ax.set_theta_zero_location('N') #设置0度正北方向
ax.set_theta_direction(-1) #设置逆时针方向绘图
ax.legend(loc=(0.82,0.92),ncol=1,fontsize=8) # 添加图例