引入依赖
import matplotlib.pyplot as plt
显示中文和负号
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
示例数据
x = ['2017年', '2018年', '2019年', '2020年', '2021年']
y1 = [8.89, 9.75, 10.81, 11.98, 12.91]
y2 = [12.17, 11.63, 12.51, 14.31, 15.70]
y3 = [y1[i]+y2[i] for i in range(len(x))]
绘图
plt.figure(figsize=(12, 6))
plt.fill_between(x, y1, color='orange', alpha=0.5, label='本科')
plt.fill_between(x, y3, color='blue', alpha=0.5, label='专科')
plt.ylabel('单位(万人)')
plt.title('每年大学生毕业人数')
plt.ylim(0, max(y3)+1)
plt.legend(loc='upper left')
plt.annotate(str(x[-1]) + '总毕业人数' + str(y3[-1]) + '万人', xy=(x[-1], y3[-1]), xytext=(x[-2], y3[-1]),
arrowprops=dict(facecolor='black', shrink=0.05, width=0.3, headwidth=6, headlength=4),
fontsize=8)
plt.gca().spines['top'].set_color('none')
plt.gca().spines['right'].set_color('none')
plt.show()
效果图
