使用matplotlib绘制下图:
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
# 创建子图
ax1 = plt.subplot(221, projection='polar')# 221 表示 2行2列第一个子图, polar表示极坐标
ax2 = plt.subplot(222, projection='polar')
ax3 = plt.subplot(223, projection='polar')
ax4 = plt.subplot(224, projection='polar')
# 球员能力标签
ability_size = 6
ability_lable = ['进攻', '防守', '速度', '射术', '盘带', '体力']
# 随机生成球员能力数据
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),
}
# 角度设置
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])
player['H'] = np.append(player['H'], player['H'][0])
player['P'] = np.append(player['P'], player['P'][0])
player['Q'] = np.append(player['Q'], player['Q'][0])
# 绘制
# 梅西
ax1.plot(theta, player['M'], 'r')
ax1.fill(theta, player['M'], 'r', alpha=0.3)
ax1.set_xticks(theta) # 数值指向定义的角度
ax1.set_xticklabels(ability_lable) # 重新定义标签
ax1.set_title('梅西', color='r', size = 20)
# 哈维
ax2.plot(theta, player['H'], 'g')
ax2.fill(theta, player['H'], 'g', alpha=0.3)
ax2.set_xticks(theta) # 数值指向定义的角度
ax2.set_xticklabels(ability_lable) # 重新定义标签
ax2.set_title('哈维', color='g', size = 20)
# 皮克
ax3.plot(theta, player['P'], 'b')
ax3.fill(theta, player['P'], 'b', alpha=0.3)
ax3.set_xticks(theta) # 数值指向定义的角度
ax3.set_xticklabels(ability_lable) # 重新定义标签
ax3.set_title('皮克', color='b', size = 20)
# 切赫
ax4.plot(theta, player['Q'], 'y')
ax4.fill(theta, player['Q'], 'y', alpha=0.3)
ax4.set_xticks(theta) # 数值指向定义的角度
ax4.set_xticklabels(ability_lable) # 重新定义标签
ax4.set_title('切赫', color='y', size = 20)
plt.show()