Python matplotlib绘制柱状图

2021-02-26 16:02:44 浏览数 (1)

一、数据准备

本篇文章使用S10总决赛从8强开始各位置的数据。每一局数据的第一个列表都是胜方数据,第二个列表都是负方数据。

代码语言:javascript复制
# coding=utf-8
data = {
    "DWG-DRX1": [[(3, 2, 4), (2, 0, 4), (1, 0, 1), (3, 1, 4), (0, 0, 4)],
                 [(2, 3, 1), (0, 2, 1), (1, 0, 0), (0, 2, 1), (0, 2, 2)]],
    "DWG-DRX2": [[(1, 2, 8), (6, 1, 5), (2, 1, 8), (3, 1, 7), (0, 2, 7)],
                 [(3, 3, 1), (0, 2, 5), (1, 3, 4), (2, 2, 4), (1, 2, 4)]],
    "DWG-DRX3": [[(2, 2, 10), (7, 0, 6), (5, 0, 8), (3, 1, 6), (4, 4, 4)],
                 [(3, 4, 0), (2, 6, 2), (1, 3, 0), (1, 3, 3), (0, 5, 3)]],
    "SN-JDG1": [[(4, 2, 9), (3, 1, 9), (5, 1, 11), (7, 3, 10), (1, 6, 7)],
                [(3, 5, 8), (1, 5, 7), (2, 5, 7), (7, 2, 6), (0, 3, 10)]],
    "SN-JDG2": [[(7, 2, 12), (7, 2, 14), (2, 0, 16), (9, 0, 12), (1, 4, 13)],
                [(2, 6, 2), (2, 6, 4), (0, 4, 7), (4, 4, 1), (0, 6, 7)]],
    "SN-JDG3": [[(5, 1, 5), (5, 1, 9), (3, 1, 8), (3, 1, 7), (1, 3, 11)],
                [(0, 4, 2), (1, 2, 4), (0, 4, 3), (3, 1, 4), (3, 6, 3)]],
    "SN-JDG4": [[(2, 2, 4), (3, 2, 5), (1, 0, 10), (7, 1, 5), (0, 2, 12)],
                [(2, 3, 1), (2, 3, 3), (1, 3, 4), (0, 2, 6), (2, 2, 3)]],
    "TES-FNC1": [[(2, 3, 8), (4, 2, 6), (2, 0, 8), (6, 0, 8), (1, 0, 10)],
                 [(0, 3, 3), (1, 3, 3), (4, 0, 0), (0, 6, 2), (0, 3, 3)]],
    "TES-FNC2": [[(0, 2, 10), (8, 1, 4), (4, 0, 6), (4, 1, 5), (1, 2, 13)],
                 [(3, 2, 3), (1, 4, 5), (1, 2, 3), (0, 2, 6), (1, 7, 1)]],
    "TES-FNC3": [[(3, 1, 4), (3, 1, 9), (3, 1, 7), (7, 1, 2), (0, 2, 12)],
                 [(0, 4, 3), (2, 6, 4), (2, 3, 2), (2, 0, 4), (0, 3, 3)]],
    "TES-FNC4": [[(1, 2, 7), (10, 1, 7), (6, 2, 5), (0, 4, 16), (1, 4, 12)],
                 [(2, 3, 3), (3, 1, 5), (1, 4, 8), (4, 3, 5), (3, 7, 5)]],
    "TES-FNC5": [[(1, 2, 1), (4, 1, 6), (4, 0, 6), (4, 1, 5), (0, 1, 6)],
                 [(2, 2, 1), (2, 3, 1), (0, 4, 1), (0, 1, 2), (0, 3, 2)]],
    "G2-GEN1": [[(4, 0, 7), (2, 2, 11), (4, 1, 11), (6, 1, 6), (3, 0, 10)],
                [(0, 5, 2), (3, 4, 1), (1, 3, 2), (0, 4, 1), (0, 3, 2)]],
    "G2-GEN2": [[(3, 3, 14), (4, 3, 12), (11, 0, 11), (9, 2, 13), (1, 3, 15)],
                [(3, 8, 1), (2, 5, 3), (2, 6, 5), (4, 4, 2), (0, 5, 7)]],
    "G2-GEN3": [[(2, 5, 11), (7, 2, 10), (6, 3, 13), (7, 3, 11), (1, 1, 18)],
                [(4, 5, 8), (2, 6, 7), (5, 4, 6), (3, 2, 6), (0, 6, 7)]],
    "DWG-G21": [[(4, 0, 12), (7, 2, 9), (4, 2, 11), (6, 0, 9), (1, 2, 8)],
                [(1, 5, 1), (3, 5, 2), (2, 5, 3), (0, 2, 3), (0, 5, 4)]],
    "DWG-G22": [[(4, 2, 7), (5, 1, 9), (6, 2, 11), (7, 3, 9), (3, 1, 11)],
                [(0, 7, 1), (0, 4, 4), (4, 4, 2), (3, 4, 1), (1, 6, 2)]],
    "DWG-G23": [[(3, 1, 9), (6, 2, 5), (5, 2, 6), (8, 2, 7), (0, 3, 13)],
                [(1, 3, 3), (3, 3, 4), (1, 4, 3), (2, 3, 3), (3, 9, 4)]],
    "DWG-G24": [[(5, 0, 3), (2, 0, 7), (2, 0, 10), (2, 1, 3), (4, 1, 4)],
                [(0, 5, 1), (1, 3, 0), (0, 3, 1), (1, 2, 1), (0, 2, 1)]],
    "SN-TES1": [[(5, 1, 5), (3, 1, 6), (1, 0, 4), (2, 3, 3), (0, 2, 3)],
                [(2, 4, 0), (0, 1, 4), (1, 2, 2), (4, 2, 0), (0, 2, 4)]],
    "SN-TES2": [[(5, 1, 4), (1, 2, 5), (3, 1, 7), (3, 3, 4), (0, 0, 7)],
                [(2, 1, 2), (1, 3, 5), (2, 5, 4), (2, 2, 0), (0, 1, 5)]],
    "SN-TES3": [[(3, 0, 7), (2, 2, 4), (2, 1, 4), (5, 2, 4), (1, 2, 7)],
                [(0, 3, 3), (2, 3, 3), (3, 1, 1), (0, 4, 4), (2, 2, 2)]],
    "SN-TES4": [[(5, 2, 4), (1, 3, 16), (8, 1, 8), (6, 4, 9), (1, 8, 13)],
                [(1, 2, 10), (9, 5, 4), (1, 4, 9), (5, 6, 10), (2, 4, 12)]],
    "DWG-SN1": [[(2, 2, 11), (5, 3, 9), (8, 1, 11), (4, 2, 12), (2, 4, 7)],
                [(1, 5, 5), (5, 4, 4), (3, 3, 2), (2, 3, 3), (1, 6, 3)]],
    "DWG-SN2": [[(10, 1, 4), (2, 1, 10), (3, 3, 11), (3, 3, 10), (2, 4, 7)],
                [(0, 4, 8), (5, 4, 2), (5, 6, 2), (2, 3, 5), (0, 3, 9)]],
    "DWG-SN3": [[(3, 3, 10), (5, 2, 8), (3, 3, 3), (5, 1, 6), (0, 2, 8)],
                [(3, 6, 5), (1, 2, 2), (4, 3, 2), (2, 3, 3), (1, 2, 6)]],
    "DWG-SN4": [[(2, 0, 12), (8, 0, 7), (1, 3, 5), (9, 1, 5), (4, 3, 4)],
                [(2, 9, 1), (1, 5, 2), (2, 2, 0), (2, 4, 2), (0, 4, 3)]],
}

