seaborn icon indicating copy to clipboard operation
seaborn copied to clipboard

Add parameter `col_wrap` to `pairplot`

Open albertotb opened this issue 4 years ago • 1 comments

Currently, a very convenient way to plot an scatter plot of all the columns against one in a pd.DataFrame is

import seaborn as sns

iris = sns.load_dataset('iris')
sns.pairplot(iris, 
             x_vars=['sepal_length', 'sepal_width', 'petal_length'] , 
             y_vars=['petal_width'])

However, all the plots are in a single row, which is a problem if there are many x_vars . A possible solution is to add a col_wrap parameter that wraps the plot matrix.

I know that for this case the name of the function pairplot does not make much sense, but since the parameters x_vars and y_vars make it flexible enough to do this kind of plots I think it would be a good addition.

albertotb avatar Jun 03 '20 15:06 albertotb

This is a good idea.

In the meantime, here's a clever workaround:

iris = sns.load_dataset('iris')
x_vars = ['sepal_length', 'sepal_width', 'petal_length']
y_var = "petal_width"

g = sns.FacetGrid(pd.DataFrame(x_vars), col=0, col_wrap=2, sharex=False)
for ax, x_var in zip(g.axes, x_vars):
    sns.scatterplot(data=iris, x=x_var, y=y_var, ax=ax)
g.tight_layout()

image

mwaskom avatar Jun 06 '21 00:06 mwaskom

In the new objects interface the Plot.pair operator has wrapping (across rows or columns):

(
    so.Plot(mpg, y="mpg")
    .pair(x=["displacement", "weight", "horsepower", "cylinders"], wrap=2)
    .add(so.Dots())
)

image

I'm going to close this issue, as supporting wrapping in PairGrid itself will be tricker. Not ruling it out, but also not planning to do it in any particular interval.

mwaskom avatar Sep 14 '22 00:09 mwaskom