运行结果效果图:
![](https://i-blog.csdnimg.cn/blog_migrate/2422cdbb0d144b53d57951f6ed194c2c.png)
代码及详细注释如下:
import numpy as np
import matplotlib.pyplot as plt
# from matplotlib.font_manager import FontProperties
plt.style.use('ggplot')
#能力标签
ability_size=6
ability_label=['Attack', 'Defense', 'Dribbling', 'Speed', 'Stamina', 'Shooting','Attack']
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)
}
# print(np.random.randint(size=ability_size,low=60,high=99),'\n')
#生成角度
theta=np.linspace(0,2*np.pi,6,endpoint=False)
# print(theta)
#是指0到2pi平均分为6份 endpoint=False是指不包含2pi
theta=np.append(theta,theta[0])
# 是指添加theta[0]至列表theta的末尾
# print(theta)
player['M']=np.append(player['M'],player['M'][0])
# print(player['M'],'\n')
# print(player['M'][0],'\n')
# 绘图梅西
ax1.plot(theta,player['M'],'r')
#填充颜色
ax1.fill(theta,player['M'],'r',alpha=0.3)
#去掉多余的x轴的度数标
ax1.set_xticks(theta)
ax1.set_yticks([20,40,60,80,100])
#将对应的x轴的度数换成文字(即对应的能力)
ax1.set_xticklabels(ability_label,size=8,y=0.01)
# #由于python无法识别文字
ax1.set_title('meixi',color='r',size=20)
# 绘图哈维
player['H']=np.append(player['H'],player['H'][0])
ax2.plot(theta,player['H'],'g')
ax2.fill(theta,player['H'],'g',alpha=0.3)
ax2.set_xticks(theta)
ax2.set_yticks([20,40,60,80,100])
ax2.set_xticklabels(ability_label,size=8,y=0.01)
ax2.set_title('hawei',color='g',size=20)
# 绘图切赫
player['Q']=np.append(player['Q'],player['Q'][0])
ax3.plot(theta,player['Q'],'y')
ax3.fill(theta,player['Q'],'y',alpha=0.3)
ax3.set_xticks(theta)
ax3.set_yticks([20,40,60,80,100])
ax3.set_xticklabels(ability_label,size=8,y=0.01)
ax3.set_title('qiehe',color='y',size=20,y=1)
# 绘图皮克
player['P']=np.append(player['P'],player['P'][0])
ax4.plot(theta,player['P'],'b')
ax4.fill(theta,player['P'],'b',alpha=0.3)
ax4.set_xticks(theta)
ax4.set_yticks([20,40,60,80,100])
ax4.set_xticklabels(ability_label,size=8,y=0.01)
ax4.set_title('pike',color='b',size=20,y=1)
plt.show()