不同颜色的 seaborn 热图中颜色统计上不显着的值
Colour statistically non-significant values in seaborn heatmap with a different colour
我遇到了这个问题,我想以某种方式强调 seaborn
的 heatmap 中统计上不显着的相关性。我知道我可以用下面的代码隐藏它们:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import pearsonr
planets = sns.load_dataset('planets')
# get the p value for pearson coefficient, subtract 1 on the diagonal
pvals = planets.corr(method=lambda x, y: pearsonr(x, y)[1]) - np.eye(*planets.corr().shape)
# set the significance threshold
psig = 0.05
plt.figure(figsize=(6,6))
sns.heatmap(planets.corr()[pvals<psig], annot=True, square=True)
然而,这会产生这些奇怪的白洞,我想保留这些值和信息,我只想用另一种颜色强调它。
解决方法是 a) 对绘制到相同轴上的另一个热图使用相同的阈值;和 b) 到图例,这样它也有一个漂亮的标签:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import pearsonr
import matplotlib.patches as mpatches
planets = sns.load_dataset('planets')
# get the p value for pearson coefficient, subtract 1 on the diagonal
pvals = planets.corr(method=lambda x, y: pearsonr(x, y)[1]) - np.eye(*planets.corr().shape)
# set the significance threshold
psig = 0.05
plt.figure(figsize=(6,6))
sns.heatmap(planets.corr()[pvals<psig], annot=True, square=True)
# add another heatmap with colouring the non-significant cells
sns.heatmap(planets.corr()[pvals>=psig], annot=True, square=True, cbar=False,
cmap=sns.color_palette("Greys", n_colors=1, desat=1))
# add a label for the colour
#
colors = [sns.color_palette("Greys", n_colors=1, desat=1)[0]]
texts = [f"n.s. (at {psig})"]
patches = [ mpatches.Patch(color=colors[i], label="{:s}".format(texts[i]) ) for i in range(len(texts)) ]
plt.legend(handles=patches, bbox_to_anchor=(.85, 1.05), loc='center')
此外,人们甚至可以使用多个条件来掩蔽和不同的显着性水平。
我遇到了这个问题,我想以某种方式强调 seaborn
的 heatmap 中统计上不显着的相关性。我知道我可以用下面的代码隐藏它们:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import pearsonr
planets = sns.load_dataset('planets')
# get the p value for pearson coefficient, subtract 1 on the diagonal
pvals = planets.corr(method=lambda x, y: pearsonr(x, y)[1]) - np.eye(*planets.corr().shape)
# set the significance threshold
psig = 0.05
plt.figure(figsize=(6,6))
sns.heatmap(planets.corr()[pvals<psig], annot=True, square=True)
然而,这会产生这些奇怪的白洞,我想保留这些值和信息,我只想用另一种颜色强调它。
解决方法是 a) 对绘制到相同轴上的另一个热图使用相同的阈值;和 b)
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import pearsonr
import matplotlib.patches as mpatches
planets = sns.load_dataset('planets')
# get the p value for pearson coefficient, subtract 1 on the diagonal
pvals = planets.corr(method=lambda x, y: pearsonr(x, y)[1]) - np.eye(*planets.corr().shape)
# set the significance threshold
psig = 0.05
plt.figure(figsize=(6,6))
sns.heatmap(planets.corr()[pvals<psig], annot=True, square=True)
# add another heatmap with colouring the non-significant cells
sns.heatmap(planets.corr()[pvals>=psig], annot=True, square=True, cbar=False,
cmap=sns.color_palette("Greys", n_colors=1, desat=1))
# add a label for the colour
#
colors = [sns.color_palette("Greys", n_colors=1, desat=1)[0]]
texts = [f"n.s. (at {psig})"]
patches = [ mpatches.Patch(color=colors[i], label="{:s}".format(texts[i]) ) for i in range(len(texts)) ]
plt.legend(handles=patches, bbox_to_anchor=(.85, 1.05), loc='center')
此外,人们甚至可以使用多个条件来掩蔽和不同的显着性水平。