二、matplotlib绘制柱状图

代码语言:javascript复制
import matplotlib.pyplot as plt
from numpy import mean


location = ["上单", "打野", "中单", "下路", "辅助"]
up_kill = [[team[0][0] for team in value] for value in data.values()]
wild_kill = [[team[1][0] for team in value] for value in data.values()]
mid_kill = [[team[2][0] for team in value] for value in data.values()]
down_kill = [[team[3][0] for team in value] for value in data.values()]
aux_kill = [[team[4][0] for team in value] for value in data.values()]
# noinspection PyTypeChecker
avg_kill = [round(mean(kill), 2) for kill in [up_kill, wild_kill, mid_kill, down_kill, aux_kill]]
plt.figure(figsize=(10, 10), dpi=100)
plt.bar(location, avg_kill, width=0.5, color=['b', 'y', 'c', 'r', 'g'])
plt.xticks(fontsize=14)
plt.yticks(range(0, 6, 1), fontsize=14)
for a, b in zip(range(5), avg_kill):
    plt.text(a, b 0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
plt.grid(linestyle="--", alpha=0.5)
plt.xlabel("位置", fontsize=16)
plt.ylabel("场均击杀", fontsize=16, rotation=0)
plt.title("S10总决赛各位置场均击杀", fontsize=16)
plt.show()

运行结果:

bar(): matplotlib中绘制柱状图的函数。可以传入很多参数,一般传入三个参数,前两个参数传入柱状图的x值列表和y值列表,第三个传入关键字参数width='宽度',宽度一般使用0~1之间的数据。其他参数根据需要添加,如设置颜色的color参数。

mean(): numpy中用于计算平均值的函数,可以用于计算一个数组的平均值,数据可以是列表,也可以是嵌套结构的列表。

round(): Python内置函数,用于处理浮点数的小数位数。

zip(): Python内置函数,用于将可迭代对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。

text(): matplotlib中用于设置柱状图的数据标注的方法。前两个参数用于设置数据标注显示的位置,第三个参数用于设置数据标注显示的内容。其他参数根据需要添加,如ha参数用于设置标注的左右位置,'center', 'right', 'left',va参数用于设置标注的上下位置,'top', 'bottom', 'center', 'baseline', 'center_baseline'。

柱状图主要用于绘制离散的数据,通过柱状图的高度能够一眼看出各个数据的大小关系,比较数据之间的差别。

上面的柱状图中,绘制的是S10总决赛从8强到决赛的所有比赛中,各位置(包含胜方和负方)的平均击杀数据。通过柱状图,各位置的击杀数大小关系一目了然。

上面的代码中,前面很大一部分都是在解析数据,图形中只用到了数据中的击杀数据,展示了各位置的场均击杀数。接下来使用多组柱状图来展示各位置更全面的数据。

三、matplotlib绘制多组柱状图

代码语言:javascript复制
import matplotlib.pyplot as plt
from numpy import mean


location = ["上单", "打野", "中单", "下路", "辅助"]
loc_kill, loc_die, loc_assists = [[list() for _ in range(5)] for _ in range(3)]
for i in range(5):
    loc_kill[i] = [[team[i][0] for team in value] for value in data.values()]
    loc_die[i] = [[team[i][1] for team in value] for value in data.values()]
    loc_assists[i] = [[team[i][2] for team in value] for value in data.values()]
# noinspection PyTypeChecker
avg_kill = [round(mean(kill), 2) for kill in loc_kill]
# noinspection PyTypeChecker
avg_die = [round(mean(die), 2) for die in loc_die]
# noinspection PyTypeChecker
avg_assists = [round(mean(assists), 2) for assists in loc_assists]
plt.figure(figsize=(20, 10), dpi=100)
x = range(len(location))
plt.bar([i-0.2 for i in x], avg_kill, width=0.2, color='b')
plt.bar(x, avg_die, width=0.2, color='r')
plt.bar([i 0.2 for i in x], avg_assists, width=0.2, color='g')
for a, b in zip(x, avg_kill):
    plt.text(a-0.2, b 0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
for a, b in zip(x, avg_die):
    plt.text(a, b 0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
for a, b in zip(x, avg_assists):
    plt.text(a 0.2, b 0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
plt.xticks(x, location, fontsize=14)
plt.yticks(range(0, 9, 1), fontsize=14)
plt.grid(linestyle="--", alpha=0.5)
plt.legend(['击杀', '死亡', '助攻'], fontsize=16, markerscale=0.5)
plt.xlabel("位置", fontsize=18)
plt.ylabel("场均数据", fontsize=18, rotation=0)
plt.title("S10总决赛各位置场均数据", fontsize=18)
plt.show()

运行结果:

要在同一张图像中绘制多组柱状图,多次调用bar()函数即可。这样的场景非常多,如本文中比较S10总决赛各位置的数据,除了比较击杀数,将死亡数和助攻数一起比较会更合理。

绘制多组柱状图时,要对柱状图的x轴进行调整,如有三组柱状图,左边一组的x轴要左移,右边一组的x轴要右移,移动的距离是中间一组柱状图的宽度。一般情况下,三组柱状图的宽度相等。

对于多组柱状图,一般同一组使用相同的颜色,展示效果会更好。并且设置好图例对不同颜色的柱状图做好说明。

从这张图像来看,打野、中单、下路的数据差异不大,与上路和辅助对比,比较吻合游戏中的定位。不过这张图中的数据是胜方和负方数据的均值,为了更准确地分析各位置对胜利的贡献,可以将胜方和负方的数据分开,比较对位数据。

四、matplotlib绘制多张多组柱状图

代码语言:javascript复制
import matplotlib.pyplot as plt
from matplotlib import ticker
from numpy import mean


location = ["上单", "打野", "中单", "下路", "辅助"]
win_loc_kill, win_loc_die, win_loc_assists = [[list() for _ in range(5)] for _ in range(3)]
lose_loc_kill, lose_loc_die, lose_loc_assists = [[list() for _ in range(5)] for _ in range(3)]
for i in range(5):
    win_loc_kill[i] = [value[0][i][0] for value in data.values()]
    win_loc_die[i] = [value[0][i][1] for value in data.values()]
    win_loc_assists[i] = [value[0][i][2] for value in data.values()]
    lose_loc_kill[i] = [value[1][i][0] for value in data.values()]
    lose_loc_die[i] = [value[1][i][1] for value in data.values()]
    lose_loc_assists[i] = [value[1][i][2] for value in data.values()]
# noinspection PyTypeChecker
win_avg_kill = [round(mean(kill), 2) for kill in win_loc_kill]
# noinspection PyTypeChecker
win_avg_die = [round(mean(die), 2) for die in win_loc_die]
# noinspection PyTypeChecker
win_avg_assists = [round(mean(assists), 2) for assists in win_loc_assists]
# noinspection PyTypeChecker
lose_avg_kill = [round(mean(kill), 2) for kill in lose_loc_kill]
# noinspection PyTypeChecker
lose_avg_die = [round(mean(die), 2) for die in lose_loc_die]
# noinspection PyTypeChecker
lose_avg_assists = [round(mean(assists), 2) for assists in lose_loc_assists]
fig, axs = plt.subplots(nrows=2, ncols=1, figsize=(20, 16), dpi=100)
x = range(len(location))
axs[0].bar([i-0.2 for i in x], win_avg_kill, width=0.2, color='b')
axs[0].bar(x, win_avg_die, width=0.2, color='r')
axs[0].bar([i 0.2 for i in x], win_avg_assists, width=0.2, color='g')
axs[1].bar([i-0.2 for i in x], lose_avg_kill, width=0.2, color='b')
axs[1].bar(x, lose_avg_die, width=0.2, color='r')
axs[1].bar([i 0.2 for i in x], lose_avg_assists, width=0.2, color='g')
for a, b in zip(x, win_avg_kill):
    axs[0].text(a-0.2, b 0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
for a, b in zip(x, win_avg_die):
    axs[0].text(a, b 0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
for a, b in zip(x, win_avg_assists):
    axs[0].text(a 0.2, b 0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
for a, b in zip(x, lose_avg_kill):
    axs[1].text(a-0.2, b 0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
for a, b in zip(x, lose_avg_die):
    axs[1].text(a, b 0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
for a, b in zip(x, lose_avg_assists):
    axs[1].text(a 0.2, b 0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
for i in range(2):
    axs[i].xaxis.set_major_locator(ticker.FixedLocator(x))
    axs[i].xaxis.set_major_formatter(ticker.FixedFormatter(location))
    axs[i].set_yticks(range(0, 11, 2))
    axs[i].grid(linestyle="--", alpha=0.5)
    axs[i].legend(['击杀', '死亡', '助攻'], loc='upper left', fontsize=16, markerscale=0.5)
    axs[i].set_xlabel("位置", fontsize=18)
    axs[i].set_ylabel("场均数据", fontsize=18, rotation=0)
axs[0].set_title("S10总决赛胜方各位置场均数据", fontsize=18)
axs[1].set_title("S10总决赛负方各位置场均数据", fontsize=18)
plt.show()

运行结果:

subplots(): 用于在同一张图像中绘制多张图表,包含折线图、散点图和柱状图等。通过nrows, ncols两个参数设置图表的张数和排列方式。subplots()函数返回两个参数,一个是图像对象fig,一个是可迭代的图表数组axs(类型为numpy中的数组对象)。绘制每一张图表时,从axs中取出每一张图表对象,再调用bar()函数绘制柱状图,要在一张图表中绘制多组柱状图,多次调用bar()函数即可。

每一张图表中的标签、标题、样式、图例等都需要单独设置。在设置坐标轴、标签、标题时,使用'set_'开头的方法进行设置,如设置x轴标签用set_xlabel()。

因为matplotlib版本的原因,在matplotlib3.2后,设置x轴的刻度标签时,需要导入matplotlib中的ticker模块,使用ticker模块中的FixedLocator和FixedFormatter两个类来设置x轴的刻度显示指定的值,否则x轴的刻度只能显示绘图时的数字1,2,3...,使用set_xticks()和set_xticklabels()都不能实现。

最后,将数据中的胜方和负方分开比较,可以看出对位数据最好的位置是中路,其次是打野。这就是使用matplotlib绘制柱状图的简单介绍,需要进行更多比较和分析可以做更多尝试。

0 人点赞