Files
2026-01-09 17:17:01 +08:00

58 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# /// script
# requires-python = ">=3.11"
# dependencies = ["numpy", "matplotlib"]
# ///
import numpy as np
import matplotlib.pyplot as plt
# 设置字体优先使用中文字体DejaVu Sans 作为 fallback
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)
# 定义二次函数 y = -2x^2 + 8x - 3
def quadratic_func(x):
return -2*x**2 + 8*x - 3
# 生成x值覆盖关键区域
x = np.linspace(-1, 5, 400)
y = quadratic_func(x)
# 绘制函数曲线
plt.plot(x, y, label=r'$y = -2x^2 + 8x - 3$', color='blue', linewidth=2)
# 标出顶点 (2, 5)
vertex_x, vertex_y = 2, 5
plt.plot(vertex_x, vertex_y, 'ro', markersize=8, label=f'顶点 ({vertex_x}, {vertex_y})')
# 添加网格
plt.grid(True, linestyle='--', alpha=0.6)
# 设置坐标轴标签和标题
plt.xlabel('x', fontsize=12)
plt.ylabel('y', fontsize=12)
plt.title('二次函数 $y = -2x^2 + 8x - 3$ 的图像', fontsize=14)
# 设置坐标轴范围
plt.xlim(-1, 5)
plt.ylim(-5, 7)
# 添加图例
plt.legend()
# 在顶点处添加注释
plt.annotate(f'顶点\n({vertex_x}, {vertex_y})',
xy=(vertex_x, vertex_y),
xytext=(vertex_x + 1.2, vertex_y - 1),
arrowprops=dict(arrowstyle='->', color='red', lw=1.5),
fontsize=12,
color='red',
ha='center')
# 保存图像
plt.savefig('figure.png', bbox_inches='tight')
plt.close()
print("图像已保存: figure.png")