Skip to content Skip to sidebar Skip to footer

Have Gradient Colours In Sns.pairplot For One Column Of Dataframe So That I Can See Which Datapoints Are Connected To Each Other

I am quite easily able to produce a seaborn pairplot with: import seaborn as sns import matplotlib.pyplot as plt import pandas as pd import numpy as np f, axes = plt.subplots(1, 1

Solution 1:

You could loop through the off-diagonal axes. And then change the colormap and color values for the scatterplots:

import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import seaborn as sns
import pandas as pd
import numpy as np

def myFunc(x):
    myReturn = x + 1 * np.random.random(x.shape[0])
    return myReturn

np.random.seed(1)
a = np.arange(0, 10, 0.1)
np.random.rand()

b = myFunc(a)
c = a * np.sin(a)
df = pd.DataFrame({'a': a, 'b': b, 'c': c})

cmap = LinearSegmentedColormap.from_list('blue-yellow', ['gold', 'lightblue', 'darkblue'])  # plt.get_cmap('viridis_r')
g = sns.pairplot(df, corner=True)
for ax in g.axes.flat:
    if ax is not None and not ax in g.diag_axes:
        for collection in ax.collections:
            collection.set_cmap(cmap)
            collection.set_array(df['a'])
plt.show()

changing the scatterplots of a sns.pairplot


Solution 2:

I think the much easier thing to do here is to pass the dataframe index as a hue variable. Since you don't want to (naively) apply to the histogram, pass it directly to the off-diagonal plots:

sns.pairplot(
    df,
    corner=True,
    diag_kws=dict(color=".6"),
    plot_kws=dict(
        hue=df.index,
        palette="blend:gold,dodgerblue",
    ),
)

enter image description here


Post a Comment for "Have Gradient Colours In Sns.pairplot For One Column Of Dataframe So That I Can See Which Datapoints Are Connected To Each Other"