Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot lower triangle in a seaborn Pairgrid

Tags:

python

seaborn

I'm a bit struggling with seaborn Pairgrid.

Let's say I have a Pairgrid like this:

enter image description here

As you can see, the upper triangle plots mirror the lower triangle ones. I'd like to be able to plot only the lower triangle plots, but I found no easy way so far to do it. Can you help me?

like image 223
user3722235 Avatar asked Dec 04 '15 11:12

user3722235


2 Answers

With seaborn >= 0.9.1:

import seaborn as sns
iris = sns.load_dataset("iris")
sns.pairplot(iris, corner=True)

enter image description here

like image 184
mwaskom Avatar answered Oct 16 '22 07:10

mwaskom


this is basically the same as the accepted answer, but uses the official methods from seaborn.PairGrid:

import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="ticks")
iris = sns.load_dataset("iris")

def hide_current_axis(*args, **kwds):
    plt.gca().set_visible(False)

g = sns.pairplot(iris)
g.map_upper(hide_current_axis)

hiding the lower half is also easy:

g.map_lower(hide_current_axis)

or hiding the diagonal:

g.map_diag(hide_current_axis)

alternatively, just use the PairGrid directly for more control:

g = sns.PairGrid(iris, hue='species', diag_sharey=False)
g.map_lower(sns.scatterplot, alpha=0.3, edgecolor='none')
g.map_diag(sns.histplot, multiple="stack", element="step")
g.map_upper(hide_current_axis)

which gives:

money shot

Since Seaborn 0.9.1 there has been a corner=True setting, allowing the above to be changed to:

import seaborn as sns
iris = sns.load_dataset("iris")
sns.pairplot(
  iris, hue='species', corner=True, 
  plot_kws=dict(alpha=0.3, edgecolor='none'),
  diag_kind="hist",
  diag_kws=dict(multiple="stack", edgecolor='none'),
)
like image 17
Sam Mason Avatar answered Oct 16 '22 05:10

Sam Mason