import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import _rebuild
from matplotlib.ticker import FuncFormatter
_rebuild()
if __name__ == '__main__':
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x1 = np.arange(2009, 2022)
x2 = x1[1:]
y1 = np.array([140, 151, 165, 176, 172, 165, 177, 201, 238, 290, 341, 377, 457])
y2 = (y1[1:] - y1[:-1]) / y1[:-1]
fig, ax1 = plt.subplots()
bar = ax1.bar(x1, y1)
ax1.legend(bar, ("考研人数(万人)",), loc=[0.2, -0.155])
for a, b in zip(x1, y1):
ax1.text(a, b + 0.05, '%.0f' % b, ha='center', va='bottom', fontsize=11)
ax2 = ax1.twinx()
def to_percent(value, position):
return "%1.0f" % (100 * value) + '%'
ax2.yaxis.set_major_formatter(FuncFormatter(to_percent))
line = ax2.plot(x2, y2, c='r')
for a, b in zip(x2, y2):
ax2.text(a, b, "%.2f" % (100 * b) + '%', ha='center', va='bottom', fontsize=10)
ax2.legend(line, ("增速",), loc=[0.6, -0.155])
ax2.set_title("全球销售额及其增速", fontsize=16)
input()
