import os from pathlib import Path import sys os.environ.setdefault("MPLCONFIGDIR", "/tmp/matplotlib") import matplotlib.pyplot as plt import numpy as np from matplotlib.ticker import AutoMinorLocator FONT_NAME = "LXGW WenKai" BG_COLOR = "white" GRID_MAJOR = "#111111" GRID_MINOR = "#8a8a8a" LINE_COLOR = "#111111" POINT_COLOR = "#111111" LOAD_RESISTANCE_OHM = np.array( [888.43, 788.46, 625.00, 534.84, 468.16, 494.69, 439.93, 415.40], dtype=float, ) POWER_MW = np.array( [130.07, 133.25, 136.90, 143.15, 142.23, 142.29, 142.22, 141.25], dtype=float, ) DEFAULT_OUTPUT = ( Path(__file__).resolve().parent.parent / "image" / "exp5" / "power_load_curve.svg" ) def draw_curve(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 order = np.argsort(LOAD_RESISTANCE_OHM) x = LOAD_RESISTANCE_OHM[order] y = POWER_MW[order] fig, ax = plt.subplots(figsize=(8.2, 6.0), dpi=160) fig.patch.set_facecolor(BG_COLOR) ax.set_facecolor(BG_COLOR) ax.plot( x, y, color=LINE_COLOR, linewidth=2.2, zorder=2, ) ax.scatter( x, y, s=55, color=POINT_COLOR, edgecolors="white", linewidths=0.8, zorder=3, ) ax.set_xlabel("负载电阻 R_L / Ω", fontsize=13) ax.set_ylabel("负载功率 P / mW", fontsize=13) ax.set_xlim(400, 900) ax.set_ylim(129, 144) ax.set_xticks(np.arange(400, 901, 100)) ax.set_yticks(np.arange(130, 145, 2)) ax.xaxis.set_minor_locator(AutoMinorLocator(2)) ax.yaxis.set_minor_locator(AutoMinorLocator(2)) ax.set_box_aspect(6 / 5) ax.grid(True, which="major", color=GRID_MAJOR, linewidth=0.95, alpha=0.9) ax.grid(True, which="minor", color=GRID_MINOR, linewidth=0.55, alpha=0.8) for spine in ax.spines.values(): spine.set_linewidth(1.0) spine.set_color("#111111") fig.tight_layout() 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_curve(output_path)