Search code examples
pythonseabornfacet-grid

Making seaborn.PairGrid() look like pairplot()


In the example below, how do I use seaborn.PairGrid() to reproduce the plots created by seaborn.pairplot()? Specifically, I'd like the diagonal distributions to span the vertical axis. Markers with white borders etc... would be great too. Thanks!

import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset('iris')

# pairplot() example
g = sns.pairplot(iris, kind='scatter', diag_kind='kde')
plt.show()

# PairGrid() example
g = sns.PairGrid(iris)
g.map_diag(sns.kdeplot)
g.map_offdiag(plt.scatter)
plt.show()

enter image description hereenter image description here


Solution

  • This is quite simple to achieve. The main differences between your plot and what pairplot does are:

    • the use of the diag_sharey parameter of PairGrid
    • using sns.scatterplot instead of plt.scatter

    With that, we have:

    iris = sns.load_dataset('iris')
    g = sns.PairGrid(iris, diag_sharey=False)
    g.map_diag(sns.kdeplot)
    g.map_offdiag(sns.scatterplot)
    

    enter image description here