Python 根据pearsonr值更改每个PairGrid Seaborn绘图中的字体颜色:

Python 根据pearsonr值更改每个PairGrid Seaborn绘图中的字体颜色:,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我正在使用seaborn和seaborn.PairGrid函数进行绘图(代码如下) 我能够计算pearsonr值并设置颜色列表 有没有办法根据pearsonr相关值更改每个成对地块的颜色 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(

我正在使用seaborn和seaborn.PairGrid函数进行绘图(代码如下)

我能够计算pearsonr值并设置颜色列表

有没有办法根据pearsonr相关值更改每个成对地块的颜色

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")
global my\u颜色
我的颜色=[]
def corrfunc(x,y,**kws):
r=stats.pearsonr(x,y)[0]**2
ax=plt.gca()
ax.Annotation(“r$^2$={.2f}”。格式(r),
xy=(.1.9),xycoords=ax.transAxes)
如果r>0.6:
my_colors.append('g'))
elif r<0.6:
my_colors.append('r'))
返回r
df=pd.read\u excel(Inp\u文件名,sheetname='IC\u数据')
IC_Plot=sns.PairGrid(df)
ICU绘图。地图绘制图(corrfunc)
IC_Plot.map_offdiag(sns.regplot,color='g'))
#需要改变每种颜色
#基于pearsonr值的pairplot
IC图。地图。地图(plt.scatter,s=10)
IC_Plot.savefig(“IC_Pair.png”)

您可以在确定颜色的函数中调用plotting方法。通过这种方式,颜色可以简单地作为绘图函数的参数

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()
仅作为sns导入seaborn.api
将matplotlib.pyplot作为plt导入
从scipy.stats导入pearsonr
df=sns.load_数据集(“iris”)
def corrfunc(x,y,**kws):
r=pearsonr(x,y)[0]**2
ax=plt.gca()
ax.Annotation(“r$^2$={.2f}”。格式(r),
xy=(.1.9),xycoords=ax.transAxes)
如果r>0.6:
col=“g”
elif r<0.6:
col='r'
sns.regplot(x,y,color=col)
返回r
IC_Plot=sns.PairGrid(df)
ICU绘图。地图绘制图(corrfunc)
plt.show()