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()
结果展示