#导入库
import numpy as np
import matplotlib.pyplot as plt
# 1.准备数据
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y_cos, y_sin = np.cos(x), np.sin(x)
# 2.绘制折线
plt.plot(x, y_sin, color='red', linewidth=1.0, label='sin(x)')
plt.plot(x, y_cos, color='blue', linewidth=1.0, alpha=0.5, label='cos(x)')
# 3.图表辅助元素定制
plt.title('Sine and Cosine')
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], ['-π', '-π/2', '0', 'π/2', 'π'])
plt.yticks([-1, 0, 1])
plt.legend(loc='upper left')
plt.grid(True)
# 3.图表辅助元素定制
plt.title('Sine and Cosine')
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], ['-π', '-π/2', '0', 'π/2', 'π'])
plt.yticks([-1, 0, 1])
plt.legend(loc='upper left')
plt.grid(True)
# 4.图表样式美化
plt.fill_between(x, y_sin, y_cos, where=(y_sin < y_cos), color='green', alpha=0.25)
# 5.添加指向型注释文本
plt.annotate('({:.2f}, {:.2f})'.format(1, np.cos(1)), xy=(1, np.cos(1)), xytext=(1, np.cos(1)+0.5),
arrowprops=dict(facecolor='black', arrowstyle='->'))
plt.show()
运行如下