首页 > 编程知识 正文

matplotlib条形堆积图并添加数据

时间:2023-05-03 15:19:31 阅读:275807 作者:2002

#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time :2021/4/8 14:35# @Author :weiz# @ProjectName :autoLabeling# @File :111.py# @Description :import matplotlib.pyplot as pltimport numpy as nplabels = ['G1', 'G2', 'G3', 'G4', 'G5']men_means = [20, 35, 30, 35, 27]women_means = [25, 32, 34, 20, 25]width = 0.35 # the width of the bars: can also be len(x) sequencefig, ax = plt.subplots(figsize=(5, 3), dpi=200)ax.bar(labels, men_means, width, label='Men', color='green')ax.bar(labels, women_means, width, bottom=men_means, label='Women', color='red')ax.set_ylabel('Scores')ax.set_title('Scores by group and gender')ax.legend()for a, b in zip(np.arange(len(labels)), men_means): plt.text(a, b / 2, '%.0f' % b, ha='center', va='bottom', fontsize=7)for a, b, c in zip(np.arange(len(labels)), men_means, women_means): plt.text(a, b + c / 2, '%.0f' % c, ha='center', va='bottom', fontsize=7)plt.show()

效果如下:

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。