Seaborn jointplot颜色边缘图分别

时间:2019-03-21 15:54:22

标签: python python-3.x matplotlib seaborn

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

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 个答案:

答案 0 :(得分: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