import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
# 生成数据
x = np.linspace(0, 20, 20)
y1 = np.random.randint(50, 100, 20)
y2 = np.random.randint(50, 100, 20)
y3 = np.random.randint(50, 100, 20)
# 堆积柱形图
plt.bar(x, y1, color='y', label='语文')
plt.bar(x, y2, bottom=y1, color='g', label='数学')
plt.bar(x, y3, bottom=y1 + y2, color='c', label='英语')
# 显示范围
plt.xlim(-2, 22)
plt.ylim(0, 280)
# 添加图例
plt.legend(loc='upper right')
plt.grid(axis='y', color='grey', linestyle=':', linewidth=1) # 网格
plt.show()