# /// 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", "SimHei", "SimSun", "DejaVu Sans", ] plt.rcParams["axes.unicode_minus"] = False # 定义函数 def f(x): return -2 * x**2 + 8 * x - 3 # 顶点坐标 x_vertex = 2 y_vertex = 5 # 生成 x 值(根据顶点位置合理设置范围) x = np.linspace(0.5, 3.5, 400) y = f(x) # 绘图 plt.figure(figsize=(10, 7), dpi=150) # 绘制函数曲线 plt.plot(x, y, "b-", linewidth=2, label="y = -2x² + 8x - 3") # 标记顶点 plt.plot( x_vertex, y_vertex, "ro", markersize=10, label=f"顶点 ({x_vertex}, {y_vertex})" ) # 添加顶点坐标标注 plt.annotate( f"最大值\n({x_vertex}, {y_vertex})", xy=(x_vertex, y_vertex), xytext=(2.5, 3), arrowprops=dict(arrowstyle="->", color="red", lw=1.5), fontsize=10, color="red", ) # 添加坐标轴标签和标题 plt.xlabel("x", fontsize=12) plt.ylabel("y", fontsize=12) plt.title("二次函数 y = -2x² + 8x - 3 的图像", fontsize=14, pad=15) # 设置坐标轴范围 plt.xlim(0.5, 3.5) plt.ylim(0, 6) # 显示网格 plt.grid(True, alpha=0.3, linestyle="--") # 添加图例 plt.legend(loc="upper right", fontsize=10) # 保存图像 plt.savefig("figure.png", bbox_inches="tight") print("图像已保存: figure.png")