首页 > 解决方案 > histplot 的不同分箱作为 JoinGrid (x,y) 边际图

问题描述

我有一个像这样的熊猫数据框:

日期 重量 星期 星期几
0 2017-11-13 76.1 2017 11 13 46 0
1 2017-11-14 76.2 2017 11 14 46 1
2 2017-11-15 76.6 2017 11 15 46 2
3 2017-11-16 77.1 2017 11 16 46 3
4 2017-11-17 76.7 2017 11 17 46 4
... ... ... ... ... ... ... ...

我创建了一个 JoinGrid:

g = sns.JointGrid(data=df,
    x="Date",
    y="Weight",
    marginal_ticks=True,
    height=6, 
    ratio=2, 
    space=.05)

然后是定义的联合和边缘图:

g.plot_joint(sns.scatterplot,
        hue=df["Year"], 
        alpha=.4,
        legend=True)
g.plot_marginals(sns.histplot, 
    multiple="stack", 
    bins=20,
    hue=df["Year"])

结果是这样的。

结果

现在的问题是:“是否可以为导致 x 和 y 边缘图的两个 histplot 指定不同的分箱?”

标签: pythonmatplotlibseaborn

解决方案


我认为没有内置的方法可以做到这一点,您可以使用您选择的绘图功能直接在边缘轴上绘图,如下所示:

penguins = sns.load_dataset('penguins')

data = penguins
x_col = "bill_length_mm"
y_col = "bill_depth_mm"
hue_col = "species"

g = sns.JointGrid(data=data, x=x_col, y=y_col, hue=hue_col)
g.plot_joint(sns.scatterplot)

# top marginal
sns.histplot(data=data, x=x_col, hue=hue_col, bins=5, ax=g.ax_marg_x, legend=False, multiple='stack')
# right marginal
sns.histplot(data=data, y=y_col, hue=hue_col, bins=40, ax=g.ax_marg_y, legend=False, multiple='stack')

在此处输入图像描述


推荐阅读