3

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?

ajwood
  • 18,227
  • 15
  • 61
  • 104

1 Answers1

8

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

bnaecker
  • 6,152
  • 1
  • 20
  • 33
  • It looks like this puts all of the subplots on the same range (https://i.stack.imgur.com/qpdNF.png). Can this be tweaked to use a per-subplot plot range? – ajwood Jan 05 '18 at 22:43
  • Sure, instead of making `mn` and `mx` dependent on the x- and y-data, just compute them by doing `mn = min(plt.gca().axis())` and `mx = max(plt.gca().axis())`. This will create a line that goes from the minimal to maximal value of the extent of each of the axes. – bnaecker Jan 05 '18 at 22:45
  • Even still that doesn't seem to do it: https://i.stack.imgur.com/UEeVw.png – ajwood Jan 06 '18 at 21:35
  • You'll have to play with the function to get exactly the desired line. You have access to the data and the axis, so use whatever limits you want and touch up the axis however you want. – bnaecker Jan 06 '18 at 21:51
  • I'm also having the same problem did you manage to figure this out? I'd like to plot the identity line without changing the scale in the subplots – R Thompson Feb 04 '19 at 16:55
  • @RThompson See my previous comment. Instead of maxing the min and max of the identity line depend on the *data*, just draw the line between the min and max of the *axes*. As in, `mn = min(plt.gca().axis())` and `mx = max(plt.gca().axis())`. – bnaecker Feb 04 '19 at 16:59
  • I attempted to do this but now the lines extend way beyond those of the original image – R Thompson Feb 04 '19 at 17:16
  • @RThompson The code in my comment will take the min/max across both axes, while you may actually want to keep each axis separate. – bnaecker Feb 04 '19 at 18:45