简体   繁体   中英

How can I plot identity lines on a seaborn pairplot?

I'm using Seaborn's pairplot:

g = sns.pairplot(df)

在此处输入图片说明

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

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.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM