47 lines
1.2 KiB
Python
47 lines
1.2 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', 'SimHei', 'SimSun', 'DejaVu Sans']
|
|
plt.rcParams['axes.unicode_minus'] = False
|
|
|
|
# 绑定图像尺寸和 DPI
|
|
plt.figure(figsize=(8, 6), dpi=150)
|
|
|
|
# 定义函数
|
|
def f(x):
|
|
return -2 * x**2 + 8 * x - 3
|
|
|
|
# 生成 x 值
|
|
x = np.linspace(-1, 5, 400)
|
|
y = f(x)
|
|
|
|
# 绘制函数曲线
|
|
plt.plot(x, y, label=r'$y = -2x^2 + 8x - 3$', color='blue')
|
|
|
|
# 标记顶点 (2, 5)
|
|
vertex_x = 2
|
|
vertex_y = 5
|
|
plt.plot(vertex_x, vertex_y, 'ro', label='顶点 (2, 5)')
|
|
plt.annotate(f'({vertex_x}, {vertex_y})', xy=(vertex_x, vertex_y), xytext=(vertex_x + 0.5, vertex_y),
|
|
arrowprops=dict(facecolor='black', shrink=0.05))
|
|
|
|
# 添加坐标轴标签和标题
|
|
plt.xlabel('x')
|
|
plt.ylabel('y')
|
|
plt.title('二次函数 $y = -2x^2 + 8x - 3$ 图像')
|
|
plt.axhline(0, color='black',linewidth=0.5)
|
|
plt.axvline(0, color='black',linewidth=0.5)
|
|
plt.grid(True, linestyle='--', alpha=0.7)
|
|
plt.legend()
|
|
|
|
# 保存图像
|
|
plt.savefig('figure.png', bbox_inches='tight')
|
|
plt.close()
|
|
print("图像已保存: figure.png")
|