Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 415
schoon Avatar asked Nov 24 '25 23:11

schoon


1 Answers

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

like image 175
ImportanceOfBeingErnest Avatar answered Nov 27 '25 13:11

ImportanceOfBeingErnest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!