【问题标题】:Seaborn jointplot colour marginal plots separatelySeaborn 联合图分别绘制颜色边缘图
【发布时间】:2019-03-21 15:54:21
【问题描述】:

我想为每个变量分别着色我的边缘图。

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")

产生:

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

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")

我可以为“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)

【问题讨论】:

    标签: python python-3.x matplotlib seaborn


    【解决方案1】:

    您可以通过访问两个边缘图的补丁并更改它们的面部颜色来做到这一点。

    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) 
    

    【讨论】:

    • 我如何让注释显示?我添加了:annot_kws=dict(stat="r") 但它没有出现在情节的任何地方?
    • 对不起,我意识到了新问题,但只是想到了!很棒的答案
    • @TommyLees:我建议对此发布一个新问题,因为它可能对许多人具有潜在的重要性。同时,我会尽快解答并通知您。
    • 非常感谢我在这里做了 - stackoverflow.com/q/55285704/9940782
    • @TommyLees:注解是指最佳拟合直线方程,对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多