cattt84
cattt84

Reputation: 951

How to change the colours of the lateral kde plots of seaborn jointplot

I want to create a sns.jointplot that uses the standard colormap "Greens". The output of this standard colorcycle is too light for the lateral kde estimated densities. I would prefer the darker colours you get when you use the "BuGn_r" colormap.

Is there an easy way to get them darker?

import matplotlib.pyplot as plt
import numpy as np
import pandas as pn
import seaborn as sns

iris = sns.load_dataset("iris")
with sns.axes_style("white"):
    sns.set_palette("BuGn_r")
    g2 = sns.jointplot("sepal_width", "petal_length", data=iris,
                kind="kde", space=0)

with sns.axes_style("white"):
    sns.set_palette("Greens")
    g3 = sns.jointplot("sepal_width", "petal_length", data=iris,
                    kind="kde", space=0)

See the output below:

enter image description here

Upvotes: 0

Views: 3213

Answers (2)

mwaskom
mwaskom

Reputation: 48992

Use the color= parameter of jointplot.

Upvotes: 0

tmdavison
tmdavison

Reputation: 69106

You can change the colour of the line and shaded region after you create it with kdeplot. The marginal axes can be accessed from g3, by g3.ax_marg_x and g3.ax_marg_y.

You can change the colour of the line with:

g3.ax_marg_x.lines[0].set_color()

And the colour of the shaded region with:

g3.ax_marg_x.collections[0].set_facecolor()

Of course, you could set these to any valid matplotlib colour. Or, to get the exact colours that you use in your first plot, you could also use get_color and get_facecolor on the g2 marginals.

You can see it all in action in the script below:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pn
import seaborn as sns

iris = sns.load_dataset("iris")
with sns.axes_style("white"):
    sns.set_palette("BuGn_r")
    g2 = sns.jointplot("sepal_width", "petal_length", data=iris,
                kind="kde", space=0)

    # Find out the face and line colours that BuGn_r uses
    facecolor = g2.ax_marg_x.collections[0].get_facecolor()
    linecolor = g2.ax_marg_x.lines[0].get_color()

    print facecolor 
    # [[ 0.0177624   0.4426759   0.18523645  0.25      ]]

    print linecolor
    # (0.017762399946942051, 0.44267590116052069, 0.18523645330877866)    

with sns.axes_style("white"):
    sns.set_palette("Greens")
    g3 = sns.jointplot("sepal_width", "petal_length", data=iris,
                    kind="kde", space=0)

    # Change the facecolor of the shaded region under the line
    g3.ax_marg_x.collections[0].set_facecolor(facecolor)
    g3.ax_marg_y.collections[0].set_facecolor(facecolor)

    # And change the line colour.
    g3.ax_marg_x.lines[0].set_color(linecolor)
    g3.ax_marg_y.lines[0].set_color(linecolor)

plt.show()

"BuGn_r"

enter image description here

"Greens" with modified colours

enter image description here

Upvotes: 1

Related Questions