最近想对足球运动员进行数据分析,用雷达图展示各个球员的不同能力,然后背景搞一张球员的帅图。找了好多教程才搞定,记录一下下。
数据来自 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
超有爱有木有!!!