import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi, np.pi, 200, endpoint=True)
c, s = np.cos(x), np.sin(x)
plt.figure(1)
plt.plot(x, c, color="blue", linewidth=1.0, linestyle="-", label="cos", alpha=0.5)
plt.plot(x, s, "r*", label="sin")
plt.title("cos sin")
# 边界设置
ax = plt.gca()
ax.spines["left"].set_position(("data", 0))
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.spines["bottom"].set_position(("data", 0))
# 左上角图例
plt.legend(loc="upper left")
# 网格
plt.grid()
# 黄色参考线
plt.plot([1, 1], [0, np.cos(1)], "y", linewidth=3, linestyle="--")
# 注释
plt.annotate("cos(1)", xy=(1, np.cos(1)), xycoords="data", xytext=(+10, +30), textcoords="offset points", arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.show()
结果展示