13

I'm trying to look at a Seaborn pairplot for two different classes of variables and I'd like to see KDEs on the offdiagonals instead of scatterplots. The documentation has instructions on how to do a KDE for all of the data, but I want to see separate KDEs for each subclass of data. Suggestions welcome!

My code looks something like this:

plot = sns.pairplot(
    df,
    vars=labels,
    hue='has_accident',
    palette='Set1',
    diag_kind='kde',
)

which results in:

enter image description here

As you can see the data are sufficiently dense that it is difficult to see the difference in the red and blue data on the off diagonal.

2
  • 1
    FWIW the KDE, which is based on a gaussian model, appears poorly suited for your data.
    – mwaskom
    Feb 4, 2017 at 15:21
  • fair point @mwaskom. i'm going to be messing around with a lot of different feature permutations, others of which i anticipate to be more spherical. thanks for sharing!
    – dino
    Feb 4, 2017 at 18:52

1 Answer 1

23

You possibly mean something like this:

import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset("iris")

g = sns.PairGrid(iris, hue="species", hue_kws={"cmap": ["Blues", "Greens", "Reds"]})
g = g.map_diag(sns.kdeplot, lw=3)
g = g.map_offdiag(sns.kdeplot, lw=1)

plt.show()

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.