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)
y1,y2 = np.sin(x),np.cos(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim(x.min() * 1.1, x.max() * 1.1)
ax.set_xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])
ax.set_yticks([-1.0, -0.5, 0, 0.5, 1.0],[r'$-1.0$', r'$-0.5$', r'$0$', r'$0.5$', r'$1.0$'])
ax.plot(x, y2, color='blue', linewidth=1.0, alpha=0.5, label='COS')
ax.plot(x, y1, color='red', linewidth=1.0, linestyle='--', label='SIN')
ax.fill_between(x, (np.abs(x) < 0.5) & (np.abs(x) >-0.5), np.cos(x), np.cos(x) > 0.5, facecolor='green', alpha=0.25)
ax.annotate("cos(1)",
xy=(1.0, np.cos(1)),
xytext=(np.pi/2-0.3, 0.8),
arrowprops=dict(arrowstyle="->"))
ax.grid(visible=True)
ax.set_title('cos & sin')
ax.legend()
plt.show()
祝余whfy
最新推荐文章于 2025-05-07 15:39:45 发布