此示例显示如何创建分组条形图以及如何使用标签注释条形。
#! /usr/bin/env python
#coding=utf-8
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei'] #
plt.rcParams['axes.unicode_minus'] = False
# 原文出自【易百教程】,商业转载请联系作者获得授权,非商业请保留原文链接。
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='男')
rects2 = ax.bar(x + width/2, women_means, width, label='女')
# Add some text for labels, title and custom x-axis tick label