Seaborn的jointplot将颜色边缘图分别绘制

5
我希望能够为每个变量单独设置边际图的颜色。
d1 = np.random.normal(10,1,100)
d2 = np.random.gamma(1,2,100)
col1 = sns.color_palette()[0]
col2 = sns.color_palette()[1]
col3 = sns.color_palette()[2]

jp = sns.jointplot(d1, d2, kind="hex", annot_kws=dict(stat="r"), joint_kws=dict(bins='log'))
ax = jp.ax_joint
ax.plot(ax.get_xlim(), ax.get_ylim(), ls="--", c=".3", label="1:1")

输出结果: Example Plot

我想要分别为每个边缘图上色。但是当我为边缘轴分配参数时,它们会将相同的参数用于两个边缘图。

jp = sns.jointplot(d1, d2, kind="hex", annot_kws=dict(stat="r"), joint_kws=dict(bins='log'), marginal_kws=dict(hist_kws= {'color': col2}))
ax = jp.ax_joint
ax.plot(ax.get_xlim(), ax.get_ylim(), ls="--", c=".3", label="1:1")

Plot Colouring the Marginal Distributions

我可以为“facecolors”着色,但无法为轴本身着色。非常感谢任何帮助!

jp = sns.jointplot(d1, d2, kind="hex", annot_kws=dict(stat="r"), joint_kws=dict(bins='log'), marginal_kws=dict(hist_kws= {'color': col2}))
ax = jp.ax_joint
ax.plot(ax.get_xlim(), ax.get_ylim(), ls="--", c=".3", label="1:1")

# new code
jp.ax_marg_x.set_facecolor(col1)
jp.ax_marg_y.set_facecolor(col3)

Coloring the facecolors individually

1个回答

5
你可以通过访问两个边缘图的补丁并更改它们的面颜色来完成。
import seaborn as sns
import numpy as np

# define data here

jp = sns.jointplot(d1, d2, kind="hex", annot_kws=dict(stat="r"), joint_kws=dict(bins='log'), color=col2)
ax = jp.ax_joint
ax.plot(ax.get_xlim(), ax.get_ylim(), ls="--", c=".3", label="1:1")

for patch in jp.ax_marg_x.patches:
    patch.set_facecolor(col1)

for patch in jp.ax_marg_y.patches:
    patch.set_facecolor(col3) 

enter image description here


抱歉,我意识到有一个新问题,但是我认为这个答案很棒! - Tommy Lees
@TommyLees:我建议您发布一个新问题,因为这可能对许多人非常重要。同时,我会继续努力回答,并尽快让您知道。 - Sheldore
非常感谢,我在这里已经这样做了 - https://stackoverflow.com/q/55285704/9940782 - Tommy Lees
@TommyLees:你所说的注释,是指最佳拟合直线的方程吗? - Sheldore
@TommyLees:你试过我在你另一个帖子中评论的解决方案了吗? - Sheldore
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接