import matplotlib.pyplot as plt
import numpy as np
#在画的图中显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False#显示中文
x = np.linspace(-np.pi,np.pi,256,endpoint=True)#x取值范围
plt.title('绘图实例:COS()和SIN()曲线',c='g')#标题
#标签,可加可不加
# plt.xlabel('x')
# plt.ylabel('y')
#x轴范围lim和刻度ticks
plt.xlim(-2*np.pi,2*np.pi)
plt.xticks([-np.pi,-np.pi/2,0,1/2 * np.pi,np.pi],[r'$-\pi$',r'$-\pi/2$','0',r'$\pi/2$',r'$\pi$'])
#y轴范围lim和刻度ticks
plt.ylim(-2,2)
plt.yticks([-1,0,1])
#设置曲线的颜色,b代表蓝色,r是红色
plt.plot(x,np.sin(x),'b')
plt.plot(x,np.cos(x),'r')
#在点(-pi,-1)位置设置一个蓝色圆点
plt.plot(-np.pi,-1,'ob')
#调整坐标轴位置
ax = plt.gca()
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
plt.grid()
#填充cos函数(-2.50.5绿色
plt.fill_between(x,np.cos(x),where=(-2.50.5,facecolor='green')
t = 2 * np.pi / 3
plt.scatter([t],[np.sin(t)] ,s=50, c='r')#在t = 2 * np.pi / 3这个点的函数上画红色圆点
#箭头文本注释
plt.annotate(r'$\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
xy=(t, np.sin(t)), xycoords='data',
xytext=(+10, +30), textcoords='offset points',
fontsize=16, color= 'k',
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.legend(['正弦Sin()','余弦Cos()'],bbox_to_anchor=(0.35,0.95))
#保存图片,figsize设置图片大小
# plt.savefig(fname="function.png",figsize=[10,10])
plt.show()