import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 1.准备数据
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y_cos, y_sin = np.cos(x), np.sin(x)
# 2.绘制折线
#plt.plot
plt.plot(x, y_cos, x, y_sin)
# 3.图表辅助元素定制(图表标题,刻度标签,图例,网格,指向型注释文本)
#plt.title(标题)
plt.title('Cos & Sin')
#plt.xticks(x轴坐标)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])
#plt.yticks(y轴坐标)
plt.yticks([-1.0, -0.5, 0.0, 0.5, 1.0])
#plt.legend(图例)
lines = ('COS', 'SIN')
plt.legend(lines, shadow=None, fancybox=True, loc='upper left')
#plt.grid(添加网格)
plt.grid(True)
#plt.annotate(添加指向型文本)
plt.annotate('cos(1)',
xy=(1, np.cos(1)),
xytext=(np.pi/3, 0.75),
arrowprops=dict(arrowstyle='->'))
# 4.图表样式美化(填充颜色)
#plt.fill_between
plt.fill_between(x, y_cos, y_sin, y_cos > y_sin, color='g', alpha=0.25)
plt.show()
运行结果图: