Matplotlib绘制漫威好汉战力争,带你飞起来!
进阶之路 | 奇妙的View之旅
媒介
我们寻常玩游戏或许看电影的时刻,会看到内里引见各种人的才能目标,以百度百科供应的为例,以下图
离别引见了各个超等好汉的智力、气力、速率、耐力、能量、能量发射、战役妙技。
然则吧,光是如许的表格,并不能很直观的看出好汉的才能,我们须要一个战役力散布图。
话不多说,先上制品:
希冀功用
传入战力目标、超等好汉才能数值,生成上面的战力散布图
才能目标 = ['智力', '气力', '速率', '耐力', '能量', '妙技']
超等好汉才能值 = {
'美国队长': [5, 4, 3, 4, 3, 7],
'钢铁侠': [6, 3, 5, 5, 3, 3],
'绿巨人': [6, 7, 3, 7, 1, 5],
'蜘蛛侠': [5, 4, 5, 4, 2, 5],
'灭霸': [7, 7, 7, 7, 7, 7],
'雷神': [2, 5, 6, 7, 6, 6],
'绯红女巫': [3, 3, 3, 3, 7, 3],
'黑寡妇': [5, 3, 2, 3, 3, 7],
'鹰眼': [5, 3, 3, 2, 2, 7],
}
生成战力争(才能目标,超等好汉才能值)
代码完成
talking is cheap,show you the code
由于涉及到显现中文,依靠字体,把代码上传到了github:
挪用以下,圆满的完成了需求,运用简朴粗犷,真可谓高端大气上档次
from tool import generate_ability_map
abilities = ['智力', '气力', '速率', '耐力', '能量', '妙技']
super_heros = {
'美国队长': [5, 4, 3, 4, 3, 7],
'钢铁侠': [6, 3, 5, 5, 3, 3],
'绿巨人': [6, 7, 3, 7, 1, 5],
'蜘蛛侠': [5, 4, 5, 4, 2, 5],
'灭霸': [7, 7, 7, 7, 7, 7],
'雷神': [2, 5, 6, 7, 6, 6],
'绯红女巫': [3, 3, 3, 3, 7, 3],
'黑寡妇': [5, 3, 2, 3, 3, 7],
'鹰眼': [5, 3, 3, 2, 2, 7],
}
generate_ability_map(abilities, super_heros)
中心代码以下
import numpy as np
import matplotlib.pyplot as plt
import math
import matplotlib.colors as mcolors
# 导入中文
import matplotlib.font_manager as font_manager
font_dirs = ['./font']
font_files = font_manager.findSystemFonts(fontpaths=font_dirs)
font_list = font_manager.createFontList(font_files)
font_manager.fontManager.ttflist.extend(font_list)
plt.rcParams['font.family'] = 'SimHei'
# 启用主题
plt.style.use('ggplot')
# 生成才能散布图
def generate_ability_map(abilities, data_list, rows=3):
# 依据才能项平分圆
angles = np.linspace(0, 2 * np.pi, len(abilities), endpoint=False)
angles = np.append(angles, angles[0])
# 生成n个子图
fg, axes = plt.subplots(math.ceil(len(data_list) / rows), rows, subplot_kw=dict(polar=True))
# 打散为一维数组
axes = axes.ravel()
# 猎取一切支撑的色彩
colors = list(mcolors.TABLEAU_COLORS)
# 轮回绘制
i = 0
for name, data in data_list.items():
data = np.append(np.array(data), data[0])
ax = axes[i]
# 绘制线条
ax.plot(angles, data, color=colors[i])
# 添补色彩
ax.fill(angles, data, alpha=0.7, color=colors[i])
# 设置角度
ax.set_xticks(angles)
# 设置坐标轴称号
ax.set_xticklabels(abilities)
# 设置称号
ax.set_title(name, size=10, color='black', position=(0.5, 0.4))
i = i + 1
plt.show()
下面是对代码的诠释
一、导入matplotlib依靠包
import numpy as np
import matplotlib.pyplot as plt
import math
import matplotlib.colors as mcolors
关于matplotlib的运用,能够看
二、支撑显现中文
# 导入中文
import matplotlib.font_manager as font_manager
font_dirs = ['./font']
font_files = font_manager.findSystemFonts(fontpaths=font_dirs)
font_list = font_manager.createFontList(font_files)
font_manager.fontManager.ttflist.extend(font_list)
plt.rcParams['font.family'] = 'SimHei'
关于假如显现中文,能够检察
三、运用ggplot主题
plt.style.use('ggplot')
matplot内置了许多主题,ggplot这个文雅,就是它了!
下面就是对生成终究图generate_ability_map函数的步骤剖析。
四、依据才能项平分圆
angles = np.linspace(0, 2 * np.pi, len(abilities), endpoint=False)
angles = np.append(angles, angles[0])
五、生成n个子图
# row默以为3,代表一行散布3个图,也能够指定参数自定义
fg, axes = plt.subplots(math.ceil(len(data_list) / rows), rows, subplot_kw=dict(polar=True))
axes = axes.ravel()
默许生成的是二维矩阵,我们须要挪用ravel转换为一维,便于遍历
六、猎取支撑的色彩
# 猎取一切支撑的色彩
colors = list(mcolors.TABLEAU_COLORS)
假如不指定色彩,就不能发生这么悦目的图了
关于色彩的运用能够检察:
六、绘制一切子图
# 轮回绘制
i = 0
for name, data in data_list.items():
data = np.append(np.array(data), data[0])
ax = axes[i]
# 绘制线条
ax.plot(angles, data, color=colors[i])
# 添补色彩
ax.fill(angles, data, alpha=0.7, color=colors[i])
# 设置角度
ax.set_xticks(angles)
# 设置坐标轴称号
ax.set_xticklabels(abilities)
# 设置称号
ax.set_title(name, size=10, color='black', position=(0.5, 0.4))
i = i + 1
plt.show()
依据数据列表,离别绘制线条,设置角度和数值,坐标轴称号等,终究奖一切的好汉显现出来,
axes对象有许多能够自定义显现的要领,概况能够检察:
更多示例
火影
显现火影人物目标,数据来源于:
挪用
abilities = ['忍', '体', '幻', '贤', '力', '速', '精', '印']
super_heros= {
'旗木卡卡西': [10, 9, 8, 10, 7, 9, 6, 10],
'自来也': [10, 9, 6, 9, 9, 9, 10, 9],
'纲手': [10, 10, 7, 10, 10, 7, 8, 8],
'宇智波鼬': [10, 9, 10, 10, 7, 10, 5, 10],
}
generate_ability_map(abilities,super_heros, 2)
显现
健身
健身一般来说就是看三大项了:深蹲卧推硬拉。
本人鄙人,深蹲140kg,卧推100kg,硬拉160kg,跟孙悟空和贝吉塔一定不能相对抗,哈哈,下边纯属文娱
abilities = ['深蹲','卧推','硬拉']
super_heros = {
'雪山飞猪': [140,100,160],
'孙悟空': [800,550,1000],
'贝吉塔': [750,500,950],
}
generate_ability_map(abilities,super_heros)
以下
Go1.14发布了,快来围观新的特性啦