67 lines
1.6 KiB
Python
67 lines
1.6 KiB
Python
# /// script
|
|
# requires-python = ">=3.11"
|
|
# dependencies = ["numpy", "matplotlib"]
|
|
# ///
|
|
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
|
|
plt.rcParams["font.sans-serif"] = [
|
|
"WenQuanYi Micro Hei",
|
|
"Noto Sans CJK SC",
|
|
"Microsoft YaHei",
|
|
"DejaVu Sans",
|
|
]
|
|
plt.rcParams["axes.unicode_minus"] = False
|
|
|
|
fig, ax = plt.subplots(figsize=(10, 7), dpi=150)
|
|
|
|
x = np.linspace(-1, 5, 500)
|
|
y = -2 * x**2 + 8 * x - 3
|
|
|
|
ax.plot(x, y, "b-", linewidth=2, label="$y = -2x^2 + 8x - 3$")
|
|
|
|
vertex_x, vertex_y = 2, 5
|
|
ax.scatter(
|
|
[vertex_x],
|
|
[vertex_y],
|
|
color="red",
|
|
s=150,
|
|
zorder=5,
|
|
label=f"顶点 ({vertex_x}, {vertex_y})",
|
|
)
|
|
ax.annotate(
|
|
f"顶点 ({vertex_x}, {vertex_y})\n最大值: y = {vertex_y}",
|
|
xy=(vertex_x, vertex_y),
|
|
xytext=(vertex_x + 0.8, vertex_y + 1.5),
|
|
fontsize=11,
|
|
ha="left",
|
|
arrowprops=dict(arrowstyle="->", color="red", lw=1.5),
|
|
bbox=dict(boxstyle="round,pad=0.3", facecolor="yellow", alpha=0.7),
|
|
)
|
|
|
|
ax.axhline(y=0, color="gray", linestyle="--", linewidth=1, alpha=0.7)
|
|
ax.axvline(x=0, color="gray", linestyle="--", linewidth=1, alpha=0.7)
|
|
ax.axvline(
|
|
x=vertex_x,
|
|
color="green",
|
|
linestyle=":",
|
|
linewidth=1.5,
|
|
alpha=0.7,
|
|
label=f"对称轴 x = {vertex_x}",
|
|
)
|
|
|
|
ax.set_xlabel("x", fontsize=12)
|
|
ax.set_ylabel("y", fontsize=12)
|
|
ax.set_title("二次函数 $y = -2x^2 + 8x - 3$ 的图像", fontsize=14, fontweight="bold")
|
|
ax.legend(loc="upper right", fontsize=10)
|
|
ax.grid(True, alpha=0.3)
|
|
|
|
ax.set_xlim(-0.5, 5.5)
|
|
ax.set_ylim(-6, 8)
|
|
|
|
plt.tight_layout()
|
|
plt.savefig("figure.png", bbox_inches="tight", dpi=150)
|
|
plt.close()
|
|
print("图像已保存: figure.png")
|