失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 数据可视化—绘制雷达图

数据可视化—绘制雷达图

时间:2021-07-31 18:35:19

相关推荐

数据可视化—绘制雷达图

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

案例1

# 球员能力图import numpy as npimport matplotlib.pyplot as plt# 专门管理字体的类from matplotlib.font_manager import FontPropertiesplt.style.use('ggplot')# 定义字体font = FontProperties(fname=r'c:\windows\fonts\simsun.ttc',size=10)# 能力标签ability_size = 6ability_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.ndarraycluster_center = pd.concat([cluster_center, cluster_center[['ZL']]], axis=1)centers = np.array(cluster_center)# 分割圆周长,并让其闭合n = len(labels)# endpoint=False表示一定没有stopangle = 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()

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

如果觉得《数据可视化—绘制雷达图》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。