Search code examples
pythonseabornvisualization

How do I get the diagonal of sns.pairplot?


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.


Solution

  • 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