美文网首页
python matplotlib做对比条形图

python matplotlib做对比条形图

作者: 丙吉 | 来源:发表于2020-04-02 20:31 被阅读0次

今天想看下两组数据的对比情况,就给画了个图:

classify = ['accept', 'reject'] # 姓名

education = list(ac['index'])

counts = (list(ac['education']), list(re['education']))

# 设置柱形图宽度

bar_width = 0.35

index = np.arange(len(counts[0]))

# 绘制

rects1 = plt.bar(index, counts[0], bar_width, color='#0072BC', label=classify[0])

# 绘制

rects2 = plt.bar(index + bar_width, counts[1], bar_width, color='#ED1C24', label=classify[1])

# X轴标题

plt.xticks(index + bar_width, education)

# Y轴范围

plt.ylim(ymax=3000, ymin=0)

# 图表标题

plt.title('the education of accept, reject')

# 图例显示在图表下方

plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.03), fancybox=True, ncol=5)

# 添加数据标签

def add_labels(rects):

    for rect in rects:

        height = rect.get_height()

        plt.text(rect.get_x() + rect.get_width() / 2, height, height, ha='center', va='bottom')

        # 柱形图边缘用白色填充,纯粹为了美观

        rect.set_edgecolor('white')

add_labels(rects1)

add_labels(rects2)

相关文章

网友评论

      本文标题:python matplotlib做对比条形图

      本文链接:https://www.haomeiwen.com/subject/yepfphtx.html