简体   繁体   中英

Changing font color in each PairGrid Seaborn plot based on pearsonr value:

I am plotting using seaborn and I am using seaborn.PairGrid function (code below).

I am able calculate pearsonr value and set my color list.

Is there way to use to change each pairplot color based on the pearsonr correlation values?

global my_colors
my_colors = []

def corrfunc(x, y, **kws):
    r = stats.pearsonr(x, y)[0] ** 2
    ax = plt.gca()
    ax.annotate("r$^2$ = {:.2f}".format(r),
                 xy=(.1, .9), xycoords=ax.transAxes)
    if r > 0.6:
       my_colors.append('g')
    elif r < 0.6:
       my_colors.append('r')
    return r

df = pd.read_excel(Inp_Filename, sheetname='IC_Data')

IC_Plot = sns.PairGrid(df)
IC_Plot.map_offdiag(corrfunc)

IC_Plot.map_offdiag(sns.regplot, color='g') 
#Need to change color of each 
#pairplot based on the pearsonr value

IC_Plot.map_offdiag(plt.scatter, s=10)

IC_Plot.savefig("IC_Pair.png")

You may call the plotting method from within the function that determines the color. In that way the color can simply be given as an argument to the plotting function.

import seaborn.apionly as sns
import matplotlib.pyplot as plt
from scipy.stats import pearsonr

df = sns.load_dataset("iris")

def corrfunc(x, y, **kws):
    r = pearsonr(x, y)[0] ** 2
    ax = plt.gca()
    ax.annotate("r$^2$ = {:.2f}".format(r),
                 xy=(.1, .9), xycoords=ax.transAxes)
    if r > 0.6:
       col = "g"
    elif r < 0.6:
       col= 'r'

    sns.regplot(x,y,color=col)  
    return r


IC_Plot = sns.PairGrid(df)
IC_Plot.map_offdiag(corrfunc)

plt.show()

在此处输入图片说明

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