生源质量——python 可视化(plt 多维度 柱状图)

代码:

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']#设置字体以便支持中文


def show():
    total_Score=[852.5, 746.7, 649.2, 625.9, 566.1, 556.7, 526.4, 497.7, 488.0, 457.2]
    matriculate_Quality=[852.5, 746.7, 649.2, 625.9, 566.1, 556.7, 526.4, 497.7, 488.0, 457.2]
    college_Name=['清华大学', '北京大学', '浙江大学', '上海交通大学', '南京大学', '复旦大学', '中国科学技术大学', '华中科技大学', '武汉大学', '中山大学']
    year=[2015]
    ind = np.arange(len(total_Score))
    width = 0.35
    fig, ax = plt.subplots()
    rects1 = ax.bar(ind - width / 2, total_Score, width, yerr=None, color="SkyBlue", label="总分")
    rects2 = ax.bar(ind + width / 2, matriculate_Quality, width, yerr=None, color="IndianRed", label="生源质量")

    ax.set_ylabel("分值")
    ax.set_title(str(year) + "年中国最好大学分数指标")
    ax.set_xticks(ind)
    ax.set_xticklabels(college_Name, rotation=20)
    ax.legend()

    def autolabel(rects, xpos='center'):
        xpos = xpos.lower()
        ha = {'center': 'center', 'right': 'left', 'left': 'right'}
        offset = {'center': 0.5, 'right': 0.57, 'left': 0.43}

        for rect in rects:
            height = rect.get_height()
            ax.text(rect.get_x() + rect.get_width() * offset[xpos], 1.01 * height,
                    '{}'.format(height), ha=ha[xpos], va='bottom')

    autolabel(rects1, "left")
    autolabel(rects2, "right")

    plt.show()
show()

  

posted @ 2020-05-19 22:34  博二爷  阅读(369)  评论(0编辑  收藏  举报