import os from pathlib import Path import sys os.environ.setdefault("MPLCONFIGDIR", "/tmp/matplotlib") import matplotlib.pyplot as plt FONT_NAME = "STIXGeneral" BG_COLOR = "white" LINE_COLOR = "#111111" DEFAULT_OUTPUT = ( Path(__file__).resolve().parent.parent / "image" / "exp0" / "ro_measure_diagram.svg" ) def draw_diagram(save_path=DEFAULT_OUTPUT): save_path = Path(save_path) save_path.parent.mkdir(parents=True, exist_ok=True) plt.rcParams["font.family"] = FONT_NAME plt.rcParams["axes.unicode_minus"] = False fig, ax = plt.subplots(figsize=(5.2, 5.5), dpi=160) fig.patch.set_facecolor(BG_COLOR) ax.set_facecolor(BG_COLOR) # 坐标范围 ax.set_xlim(-0.6, 9.8) ax.set_ylim(-0.45, 9.85) ax.axis("off") # 关键点 origin = (0.0, 0.0) u_oc = (0.0, 8.8) i_sc = (8.3, 0.0) a_pt = (2.6, 6.05) b_pt = (5.5, 2.95) # 坐标轴 ax.annotate( "", xy=(9.35, 0.0), xytext=origin, arrowprops=dict(arrowstyle="-|>", lw=1.9, color=LINE_COLOR), ) ax.annotate( "", xy=(0.0, 9.65), xytext=origin, arrowprops=dict(arrowstyle="-|>", lw=1.9, color=LINE_COLOR), ) # 主特性直线 ax.plot([u_oc[0], i_sc[0]], [u_oc[1], i_sc[1]], color=LINE_COLOR, lw=2.2) # A/B 点 ax.text(a_pt[0] + 0.18, a_pt[1] + 0.22, "A", fontsize=14) ax.text(b_pt[0] + 0.18, b_pt[1] - 0.1, "B", fontsize=14) # ΔU / ΔI 虚线框 x_left = a_pt[0] x_right = b_pt[0] y_top = a_pt[1] y_bottom = b_pt[1] dash = (0, (6, 4)) ax.plot([x_left, x_left], [0, y_top], color=LINE_COLOR, lw=1.3, ls=dash) ax.plot([x_right, x_right], [0, y_bottom], color=LINE_COLOR, lw=1.3, ls=dash) ax.plot([0, x_left], [y_top, y_top], color=LINE_COLOR, lw=1.3, ls=dash) ax.plot([0, x_right], [y_bottom, y_bottom], color=LINE_COLOR, lw=1.3, ls=dash) # ΔU 竖向双箭头 x_du = 1.45 ax.annotate( "", xy=(x_du, y_top), xytext=(x_du, y_bottom), arrowprops=dict(arrowstyle="<|-|>", lw=1.4, color=LINE_COLOR), ) ax.text(x_du - 0.55, (y_top + y_bottom) / 2 - 0.15, r"$\Delta U$", fontsize=14) # ΔI 横向双箭头 y_di = 1.05 ax.annotate( "", xy=(x_right, y_di), xytext=(x_left, y_di), arrowprops=dict(arrowstyle="<|-|>", lw=1.4, color=LINE_COLOR), ) ax.text((x_left + x_right) / 2 - 0.35, y_di - 0.72, r"$\Delta I$", fontsize=14) # 角度 phi ax.text(4.32, 2.3, r"$\varphi$", fontsize=16) # 轴与截距标注 ax.text(9.05, -0.48, r"$I$", fontsize=18) ax.text(0.25, 9.35, r"$U$", fontsize=18) ax.text(-0.18, -0.35, "0", fontsize=13) ax.text(0.22, 8.9, r"$U_{oc}$", fontsize=14) ax.text(8.06, -0.62, r"$I_{sc}$", fontsize=14) fig.tight_layout(pad=0.2) fig.savefig(save_path, bbox_inches="tight") plt.close(fig) print(f"saved to: {save_path}") if __name__ == "__main__": output_path = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_OUTPUT draw_diagram(output_path)