Tommy Lees
Tommy Lees

Reputation: 1373

Seaborn jointplot colour marginal plots separately

I want to colour my marginal plots separately for each variable.

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

Produces: Example Plot

I want to colour each marginal plot separately. But when I assign arguments to the marginal axes they colour both marginal plots with the same arguments .

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

I can colour the 'facecolors' but not the axes themselves. Any help very much appreciated!

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

Upvotes: 5

Views: 1602

Answers (1)

Sheldore
Sheldore

Reputation: 39072

You can do it by accessing the patches of two marginal plots and changing their face colors.

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

Upvotes: 5

Related Questions