matplotlib条形堆积图并添加
matplotlib条形堆积图并添加
#!/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 plt
import numpy as np
labels = [‘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) sequence
fig, 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()
效果如下: