看到在Tableau上畫雷達圖挺麻煩的,需要導入背景圖片;matplotlib也沒有直接畫雷達圖的函數,需要藉助數學知識一步步實現。其他軟件就不清楚了,但是pyecharts裏有對應的函數Radar,生成的圖片簡介美觀而且互動性強。
參考博客:
RGB顏色對應表:https://tool.oschina.net/commons?type=3
雷達圖:雷達圖
import pandas as pd
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
excelFile = r'C:/Users/admin/Desktop/論文/球員數據.xls'
df=pd.DataFrame(pd.read_excel(excelFile))
df.head()
姓名 | 傳球 | 射門 | 身體 | 防守 | 速度 | 盤帶 | |
---|---|---|---|---|---|---|---|
0 | C·羅納爾多 | 83 | 92 | 87 | 49 | 89 | 86 |
1 | 梅西 | 88 | 95 | 66 | 43 | 86 | 96 |
2 | 蘇亞雷斯 | 80 | 92 | 87 | 58 | 78 | 81 |
3 | 萊萬多夫斯基 | 79 | 92 | 84 | 53 | 81 | 87 |
4 | 格列茲曼 | 84 | 87 | 70 | 54 | 86 | 89 |
from pyecharts import options as opts #用以設置
from pyecharts.charts import Radar #導入雷達類
下面的數據如果用list(df.iloc[0])[1:]表示v1的話,則一直顯示不出來,挺坑的。所以還是貼具體數字。
from matplotlib import pyplot as plt
v1=[[83, 92, 87, 49, 89, 86]] # 數據必須爲二維數組,否則會集中一個指示器顯示
v2=[[88, 95, 66, 43, 86, 96]]
v3=[[80, 92, 87, 58, 78, 81]]
radar1=(
Radar()
.add_schema(# 添加schema架構
schema=[
opts.RadarIndicatorItem(name='傳球',max_=100),# 設置指示器名稱和最大值
opts.RadarIndicatorItem(name='射門',max_=100),
opts.RadarIndicatorItem(name='身體',max_=100),
opts.RadarIndicatorItem(name='防守',max_=100),
opts.RadarIndicatorItem(name='速度',max_=100),
opts.RadarIndicatorItem(name='盤帶',max_=100),
]
)
.add(df["姓名"][0],v1,color="#f9713c") # 添加一條數據,參數1爲數據名,參數2爲數據,參數3爲顏色
.add(df["姓名"][1],v2,color="#4169E1")
.add(df["姓名"][2],v3,color="#00BFFF")
.set_global_opts(title_opts=opts.TitleOpts(title='雷達圖'),)
)
radar1.render('1.html')
在html裏面可以單獨看某一個球員的數據,也可以同時在一個圖中看所有球員的數據。