""" 函数图像生成脚本 - 考研数学一标准图像库 ===================================== 风格规范: - CJK 字体: SimHei (fallback: Noto Sans SC) - 坐标系: 黑色实线过原点,灰色网格 - 曲线: C0 蓝色线宽 2 - 渐近线: 红色虚线 - 关键点: 红色圆点 + 黄色标签 输出: images/ 下 PNG,统一 7×5 英寸 依赖: matplotlib, numpy """ import matplotlib matplotlib.use('Agg') # non-interactive backend for batch generation import matplotlib.pyplot as plt import numpy as np from pathlib import Path # ============ 1. 全局样式 ============ plt.rcParams['font.sans-serif'] = ['SimHei', 'Noto Sans SC', 'sans-serif'] plt.rcParams['axes.unicode_minus'] = False plt.rcParams['figure.dpi'] = 110 plt.rcParams['savefig.dpi'] = 110 plt.rcParams['axes.spines.top'] = False plt.rcParams['axes.spines.right'] = False plt.rcParams['lines.linewidth'] = 2.0 HERE = Path(__file__).parent IMG = HERE / 'images' IMG.mkdir(exist_ok=True) # ============ 2. 工具函数 ============ def new_ax(title=''): fig, ax = plt.subplots(figsize=(7, 5)) if title: ax.set_title(title, fontsize=13, pad=10) ax.axhline(0, color='k', linewidth=0.6) ax.axvline(0, color='k', linewidth=0.6) ax.grid(True, alpha=0.3, linestyle=':') ax.set_xlabel('x', fontsize=11) ax.set_ylabel('y', fontsize=11) return fig, ax def plot_curve(ax, f, x_range, label=None, color='C0', n=2000, lw=2.0): """绘制函数曲线,自动跳过 inf/nan 与不连续跳变""" x = np.linspace(*x_range, n) with np.errstate(divide='ignore', invalid='ignore'): y = np.array(f(x), dtype=float) valid = np.isfinite(y) # 在 invalid 处切段绘制(保证垂直渐近线两侧曲线都画出) splits = np.where(~valid)[0] starts = [0] + (splits + 1).tolist() ends = splits.tolist() + [len(x)] drawn_label = label for s, e in zip(starts, ends): if e - s >= 2: ax.plot(x[s:e], y[s:e], color=color, linewidth=lw, label=drawn_label) drawn_label = None # 只给第一段加 label def add_vertical_asymptote(ax, x, label=None): ax.axvline(x, color='r', linestyle='--', alpha=0.6, label=label or f'x={x:g} 渐近线') def add_horizontal_asymptote(ax, y, label=None): ax.axhline(y, color='r', linestyle='--', alpha=0.6, label=label or f'y={y:g} 渐近线') def add_oblique_asymptote(ax, k, b, x_range=(-10, 10), label=None): x = np.linspace(*x_range, 100) ax.plot(x, k * x + b, 'r--', alpha=0.6, label=label or f'y={k:g}x+{b:g} 渐近线') def mark_point(ax, x, y, label=None): ax.plot(x, y, 'o', color='red', markersize=7, zorder=5) if label: ax.annotate(label, xy=(x, y), xytext=(8, 8), textcoords='offset points', fontsize=10, bbox=dict(boxstyle='round,pad=0.3', fc='lightyellow', ec='gray', alpha=0.9)) def save(fig, name): out = IMG / name fig.savefig(out, bbox_inches='tight') plt.close(fig) print(f' 生成: {name}') # ============ 3. 各组绘图函数 ============ # ====== Group 1: 基本初等函数 ====== def g01_power_family(): fig, ax = new_ax('幂函数族') xr = (-2, 2) plot_curve(ax, lambda x: x ** 2, xr, label=r'y=$x^2$', color='C0') plot_curve(ax, lambda x: x ** 3, xr, label=r'y=$x^3$', color='C1') plot_curve(ax, lambda x: np.sqrt(np.maximum(x, 0)), (0, 2), label=r'y=$\sqrt{x}$', color='C2') plot_curve(ax, lambda x: np.cbrt(x), xr, label=r'y=$x^{1/3}$', color='C3') plot_curve(ax, lambda x: 1 / x, (-2, 0), label='y=1/x (左)', color='C4') plot_curve(ax, lambda x: 1 / x, (0, 2), label='y=1/x (右)', color='C4') ax.set_xlim(-2, 2) ax.set_ylim(-3, 3) ax.legend(fontsize=9, loc='upper left') save(fig, '01_power_family.png') def g01_exp_family(): fig, ax = new_ax('指数函数族') xr = (-3, 3) plot_curve(ax, lambda x: np.exp(x), xr, label='y=e^x', color='C0') plot_curve(ax, lambda x: 2 ** x, xr, label='y=2^x', color='C1') plot_curve(ax, lambda x: (1 / 2) ** x, xr, label='y=(1/2)^x', color='C2') add_horizontal_asymptote(ax, 0) ax.set_xlim(-3, 3) ax.set_ylim(-1, 8) ax.legend(fontsize=9) save(fig, '02_exp_family.png') def g01_log_family(): fig, ax = new_ax('对数函数族') xr = (0.001, 4) plot_curve(ax, lambda x: np.log(x), xr, label='y=ln(x)', color='C0') plot_curve(ax, lambda x: np.log2(x), xr, label=r'y=$\log_2(x)$', color='C1') add_vertical_asymptote(ax, 0) ax.set_xlim(-0.5, 4) ax.set_ylim(-3, 3) ax.legend(fontsize=9) save(fig, '03_log_family.png') def g01_trig_basic(): fig, ax = new_ax('基本三角函数 (一个周期)') xr = (-2 * np.pi, 2 * np.pi) plot_curve(ax, np.sin, xr, label='y=sin(x)', color='C0') plot_curve(ax, np.cos, xr, label='y=cos(x)', color='C1') # tan 在 ±π/2 附近会炸,画小范围 xt = np.linspace(-1.4, 1.4, 400) yt = np.tan(xt) valid = np.abs(yt) < 10 ax.plot(xt[valid], yt[valid], color='C2', label='y=tan(x)') add_vertical_asymptote(ax, np.pi / 2, label=r'x=$\pi/2$ (tan)') add_vertical_asymptote(ax, -np.pi / 2, label=r'x=$-\pi/2$ (tan)') ax.set_xlim(-2 * np.pi, 2 * np.pi) ax.set_ylim(-3, 3) ax.legend(fontsize=9, loc='upper right') save(fig, '04_trig_basic.png') # ====== Group 2: 反函数与反三角 ====== def g02_arcsin_arctan(): fig, ax = new_ax('反三角函数') xr = (-1, 1) plot_curve(ax, np.arcsin, xr, label=r'y=$\arcsin(x)$', color='C0') # arctan 定义域 R,画全 plot_curve(ax, np.arctan, (-5, 5), label=r'y=$\arctan(x)$', color='C1') add_horizontal_asymptote(ax, np.pi / 2, label='y=π/2') add_horizontal_asymptote(ax, -np.pi / 2, label='y=-π/2') ax.set_xlim(-5, 5) ax.set_ylim(-2, 2) ax.legend(fontsize=9) save(fig, '05_arcsin_arctan.png') def g02_exp_log_inverse(): fig, ax = new_ax('指数与对数:关于 y=x 对称') xr = (-3, 3) plot_curve(ax, np.exp, xr, label='y=e^x', color='C0') # y = ln(x) 镜像画 xr2 = (0.001, 10) plot_curve(ax, np.log, xr2, label='y=ln(x)', color='C1') # 对称线 y = x xa = np.linspace(-2, 10, 100) ax.plot(xa, xa, 'k--', alpha=0.4, label='y=x') add_horizontal_asymptote(ax, 0, label=None) add_vertical_asymptote(ax, 0, label=None) ax.set_xlim(-3, 8) ax.set_ylim(-3, 8) ax.legend(fontsize=9, loc='upper left') save(fig, '06_exp_log_inverse.png') def g02_arccos(): fig, ax = new_ax('y = arccos(x)') xr = (-1, 1) plot_curve(ax, np.arccos, xr, label=r'y=$\arccos(x)$', color='C0') ax.set_xlim(-1.5, 1.5) ax.set_ylim(0, np.pi + 0.3) ax.legend(fontsize=9) save(fig, '07_arccos.png') # ====== Group 3: 函数变换 ====== def g03_shift(): fig, ax = new_ax('平移变换 y = sin(x), sin(x-π/2), sin(x)+1') xr = (0, 2 * np.pi) xs = np.linspace(*xr, 1000) ax.plot(xs, np.sin(xs), label='y=sin(x)', color='C0') ax.plot(xs, np.sin(xs - np.pi / 2), label=r'y=$\sin(x-\pi/2)$ → 右移 $\pi/2$', color='C1') ax.plot(xs, np.sin(xs) + 1, label=r'y=$\sin(x)+1$ → 上移 1', color='C2') ax.set_xlim(0, 2 * np.pi) ax.set_ylim(-2, 3) ax.legend(fontsize=9) save(fig, '08_shift.png') def g03_scale(): fig, ax = new_ax('伸缩变换 y = sin(x), 2sin(x), sin(2x)') xr = (0, 2 * np.pi) xs = np.linspace(*xr, 1000) ax.plot(xs, np.sin(xs), label='y=sin(x)', color='C0') ax.plot(xs, 2 * np.sin(xs), label=r'y=$2\sin(x)$ → 振幅 ×2', color='C1') ax.plot(xs, np.sin(2 * xs), label=r'y=$\sin(2x)$ → 周期 ×1/2', color='C2') ax.set_xlim(0, 2 * np.pi) ax.set_ylim(-3, 3) ax.legend(fontsize=9) save(fig, '09_scale.png') def g03_symmetry(): fig, ax = new_ax('对称变换 y = sin(x), -sin(x), |sin(x)|') xr = (-2 * np.pi, 2 * np.pi) xs = np.linspace(*xr, 1000) ax.plot(xs, np.sin(xs), label='y=sin(x)', color='C0') ax.plot(xs, -np.sin(xs), label=r'y=$-\sin(x)$ → 关于 x 轴对称', color='C1') ax.plot(xs, np.abs(np.sin(xs)), label=r'y=$|\sin(x)|$ → x 轴下翻折', color='C2') ax.set_xlim(-2 * np.pi, 2 * np.pi) ax.set_ylim(-2, 2) ax.legend(fontsize=9) save(fig, '10_symmetry.png') def g03_abs_transform(): fig, ax = new_ax('绝对值变换 y = sin(x), sin(|x|), |sin(x)|') xr = (-2 * np.pi, 2 * np.pi) xs = np.linspace(*xr, 1000) ax.plot(xs, np.sin(xs), label=r'原: y=$\sin(x)$', color='C0') ax.plot(xs, np.sin(np.abs(xs)), label=r'y=$\sin(|x|)$ → 偶化(y 轴镜像)', color='C1') ax.plot(xs, np.abs(np.sin(xs)), label=r'y=$|\sin(x)|$ → 下半轴翻折', color='C2') ax.set_xlim(-2 * np.pi, 2 * np.pi) ax.set_ylim(-2, 2) ax.legend(fontsize=9, loc='upper right') save(fig, '11_abs_transform.png') # ====== Group 4: 考研典型函数 ====== def g04_x_exp_neg(): fig, ax = new_ax('y = x·e^(-x) (典型: 单调 + 极值 + 拐点)') xr = (-1, 4) xs = np.linspace(*xr, 1000) ys = xs * np.exp(-xs) ax.plot(xs, ys, color='C0') # 极值点: x=1, y=1/e mark_point(ax, 1, 1 / np.e, r'极值 (1, $1/e$)') # 拐点: x=2, y=2/e² mark_point(ax, 2, 2 / np.e ** 2, r'拐点 (2, $2/e^2$)') # 渐近线: x→-∞, y → -∞; x→+∞, y→0 add_horizontal_asymptote(ax, 0, label='y=0 (x→+∞)') ax.set_xlim(-1, 4) ax.set_ylim(-1, 1) ax.legend(fontsize=9) save(fig, '12_x_exp_neg.png') def g04_x_ln_x(): fig, ax = new_ax('y = x·ln(x) (典型: 含参单调讨论)') xr = (0.001, 2) xs = np.linspace(*xr, 1000) ys = xs * np.log(xs) ax.plot(xs, ys, color='C0') mark_point(ax, 1 / np.e, -1 / np.e, r'极小 ($1/e$, $-1/e$)') mark_point(ax, 1, 0, '零点 (1, 0)') add_vertical_asymptote(ax, 0) ax.set_xlim(-0.2, 2) ax.set_ylim(-1, 1) ax.legend(fontsize=9) save(fig, '13_x_ln_x.png') def g04_sigmoid(): fig, ax = new_ax('y = e^x/(1+e^x) (Sigmoid)') xr = (-6, 6) xs = np.linspace(*xr, 1000) ys = np.exp(xs) / (1 + np.exp(xs)) ax.plot(xs, ys, color='C0') mark_point(ax, 0, 0.5, '(0, 1/2) 对称中心') add_horizontal_asymptote(ax, 1, label='y=1 (上)') add_horizontal_asymptote(ax, 0, label='y=0 (下)') ax.set_xlim(-6, 6) ax.set_ylim(-0.5, 1.5) ax.legend(fontsize=9) save(fig, '14_sigmoid.png') def g04_sinc(): fig, ax = new_ax('y = sin(x)/x (重要极限 lim = 1)') xr = (-15, 15) xs = np.linspace(*xr, 2000) with np.errstate(divide='ignore', invalid='ignore'): ys = np.sin(xs) / xs valid = np.abs(ys) < 5 ax.plot(xs[valid], ys[valid], color='C0') mark_point(ax, 0, 1, '(0,1) 可去间断点') add_horizontal_asymptote(ax, 0, label='y=0') ax.set_xlim(-15, 15) ax.set_ylim(-0.5, 1.5) ax.legend(fontsize=9) save(fig, '15_sinc.png') def g04_arctan(): fig, ax = new_ax('y = arctan(x) (典型: 双侧水平渐近线)') xr = (-5, 5) xs = np.linspace(*xr, 1000) ax.plot(xs, np.arctan(xs), color='C0') mark_point(ax, 0, 0, '(0,0)') add_horizontal_asymptote(ax, np.pi / 2, label='y=π/2') add_horizontal_asymptote(ax, -np.pi / 2, label='y=-π/2') ax.set_xlim(-5, 5) ax.set_ylim(-2, 2) ax.legend(fontsize=9) save(fig, '16_arctan.png') def g04_rational_x3(): """流水线例题函数: y = x³/(x²-1)""" fig, ax = new_ax(r'y = $x^3/(x^2-1)$ (流水线例题函数)') f = lambda x: x ** 3 / (x ** 2 - 1) xr = (-3, 3) plot_curve(ax, f, xr, label=r'y=$x^3/(x^2-1)$', color='C0') # 垂直渐近线 x = ±1 add_vertical_asymptote(ax, 1) add_vertical_asymptote(ax, -1) # 斜渐近线 y = x add_oblique_asymptote(ax, 1, 0, x_range=(-5, 5), label=r'y=$x$ (斜渐近线)') # 极值点 mark_point(ax, np.sqrt(3), 3 * np.sqrt(3) / 2, r'极小 ($\sqrt{3}$, $3\sqrt{3}/2$)') mark_point(ax, -np.sqrt(3), -3 * np.sqrt(3) / 2, r'极大 ($-\sqrt{3}$, $-3\sqrt{3}/2$)') # 拐点 mark_point(ax, 0, 0, '拐点 (0,0)') ax.set_xlim(-3, 3) ax.set_ylim(-5, 5) ax.legend(fontsize=9) save(fig, '17_rational_x3.png') def g04_rational_1_minus_1_over_x2(): """渐近线典型: y = 1 - 1/x²""" fig, ax = new_ax(r'y = $1 - 1/x^2$ (典型渐近线)') f = lambda x: 1 - 1 / (x ** 2) xr = (-3, 3) plot_curve(ax, f, xr, label=r'y=$1-1/x^2$', color='C0') add_vertical_asymptote(ax, 0, label='x=0 (垂直)') add_horizontal_asymptote(ax, 1, label='y=1 (水平)') mark_point(ax, 1, 0, '(1, 0) 零点') mark_point(ax, -1, 0, '(-1, 0) 零点') # 极小点: x = ±√3, y = 2/3 sq3 = np.sqrt(3) mark_point(ax, sq3, 2 / 3, r'($\sqrt{3}$, $2/3$) 极小') mark_point(ax, -sq3, 2 / 3, r'($-\sqrt{3}$, $2/3$) 极小') ax.set_xlim(-3, 3) ax.set_ylim(-2, 3) ax.legend(fontsize=9) save(fig, '18_rational_1_minus_1_over_x2.png') def g04_rational_1_plus_x_over_1_plus_x2(): """典型分式: y = (1+x)/(1+x²)""" fig, ax = new_ax(r'y = $(1+x)/(1+x^2)$ (典型分式)') f = lambda x: (1 + x) / (1 + x ** 2) xr = (-3, 3) plot_curve(ax, f, xr, label=r'y=$(1+x)/(1+x^2)$', color='C0') add_horizontal_asymptote(ax, 0, label='y=0 (双侧)') mark_point(ax, 0, 1, '极大 (0, 1)') mark_point(ax, -1, 0, '零点 (-1, 0)') mark_point(ax, -2, -1 / 5, '极小 (-2, -1/5)') ax.set_xlim(-3, 3) ax.set_ylim(-1, 1.5) ax.legend(fontsize=9) save(fig, '19_rational_1_plus_x_over_1_plus_x2.png') # ============ 4. 主入口 ============ if __name__ == '__main__': print('生成函数图像库...\n') groups = [ ('Group 1: 基本初等函数', [ g01_power_family, g01_exp_family, g01_log_family, g01_trig_basic, ]), ('Group 2: 反函数与反三角', [ g02_arcsin_arctan, g02_exp_log_inverse, g02_arccos, ]), ('Group 3: 函数变换', [ g03_shift, g03_scale, g03_symmetry, g03_abs_transform, ]), ('Group 4: 考研典型函数', [ g04_x_exp_neg, g04_x_ln_x, g04_sigmoid, g04_sinc, g04_arctan, g04_rational_x3, g04_rational_1_minus_1_over_x2, g04_rational_1_plus_x_over_1_plus_x2, ]), ] for group_name, fns in groups: print(f'[{group_name}]') for fn in fns: fn() print() print(f'全部生成完毕,输出目录: {IMG}')