Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I plot identity lines on a seaborn pairplot?

Tags:

python

seaborn

I'm using Seaborn's pairplot:

g = sns.pairplot(df)

enter image description here

Is it possible to draw identity lines on each of the scatter plots?

like image 966
ajwood Avatar asked Sep 20 '25 08:09

ajwood


1 Answers

Define a function which will plot the identity line on the current axes, and apply it to the off-diagonal axes of the grid using PairGrid.map_offdiag() method.

For example:

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

def plot_unity(xdata, ydata, **kwargs):
    mn = min(xdata.min(), ydata.min())
    mx = max(xdata.max(), ydata.max())
    points = np.linspace(mn, mx, 100)
    plt.gca().plot(points, points, color='k', marker=None,
            linestyle='--', linewidth=1.0)

ds = sns.load_dataset('iris')
grid = sns.pairplot(ds)
grid.map_offdiag(plot_unity)

This makes the following plot on my setup. You can tweak the kwargs of the plot_unity function to style the plot however you want.

enter image description here

like image 98
bnaecker Avatar answered Sep 21 '25 20:09

bnaecker