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

如何移除Seaborn Jointplot边际直方图的Bin叠加线条?

解决Jointplot边际直方图白色Bin线条问题

你可以通过两种方式移除边际直方图的白色Bin线条,同时保留小提琴图:

方法1:创建Jointplot时直接指定参数

在marginal_kws中添加"edgecolor": "none",直接让直方图的Bin边缘线条不显示,这是最直接的方案。此外注意要给小提琴图指定目标轴ax=g.ax_marg_y,避免画到主图区域:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

column1 = "F"
array1 = np.random.randn(400)
column2 = "R"
array2 = np.random.randn(400)

df = pd.DataFrame(np.column_stack((array1, array2)), columns=[column1, column2])
viol = pd.DataFrame(np.concatenate((array1, array2)), columns=["val"])
viol["Sim"] = np.concatenate((np.repeat("F", np.size(array1)), np.repeat("R", np.size(array2))))
viol["xx"] = np.repeat("1", np.size(array1) * 2)

# 新增edgecolor参数隐藏Bin线条
g = sns.jointplot(x=column1,y=column2,data=df,ratio=3,
                  marginal_kws={"kde": False, "color": "white", "element": "poly", "edgecolor": "none" })
sns.regplot(x=column1,y=column2,data=df,ax=g.ax_joint)
# 指定小提琴图绘制到边际y轴
sns.violinplot(data=viol, x="xx", y="val", hue="Sim", ax=g.ax_marg_y)
g.ax_marg_y.get_legend().remove()
sns.despine()
g.fig.colorbar(g.ax_joint.collections[0], ax=[g.ax_joint, g.ax_marg_y, g.ax_marg_x], orientation="vertical")

g.ax_marg_x.remove()
g.ax_marg_y.spines["bottom"].set_visible(False)

plt.show()

方法2:事后修改边际轴的直方图元素

如果已经创建了jointplot对象,可以遍历边际轴中的直方图patch,将边缘颜色设为透明:

# 在创建完g对象后执行这段代码
for patch in g.ax_marg_y.patches:
    patch.set_edgecolor('none')
for patch in g.ax_marg_x.patches:
    patch.set_edgecolor('none')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 17:01:17