数据可视化—绘制雷达图

  这里以案例的形式绘制雷达图

案例1

# 球员能力图
import numpy as np
import matplotlib.pyplot as plt
# 专门管理字体的类
from matplotlib.font_manager import FontProperties

plt.style.use('ggplot')
# 定义字体
font = FontProperties(fname=r'c:\windows\fonts\simsun.ttc',size=10)
# 能力标签
ability_size = 6
ability_label = ['进攻','防守','盘带','速度','体力','射术']
# 生成4个子图
ax1 = plt.subplot(221,projection='polar')
ax2 = plt.subplot(222,projection='polar')
ax3 = plt.subplot(223,projection='polar')
ax4 = plt.subplot(224,projection='polar')

# 随机生成球员能力
player = {
    
    
    'M':np.random.randint(size=ability_size,low=60,high=99),
    'H':np.random.randint(size=ability_size,low=60,high=99),
    'P':np.random.randint(size=ability_size,low=60,high=99),
    'Q':np.random.randint(size=ability_size,low=60,high=99),
}
# 角度值,首尾相接共7个
theta = np.linspace(0,2*np.pi,6,endpoint=False)
theta = np.append(theta,theta[0])
player['M'] = np.append(player['M'],player['M'][0])
# 画图
ax1.plot(theta,player['M'],'r')
# 填充颜色
ax1.fill(theta,player['M'],'r',alpha=0.3)
# 重新生成极坐标 与能力对应
ax1.set_xticks(theta)
# 设置标签,此时,字体并没有正确的显示出来,matplotlib不知道用什么字体读取,需要显式的设置
ax1.set_xticklabels(ability_label,y=0.05,fontproperties=font)
# 添加标签
ax1.set_title('梅西',fontproperties=font,color='r',size=15)

player['H'] = np.append(player['H'],player['H'][0])
ax2.plot(theta,player['H'],'r')
ax2.fill(theta,player['H'],'r',alpha=0.3)
ax2.set_xticks(theta)
ax2.set_xticklabels(ability_label,y=0.05,fontproperties=font)
ax2.set_title('哈维',fontproperties=font,color='r',size=15)

player['P'] = np.append(player['P'],player['P'][0])
ax3.plot(theta,player['P'],'r')
ax3.fill(theta,player['P'],'r',alpha=0.3)
ax3.set_xticks(theta)
ax3.set_xticklabels(ability_label,y=0.05,fontproperties=font)
ax3.set_title('皮克',fontproperties=font,color='r',size=15)

player['Q'] = np.append(player['Q'],player['Q'][0])
ax4.plot(theta,player['Q'],'r')
ax4.fill(theta,player['Q'],'r',alpha=0.3)
ax4.set_xticks(theta)
ax4.set_xticklabels(ability_label,y=0.05,fontproperties=font)
ax4.set_title('切赫',fontproperties=font,color='r',size=15)

plt.show()

在这里插入图片描述

案例2

前面步骤可参考数据挖掘实战—航空公司客户价值分析

import matplotlib.pyplot as plt 
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号 

# 客户分群雷达图
labels = ['ZL','ZR','ZF','ZM','ZC']
legen = ['客户群' + str(i + 1) for i in cluster_center.index]  # 客户群命名,作为雷达图的图例
lstype = ['-','--',(0, (3, 5, 1, 5, 1, 5)),':','-.']
kinds = list(cluster_center.index)
# 由于雷达图要保证数据闭合,因此再添加L列,并转换为 np.ndarray
cluster_center = pd.concat([cluster_center, cluster_center[['ZL']]], axis=1)
centers = np.array(cluster_center)

# 分割圆周长,并让其闭合
n = len(labels)
# endpoint=False表示一定没有stop
angle = np.linspace(0, 2 * np.pi, n, endpoint=False)
angle = np.concatenate((angle, [angle[0]]))
# 绘图
fig = plt.figure(figsize = (8,6))
# 以极坐标的形式绘制图形
ax = fig.add_subplot(111, polar=True)  
# 画线
for i in range(len(kinds)):
    ax.plot(angle, centers[i], linestyle=lstype[i], linewidth=2,label=kinds[i])
# 添加属性标签
ax.set_thetagrids(angle * 180 / np.pi, labels)
plt.title('客户特征分析雷达图')
plt.legend(legen)
plt.show()

在这里插入图片描述


如果对您有帮助,麻烦点赞关注,这真的对我很重要!!!如果需要互关,请评论留言或私信!
在这里插入图片描述


猜你喜欢

转载自blog.csdn.net/weixin_46649052/article/details/115468835