2

OK I am probably being thick, but how do I get just the graphs in the diagonal (top left to bottom right) in a nice row or 2x2 grid of:

import seaborn as sns; sns.set(style="ticks", color_codes=True)
iris = sns.load_dataset("iris")
g = sns.pairplot(iris, hue="species", palette="husl")

TO CLARIFY: I just want these graphs I do not care whether pairplot or something else is used.

schoon
  • 2,858
  • 3
  • 46
  • 78

5 Answers5

6

Doing this the seaborn-way would make use of a FacetGrid. For this we would need to convert the wide-form input to a long-form dataframe, such that every observation is a single row. This is done via pandas.melt.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset("iris")
df = pd.melt(iris, iris.columns[-1], iris.columns[:-1])

g = sns.FacetGrid(df, col="variable", hue="species", col_wrap=2)
g.map(sns.kdeplot, "value", shade=True)

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
1

Why do you even want to do that. The diagonal of the pairplot gives you the distplot of that feature. It will be more effective if you can plot the idividual distplots as subplot or mux them Ex:

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
import seaborn as sns

iris = load_iris()
iris = pd.DataFrame(data=np.c_[iris['data'], iris['target']],
                    columns=iris['feature_names'] + ['target'])

# Sort the dataframe by target
target_0 = iris.loc[iris['target'] == 0]
target_1 = iris.loc[iris['target'] == 1]
target_2 = iris.loc[iris['target'] == 2]

sns.distplot(target_0[['sepal length (cm)']], hist=False, rug=True)
sns.distplot(target_1[['sepal length (cm)']], hist=False, rug=True)
sns.distplot(target_2[['sepal length (cm)']], hist=False, rug=True)

sns.plt.show()

The output will be somewhat like this: Distplot[1]

Read more here : python: distplot with multiple distributions

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

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

g = sns.pairplot(iris, hue="species", palette="husl")
g.map_upper(hide_current_axis)
g.map_lower(hide_current_axis)

Output:

Pairplot Diagonal

Harish Vutukuri
  • 1,092
  • 6
  • 14
0
plt.subplots(2, 2)
for i, col in enumerate(iris.columns[:4]):
    plt.subplot(2, 2, i+1)
    sns.kdeplot(iris.loc[iris['species'] == 'setosa', col], shade=True, label='setosa')
    sns.kdeplot(iris.loc[iris['species'] == 'versicolor', col], shade=True, label='versicolor')
    sns.kdeplot(iris.loc[iris['species'] == 'virginica', col], shade=True, label='virginica')
    plt.xlabel('cm')
    plt.title(col)
    if i == 1:
        plt.legend(loc='upper right')
    else:
        plt.legend().remove()

plt.subplot_tool() # Opens a widget which allows adjusting plot aesthetics

enter image description here

KRKirov
  • 3,854
  • 2
  • 16
  • 20
  • Ah I did not see this answer before accepting the other one. Looks good although a little more complex. Thanks. – schoon Dec 07 '19 at 09:02
-2
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset("iris")

sns.pairplot(iris, hue="species", corner=True)

enter image description here

  • Your answer keeps the lower triangle plots there, but this isn't exactly what OP wanted. The question suggested to keep the diagonal plots only. – Nikita Shabankin Oct 17 '22 at 18:24