Python—雷达图

2020-12-21  本文已影响0人  灬鱼飞
import matplotlib.pyplot as plt
import numpy as np

import matplotlib.patches as mpathes


#构造数据
title = ""
values = [1,    1,  0.927358663,    0.787577119,    0.505513384,    0,  0,  0]
MaxVal = max(values)
feature = ["Tree species composition", "Age structure","Diameter","Height","Degree of slant","Preservation rate","Stem form","Stand density"]


#设置雷达图的角度,用于平分切开一个平面
N = len(values)
angles = np.linspace(0,2*np.pi,N,endpoint=False)
angle = 2*np.pi/N

#极坐标
ax= plt.subplot(111, projection='polar')

# 中文和负号的正常显示
plt.rcParams['font.sans-serif'] = 'Times New Roman'
plt.rcParams['axes.unicode_minus'] = False
#使用ggplot的风格绘图
plt.style.use('ggplot')
# plt.title("ω1",y=-0.13)
plt.grid(c='black')

#极坐标正方向
ax.set_theta_direction(-1)
#极坐标0°位置
ax.set_theta_zero_location('N')
#极坐标极径标签位置
ax.set_rlabel_position(0)
# 方法用于设置极坐标角度网格线显示
ax.set_thetagrids(angles=angles*180/np.pi,labels=[])
# ax.tick_params( 'x', pad = 20 )
#极坐标极径网格线显示
ax.set_rgrids(radii=[0.25,0.5,1],labels=[],angle=0, fmt='%.2f')


#画背景
plt.bar(x=[0,0,0],height=[1,0.5,0.25],width=10,color=['#CECCCC','#ffffff','#CECCCC'],alpha=1)

#画数据-符合最大值
for i in range(N):
    if (i == N-1):  
        break
    val = values[i]
    next_val = values[i+1]
    if(val==1 and next_val==1):
        plt.bar(x=angles[i],height=MaxVal,width=angle,color='#E17D7D',alpha=0.8,align="edge")
    else:
        break
#画数据-非最大值
values_new=values[i:]   
angles_new=angles[i:]    
ax.plot(angles_new, values_new, 'o-', linewidth=1, marker='',color='black')

values_new = np.concatenate((values_new,[0,values[0]]))
angles_new = np.concatenate((angles_new,[0,angles_new[0]]))
ax.fill(angles_new, values_new, facecolor='#E17D7D', alpha=0.8)


#标签
label_val = [1.12,1.05,1.05,1.07,1.12,1.45,1.35,1.36]
label_ang = angles.copy()
label_ang[0] = -angle*1/3-angle/10
label_ang[2] = angles[2]+angle/30
label_ang[4] = angles[4]+angle/4
label_ang[5] = angles[5]+angle/4
label_ang[6] = angles[6]-angle/30
label_ang[7] = angles[7]-angle/4
print(label_ang)
for i in range(N):
    plt.text(label_ang[i], label_val[i], feature[i], fontsize=10)

#极径label
plt.text(-angle*2/3, 0.3, "0.25", fontsize=10)
plt.text(-angle*1/3, 0.55, "0.50", fontsize=10)
plt.text(-angle*1/5, 0.92, "1.00", fontsize=10)

ax.set_ylim(0,1)
plt.savefig('./img/img1.jpg', dpi=600)
plt.show()
img1.jpg
上一篇下一篇

猜你喜欢

热点阅读