27 联合网格 JointGrid
27 联合网格 JointGrid
这类图是什么
联合图的自定义版,主图与边缘图分别指定函数。
看什么:完全控制主图与边缘图的画法,样式不受封装限制。
5 个案例
27-1 自定义联合图:主图散点 + 边缘密度,样式全可控

27-2 主图用六边形分箱的联合图

27-3 主图回归、边缘箱线的联合图

27-4 主图密度等高线、边缘直方图的联合图

27-5 按方案分组的联合图:两组散点与边缘叠放

代码
5 个函数取自 code/第五批_组合网格261107.py。关键约束:不调用 sns.set_theme(),否则 font.family 被改成 Arial,中文会变方框;版式由 code/公共配置261107.py 里的 setup_cn_style() 与 save() / save_grid() 统一接管。
# 摘自 code/第五批_组合网格261107.py
def jg_01():
"""自定义联合图:主图散点 + 边缘密度,样式全可控。"""
rng = np.random.default_rng(2701)
t = rng.normal(28, 8, 400)
p = 380 + 9.5 * (t - 20) + rng.normal(0, 50, 400)
g = sns.JointGrid(data=pd.DataFrame({"气温": t, "负荷": p}),
x="气温", y="负荷", height=4.1)
g.plot_joint(sns.scatterplot, s=12, color=C1, edgecolor="white", linewidth=0.2)
g.plot_marginals(sns.kdeplot, fill=True, color=C5, alpha=0.45)
g.ax_joint.set_xlabel("气温 " + IT["T"] + " / °C")
g.ax_joint.set_ylabel("有功负荷 " + IT["P"] + " / kW")
save_grid(g, "27_联合网格", "27_联合网格_散点加边缘密度", multi=True,
w_mm=106, h_mm=104,
adjust=dict(left=0.135, bottom=0.11, right=0.95, top=0.94))
def jg_02():
"""主图用六边形分箱的联合图。"""
rng = np.random.default_rng(2702)
i = rng.gamma(9, 32, 5000)
u = 10.18 - 0.00095 * i + rng.normal(0, 0.05, 5000)
g = sns.JointGrid(data=pd.DataFrame({"电流": i, "电压": u}),
x="电流", y="电压", height=4.1)
g.plot_joint(sns.histplot, bins=28, cmap="Blues", cbar=False)
g.plot_marginals(sns.histplot, color=C5, edgecolor="0.4", linewidth=0.3,
bins=24)
g.ax_joint.set_xlabel("负荷电流 " + IT["I"] + " / A")
g.ax_joint.set_ylabel("母线电压 " + IT["U"] + " / kV")
save_grid(g, "27_联合网格", "27_联合网格_六边形分箱", multi=True,
w_mm=106, h_mm=104,
adjust=dict(left=0.135, bottom=0.11, right=0.95, top=0.94))
def jg_03():
"""主图回归、边缘箱线的联合图。"""
rng = np.random.default_rng(2703)
b = rng.uniform(0.15, 1.0, 350)
eta = 98.4 - 1.5 * (1 - b) ** 1.4 + rng.normal(0, 0.08, 350)
g = sns.JointGrid(data=pd.DataFrame({"负载率": b, "效率": eta}),
x="负载率", y="效率", height=4.1)
g.plot_joint(sns.regplot, color=C3,
scatter_kws={"s": 11, "edgecolor": "white", "linewidths": 0.2},
line_kws={"lw": 1.1})
g.plot_marginals(sns.boxplot, color=C5, width=0.4, linewidth=0.6,
fliersize=2.0)
g.ax_joint.set_xlabel("负载率 " + IT["beta"])
g.ax_joint.set_ylabel("效率 " + IT["eta"] + " / %")
save_grid(g, "27_联合网格", "27_联合网格_回归加箱线", multi=True,
w_mm=106, h_mm=104,
adjust=dict(left=0.135, bottom=0.11, right=0.95, top=0.94))
def jg_04():
"""主图密度等高线、边缘直方图的联合图。"""
rng = np.random.default_rng(2704)
v = rng.weibull(2.1, 500) * 9.5
pw = np.clip(0.42 * v ** 3, 0, 1650) + rng.normal(0, 90, 500)
g = sns.JointGrid(data=pd.DataFrame({"风速": v, "出力": np.clip(pw, 0, None)}),
x="风速", y="出力", height=4.1)
g.plot_joint(sns.kdeplot, fill=True, cmap="Blues", levels=8, thresh=0.05)
g.plot_marginals(sns.histplot, color=C5, edgecolor="0.4", linewidth=0.3,
bins=22, stat="density")
g.ax_joint.set_xlabel("风速 " + IT["v"] + " / (m/s)")
g.ax_joint.set_ylabel("风电出力 " + IT["P"] + " / kW")
save_grid(g, "27_联合网格", "27_联合网格_密度加直方", multi=True,
w_mm=106, h_mm=104,
adjust=dict(left=0.135, bottom=0.11, right=0.95, top=0.94))
def jg_05():
"""按方案分组的联合图:两组散点与边缘叠放。"""
rng = np.random.default_rng(2705)
rows = []
for plan, mu in [("方案 A", 0.872), ("方案 B", 0.889)]:
eff = rng.normal(mu, 0.03, 260)
rows.append(pd.DataFrame({"效率": eff,
"温升": 30 + 4200 * (eff - 0.80) + rng.normal(0, 1.6, 260),
"方案": plan}))
df = pd.concat(rows, ignore_index=True)
g = sns.JointGrid(data=df, x="效率", y="温升", hue="方案",
palette=[C5, C3], height=4.1)
g.plot_joint(sns.scatterplot, s=10, edgecolor="white", linewidth=0.2)
g.plot_marginals(sns.kdeplot, fill=True, alpha=0.4)
g.ax_joint.set_xlabel("系统综合效率 " + IT["eta"])
g.ax_joint.set_ylabel("绕组温升 " + IT["T"] + " / K")
legend_in(g.ax_joint, "upper left")
save_grid(g, "27_联合网格", "27_联合网格_按方案分组", multi=True,
w_mm=110, h_mm=104,
adjust=dict(left=0.135, bottom=0.11, right=0.95, top=0.94))
说明
- axes 级图 1181×944 px;figure 级多轴图按内容取 1100~1470 px 宽。
- 图内不放标题,图题由本文件的标题承担。
- 数据为 10 kV 配网、380 V 低压、50 Hz 量级的仿真算例,随机种子固定。

浙公网安备 33010602011771号