import osimport numpy as npimport matplotlib as mplimport matplotlib.pyplot as pltfrom matplotlib import font_manager as fm# =========================# 字体设置# =========================win_fonts = r"C:\Windows\Fonts"for p in [ os.path.join(win_fonts,"times.ttf"), os.path.join(win_fonts,"timesbd.ttf"), os.path.join(win_fonts,"timesi.ttf"), os.path.join(win_fonts,"simsun.ttc"),]:if os.path.exists(p): try: fm.fontManager.addfont(p) except Exception: passmpl.rcParams["font.family"] = ["Times New Roman", "SimSun"]mpl.rcParams["axes.unicode_minus"] = False# =========================# 输出路径# =========================OUT_DIR = r"D:\py_figs"os.makedirs(OUT_DIR, exist_ok=True)# =========================# 构造数据# =========================np.random.seed(42)n = 500x1 = np.random.normal(0, 1, n)y1 = x1 + np.random.normal(0, 0.5, n)x2 = np.random.normal(2, 1, n)y2 = 0.5 * x2 + np.random.normal(0, 0.5, n)x3 = np.random.normal(-2, 1, n)y3 = -0.8 * x3 + np.random.normal(0, 0.5, n)# =========================# 绘图# =========================fig, ax = plt.subplots(figsize=(6,5))ax.scatter(x1, y1, s=12, alpha=0.4, color="#4C78A8", marker='o', label="Group A / 组A")ax.scatter(x2, y2, s=12, alpha=0.4, color="#F58518", marker='s', label="Group B / 组B")ax.scatter(x3, y3, s=12, alpha=0.4, color="#54A24B", marker='^', label="Group C / 组C")ax.set_title("Grouped Scatter Plot / 分组散点图", fontsize=14)ax.set_xlabel("X Variable / 自变量", fontsize=12)ax.set_ylabel("Y Variable / 因变量", fontsize=12)ax.legend(frameon=False)for spine in ax.spines.values():spine.set_linewidth(1.2)# =========================# 保存# =========================out_path = os.path.join(OUT_DIR, "grouped_scatter.jpg")fig.savefig(out_path, dpi=300, bbox_inches="tight")plt.close()print("Saved:", out_path)