05 核密度图 kdeplot
这类图是什么
核密度曲线,传 x 与 y 得到二维密度等高线。
看什么:看分布形状与密度中心,二维版能替代散点看聚集位置。
5 个案例
05-1 单组密度的填充分布:不需要直方图的柱状即可看形状

05-2 三类负荷的密度曲线:交叠区即难以区分的区间

05-3 二维密度等高线:负荷与气温的联合分布
kdeplot 传 x 与 y 就得到二维核密度,fill 后是等高线填色,比散点更能看出"密度中心在哪",也不受样本量堆叠影响。

05-4 母线电压的密度分布:与限值线对照
密度曲线与两条限值竖线的相对位置,直接给出"有多少概率落在允许范围外"。

05-5 两方案效率的密度重叠:判断提升是否显著

代码
5 个函数取自 code/第二批_分布图261107.py。关键约束:不调用 sns.set_theme(),否则 font.family 被改成 Arial,中文会变方框;版式由 code/公共配置261107.py 里的 setup_cn_style() 与 save() / save_grid() 统一接管。
# 摘自 code/第二批_分布图261107.py
def kd_01():
"""单组密度的填充分布:不需要直方图的柱状即可看形状。"""
fig, ax = new_fig(left=0.19, bottom=0.21)
rng = np.random.default_rng(501)
d = np.concatenate([rng.normal(430, 52, 700), rng.normal(665, 58, 560)])
sns.kdeplot(x=d, fill=True, color=C1, alpha=0.35, lw=1.1, ax=ax)
ax.set_xlabel("有功负荷 " + IT["P"] + " / kW")
ax.set_ylabel("概率密度")
ax.set_xlim(240, 880)
save(fig, "05_核密度图", "05_核密度图_日负荷双峰")
def kd_02():
"""三类负荷的密度曲线:交叠区即难以区分的区间。"""
fig, ax = new_fig(left=0.19, bottom=0.21)
rng = np.random.default_rng(502)
rows = []
for kind, mu, sd in [("工业", 620, 90), ("商业", 480, 70), ("居民", 360, 55)]:
rows.append(pd.DataFrame({"负荷": rng.normal(mu, sd, 600), "类型": kind}))
df = pd.concat(rows, ignore_index=True)
sns.kdeplot(data=df, x="负荷", hue="类型", fill=True, palette=PALETTE3,
alpha=0.35, lw=1.0, ax=ax)
ax.set_xlabel("有功负荷 " + IT["P"] + " / kW")
ax.set_ylabel("概率密度")
ax.set_xlim(180, 920)
legend_in(ax, "upper right")
save(fig, "05_核密度图", "05_核密度图_三类负荷曲线")
def kd_03():
"""二维密度等高线:负荷与气温的联合分布。
kdeplot 传 x 与 y 就得到二维核密度,fill 后是等高线填色,
比散点更能看出"密度中心在哪",也不受样本量堆叠影响。
"""
fig, ax = new_fig(left=0.19, bottom=0.21)
rng = np.random.default_rng(503)
t = rng.normal(28, 8, 1500)
p = 380 + 9.5 * (t - 20) + rng.normal(0, 55, 1500)
df = pd.DataFrame({"气温": t, "负荷": p})
sns.kdeplot(data=df, x="气温", y="负荷", fill=True, cmap="Blues",
levels=12, thresh=0.05, ax=ax)
ax.set_xlabel("气温 " + IT["T"] + " / °C")
ax.set_ylabel("有功负荷 " + IT["P"] + " / kW")
ax.set_xlim(0, 46)
save(fig, "05_核密度图", "05_核密度图_气温负荷联合密度")
def kd_04():
"""母线电压的密度分布:与限值线对照。
密度曲线与两条限值竖线的相对位置,直接给出"有多少概率落在允许范围外"。
"""
fig, ax = new_fig(left=0.19, bottom=0.21)
rng = np.random.default_rng(504)
u = rng.normal(10.02, 0.16, 1200)
sns.kdeplot(x=u, fill=True, color=C1, alpha=0.35, lw=1.1, ax=ax)
ax.axvline(9.30, color=C2, lw=0.8, ls="--")
ax.axvline(10.70, color=C2, lw=0.8, ls=":")
ax.set_xlabel("母线电压 " + IT["U"] + " / kV")
ax.set_ylabel("概率密度")
ax.set_xlim(9.35, 10.70)
save(fig, "05_核密度图", "05_核密度图_母线电压与限值")
def kd_05():
"""两方案效率的密度重叠:判断提升是否显著。"""
fig, ax = new_fig(left=0.19, bottom=0.21)
rng = np.random.default_rng(505)
rows = []
for plan, mu, sd in [("方案 A", 0.872, 0.030), ("方案 B", 0.889, 0.032)]:
rows.append(pd.DataFrame({"效率": rng.normal(mu, sd, 900), "方案": plan}))
df = pd.concat(rows, ignore_index=True)
sns.kdeplot(data=df, x="效率", hue="方案", fill=True, palette=[C5, C3],
alpha=0.45, lw=1.0, ax=ax)
ax.set_xlabel("系统综合效率 " + IT["eta"])
ax.set_ylabel("概率密度")
ax.set_xlim(0.76, 1.00)
legend_in(ax, "upper left")
save(fig, "05_核密度图", "05_核密度图_两方案效率重叠")
说明
- axes 级图 1181×944 px;figure 级多轴图按内容取 1100~1470 px 宽。
- 图内不放标题,图题由本文件的标题承担。
- 数据为 10 kV 配网、380 V 低压、50 Hz 量级的仿真算例,随机种子固定。
浙公网安备 33010602011771号