I am plotting a dataframe as a pairplot and fitting lowess lines using regplot in seaborn. I have used hue = "variable", but would like to see the lowess line fitted on the whole data (non-hue) in the same pairplot as well. I would appreciate any help or tips on this question.
pg = sns.pairplot(df, hue="variable", plot_kws={'alpha':0.015})
a = pg.map(sns.regplot, lowess=True, scatter_kws={'alpha':0.03})
Let's replace
sns.regplotby a custom function, e.g.plot_extra.pg.mapwill callplot_extraonce for each hue value for each subplot. It will pass the following parameters:label: the name of the hue value of this callxandy: the columns for this subplot, restricted to the current hue valuepg.mapTo draw the
regplotfor the complete dataframe, the dataframe can be given as extra parameter. To prevent that the same function will be executed again and again for each of the hue values, the function could test on the label and only go further for one specific label. The.nameofxandycan be used to indicate which columns to plot..map()will call for each of the subplots, while.map_offdiag()will be restricted to the subplots that aren't on the diagonal.The legend can be updated: extract the information from the existing
pairplotlegend and add a reference to the regplot line.Here is an example using the standard iris data set.