12 小提琴图 violinplot
12 小提琴图 violinplot
这类图是什么
核密度镜像成小提琴,inner 可内嵌箱线或四分位。
看什么:比箱线多看到峰数、偏斜与尾部厚度。
5 个案例
12-1 各月光伏出力的小提琴:seaborn 版可内嵌箱线

12-2 各馈线电流小提琴:轮廓宽度即密度

12-3 四季风速小提琴:尾部厚度可见

12-4 四种调度策略的效率分布

12-5 三相电压不平衡度的分布对比

代码
5 个函数取自 code/第三批_类别图261107.py。关键约束:不调用 sns.set_theme(),否则 font.family 被改成 Arial,中文会变方框;版式由 code/公共配置261107.py 里的 setup_cn_style() 与 save() / save_grid() 统一接管。
# 摘自 code/第三批_类别图261107.py
def vi_01():
"""各月光伏出力的小提琴:seaborn 版可内嵌箱线。"""
fig, ax = new_fig(left=0.19)
rng = np.random.default_rng(1201)
rows = []
for m, mu in [("1 月", 210), ("4 月", 396), ("7 月", 486), ("10 月", 320)]:
rows.append(pd.DataFrame({"出力": rng.normal(mu, mu * 0.16, 150), "月份": m}))
df = pd.concat(rows, ignore_index=True)
sns.violinplot(data=df, x="月份", y="出力", hue="月份", palette=PALETTE4,
inner="box", linewidth=0.5, width=0.78, legend=False, ax=ax,
saturation=0.85)
ax.set_xlabel("月份")
ax.set_ylabel("日均出力 " + IT["P"] + " / kW")
ax.set_ylim(-60, 780)
save(fig, "12_小提琴图", "12_小提琴图_各月光伏出力")
def vi_02():
"""各馈线电流小提琴:轮廓宽度即密度。"""
fig, ax = new_fig(left=0.19)
rng = np.random.default_rng(1202)
rows = []
for fd, k, th in [("L1", 9, 32), ("L2", 11, 26), ("L3", 8, 46), ("L4", 13, 22)]:
rows.append(pd.DataFrame({"电流": rng.gamma(k, th, 150), "馈线": fd}))
df = pd.concat(rows, ignore_index=True)
sns.violinplot(data=df, x="馈线", y="电流", hue="馈线", palette=PALETTE4,
inner="quartile", linewidth=0.5, width=0.78, legend=False,
ax=ax, saturation=0.85)
ax.set_xlabel("馈线")
ax.set_ylabel("电流有效值 " + IT["I"] + " / A")
ax.set_ylim(0, 620)
save(fig, "12_小提琴图", "12_小提琴图_各馈线电流")
def vi_03():
"""四季风速小提琴:尾部厚度可见。"""
fig, ax = new_fig(left=0.19)
rng = np.random.default_rng(1203)
rows = []
for s, k, lam in [("春季", 2.4, 9.2), ("夏季", 1.9, 6.8),
("秋季", 2.2, 7.6), ("冬季", 2.7, 10.4)]:
rows.append(pd.DataFrame({"风速": lam * rng.weibull(k, 200), "季节": s}))
df = pd.concat(rows, ignore_index=True)
sns.violinplot(data=df, x="季节", y="风速", hue="季节", palette=PALETTE4,
inner="box", linewidth=0.5, width=0.78, legend=False, ax=ax,
saturation=0.85)
ax.set_xlabel("季节")
ax.set_ylabel("风速 " + IT["v"] + " / (m/s)")
ax.set_ylim(0, 28)
save(fig, "12_小提琴图", "12_小提琴图_四季风速")
def vi_04():
"""四种调度策略的效率分布。"""
fig, ax = new_fig(left=0.21)
rng = np.random.default_rng(1204)
rows = []
for p, mu, sd in [("策略一", 0.860, 0.022), ("策略二", 0.890, 0.018),
("策略三", 0.910, 0.015), ("策略四", 0.920, 0.012)]:
rows.append(pd.DataFrame({"效率": rng.normal(mu, sd, 160), "策略": p}))
df = pd.concat(rows, ignore_index=True)
sns.violinplot(data=df, x="策略", y="效率", hue="策略", palette=PALETTE4,
inner="box", linewidth=0.5, width=0.75, legend=False, ax=ax,
saturation=0.85)
ax.set_xlabel("调度策略")
ax.set_ylabel("综合效率 " + IT["eta"])
ax.set_ylim(0.80, 0.96)
save(fig, "12_小提琴图", "12_小提琴图_各策略效率")
def vi_05():
"""三相电压不平衡度的分布对比。"""
fig, ax = new_fig(left=0.20)
rng = np.random.default_rng(1205)
rows = []
for n, k, th in [("节点 A", 5.5, 0.42), ("节点 B", 7.2, 0.36), ("节点 C", 4.4, 0.50)]:
rows.append(pd.DataFrame({"不平衡度": rng.gamma(k, th, 180), "节点": n}))
df = pd.concat(rows, ignore_index=True)
sns.violinplot(data=df, x="节点", y="不平衡度", hue="节点", palette=PALETTE3,
inner="box", linewidth=0.5, width=0.72, legend=False, ax=ax,
saturation=0.85)
ax.axhline(2.0, color=C2, lw=0.8, ls="--")
ax.set_xlabel("监测节点")
ax.set_ylabel("电压不平衡度 / %")
ax.set_ylim(0, 12)
save(fig, "12_小提琴图", "12_小提琴图_三相不平衡度")
说明
- axes 级图 1181×944 px;figure 级多轴图按内容取 1100~1470 px 宽。
- 图内不放标题,图题由本文件的标题承担。
- 数据为 10 kV 配网、380 V 低压、50 Hz 量级的仿真算例,随机种子固定。

浙公网安备 33010602011771号