You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除小提琴图(violin plot)的黑色轮廓?

移除组合图中小提琴图的黑色轮廓

Seaborn 实现方案

如果使用Seaborn绘制小提琴图+箱线图的组合,只需在violinplot()中设置linewidth=0或edgecolor="none"即可移除小提琴的黑色轮廓,同时箱线图保持正常显示:

import seaborn as sns
import matplotlib.pyplot as plt

# 加载示例数据集
tips = sns.load_dataset("tips")

# 绘制无轮廓的小提琴图
sns.violinplot(x="day", y="total_bill", data=tips, linewidth=0, edgecolor="none")
# 叠加箱线图(缩小宽度避免遮挡)
sns.boxplot(x="day", y="total_bill", data=tips, width=0.1, color="white")

plt.show()
  • linewidth=0直接将轮廓线宽设为0,edgecolor="none"将轮廓颜色设为透明,两种方式均可实现需求;
  • 箱线图通过width参数缩小宽度,避免遮挡小提琴图主体。

Matplotlib 原生实现方案

如果使用Matplotlib原生函数绘制组合图,可通过获取小提琴图的路径集合,手动设置其边缘样式:

import matplotlib.pyplot as plt
import numpy as np

# 生成示例数据
np.random.seed(10)
data = [np.random.normal(0, std, 100) for std in range(1, 5)]

# 绘制小提琴图并获取返回对象
violins = plt.violinplot(data)
# 遍历移除所有小提琴的轮廓
for pc in violins['bodies']:
    pc.set_edgecolor('none')
    # 也可使用 pc.set_linewidth(0) 达到同样效果

# 叠加箱线图
plt.boxplot(data, widths=0.1)

plt.show()

Matplotlib的violinplot()返回字典,其中'bodies'对应每个小提琴的填充区域,通过修改该对象的边缘属性即可移除黑色轮廓。

内容的提问来源于stack exchange,提问作者user21877030

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 15:03:09