用matplotlib畫FIFA球星雷達圖,自定義背景圖片

最近想對足球運動員進行數據分析,用雷達圖展示各個球員的不同能力,然後背景搞一張球員的帥圖。找了好多教程才搞定,記錄一下下。
數據來自 kaggle上的FIFA2019

import numpy as np 
import pandas as pd 
import matplotlib as  mpl
import matplotlib.pyplot as  plt 
from PIL import  Image
import seaborn as sns
import warnings
df=pd.read_csv('data.csv')

數據非常非常非常多,而且特別詳細,附上一張部分數據釋意圖在這裏插入圖片描述
我們選取部分數據,重新組合,提取出’進攻’, ‘技巧’, ‘移動’, ‘力量’, ‘心理’, ‘防守’, '守門’這幾個新特徵(不要問我爲什麼,我也是百度的)

# 進攻
df['進攻'] = (df['Crossing'] + df['Finishing'] + df['HeadingAccuracy'] + df['ShortPassing'] + df['Volleys']) / 5
# 技巧
df['技巧'] = (df['Dribbling'] + df['Curve'] + df['FKAccuracy'] + df['LongPassing'] + df['BallControl']) / 5
# 移動
df['移動'] = (df['Acceleration'] + df['SprintSpeed'] + df['Agility'] + df['Reactions'] + df['Balance']) / 5
# 力量
df['力量'] = (df['ShotPower'] + df['Jumping'] + df['Stamina'] + df['Strength'] + df['LongShots']) / 5
# 心理
df['心理'] = (df['Aggression'] + df['Interceptions'] + df['Positioning'] + df['Vision'] + df['Penalties'] + df['Composure']) / 6
# 防守
df['防守'] = (df['Marking'] + df['StandingTackle'] + df['SlidingTackle']) / 3
# 守門
df['守門'] = (df['GKDiving'] + df['GKHandling'] + df['GKKicking'] + df['GKPositioning'] + df['GKReflexes']) / 5

因爲要畫球員的雷達圖,所以只取名字和上面提取好的新特徵

person= df[['Name','進攻', '技巧', '移動', '力量', '心理', '防守', '守門']]
person.head()

在這裏插入圖片描述
接下來就開始畫圖了,先畫一個人的試試

labels = np.array(['進攻', '技巧', '移動', '力量', '心理', '防守', '守門'])#行標籤
#數據個數
dataLenth = len(labels)
for i in range(1):#這裏懶得寫不是循環的了,大概就是這麼個意思吧
    data =np.array(person.loc[i][1:])
    angles = np.linspace(0, 2*np.pi, dataLenth, endpoint=False)
    data = np.concatenate((data, [data[0]])) # 閉合
    angles = np.concatenate((angles, [angles[0]])) # 閉合
    
    fig = plt.figure()
    ax = fig.add_subplot(111, polar=True)# polar參數!!
    ax.plot(angles, data, 'bo-', linewidth=2)# 畫線
    ax.fill(angles, data, facecolor='b', alpha=0.25)# 填充
    ax.set_thetagrids(angles * 180/np.pi, labels, fontproperties="SimHei")
    #ax.set_title(person['Name'].loc[i]+"雷達圖", va='bottom', fontproperties="SimHei")
    ax.set_rlim(0,100)
    ax.grid(True)
    plt.show()
    names=person['Name'].loc[i]+'.jpg'
    fig.savefig(names,dpi=100,box_inches='tight') 

在這裏插入圖片描述
具體細節可以根據需要或者喜好自行修改哈(我沒改因爲我不會,纔不是懶)

然後就到了激動人心的加背景環節了,我之前想的是在figure裏設置,但是怎麼搞都搞不明白。後來我想了個笨辦法,就是先把雷達圖畫好;然後找一張背景圖,按照雷達圖的大小調整尺寸;把兩張圖疊加在一起不就行了!

img1 = Image.open('LMessi.jpg')#背景圖
img2 = Image.open('L. Messi.jpg')#雷達圖
img1 = img1.convert('RGBA')
img2 = img2.convert('RGBA')
if not img1.size==img2.size:
    img1 = img1.resize((img2.size),Image.ANTIALIAS)#高質量
img = Image.blend(img1, img2,0.77)#數字表示透明度
img.save('LDTL. Messi.png')
img

背景圖
完成
這裏背景圖找的尺寸不太好,不小心給梅天王瘦臉了一下哈哈哈
最後放一張FIFA2019前9球星的雷達圖在這裏插入圖片描述
看起來大多數球員還是來自巴薩皇馬啊,大部分上榜的都是進攻型球員,只有一名門將擠進了前10~
對了,再來一張梅西跟C羅的對比圖吧

labels = np.array(['進攻', '技巧', '移動', '力量', '心理', '防守', '守門'])
kinds = list(person.iloc[:, 0].head(2))
result = pd.concat([person, person[['進攻']]], axis=1)
centers = np.array(result.iloc[:, 1:])
n = len(labels)
angle = np.linspace(0, 2 * np.pi, n, endpoint=False)
angle = np.concatenate((angle, [angle[0]]))

fig = plt.figure()
ax = fig.add_subplot(111, polar=True)  
for i in range(len(kinds)):
    ax.plot(angle, centers[i], linewidth=2, label=kinds[i])
        # ax.fill(angle, centers[i])  # 填充底色

# 添加屬性標籤
ax.set_thetagrids(angle * 180 / np.pi, labels)
#plt.title('different kind')
plt.legend(loc='lower right')
plt.show()
fig.savefig('mac.jpg',dpi=100,box_inches='tight') 
img1 = Image.open('cam.jpg')
img2 = Image.open('mac.jpg')
img1 = img1.convert('RGBA')
img2 = img2.convert('RGBA')
if not img1.size==img2.size:
    img1 = img1.resize((img2.size),Image.ANTIALIAS)
img = Image.blend(img1, img2,0.77)
img.save('LDTmc.png')
img

在這裏插入圖片描述
超有愛有木有!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章