如何在地图上加上X语言的交叉单元格?

2024-09-27 21:31:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在热图单元上加上十字(X)(取决于显著性水平,但问题在于添加X)。在

就像R语言一样(信号电平=XXX)。在

请参阅使用的Python和R代码以及相应的输出图像。在

谢谢你的帮助。在

# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr, mask=mask, cmap=cmap, center=0, vmin=-1, vmax=1, square=True, linewidths=0.5, fmt=".2f",
            cbar_kws={"shrink": .65, "orientation": "horizontal", "ticks":np.arange(-1, 1+1, 0.2)}, 
            annot = True, annot_kws={"weight": 'bold', "size":15})




corrplot(cor(subset (wqw, select = 
                       c(fixed.acidity:quality,ratio.sulfur.dioxide))),
         # compute the p matrix
         p.mat = cor.mtest(subset 
            (wqw, select = c(fixed.acidity:quality,ratio.sulfur.dioxide))), 
         # significance level 0.01
         sig.level = 0.01, 
         # Method to display : color (could be corcle, ...)
         method = "color",
         # color palette
         col = colorRampPalette(c("#BB4444", "#EE9988", 
                                  "#FFFFFF", "#77AADD", "#4477AA"))(200),


         )

```

python matrixR matrix


Tags: thetruemaskselectcolorfixedcmapsubset
1条回答
网友
1楼 · 发布于 2024-09-27 21:31:10

简单的解决方案是添加一个带有X形标记的散点图,以划掉不需要的单元格。在

import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt

data = np.random.rand(10,10)
mask = np.zeros_like(data)
mask[np.triu_indices_from(mask)] = True

data_masked = np.ma.array(data, mask=mask)

fig, ax = plt.subplots()
im = ax.imshow(data_masked, cmap="YlGnBu", origin="upper")
fig.colorbar(im)

ax.scatter(*np.argwhere(data_masked.T < 0.4).T, marker="x", color="black", s=100)

plt.show()

enter image description here

这样做的缺点是markersize(s)与单元格的数量无关,需要根据不同的图形大小进行调整。在

因此,另一种方法是在各自的位置绘制一些线(X是两条交叉线)。这里我们创建一个函数crossout(points, ax=None, scale=1, **kwargs),其中scale是行从每个单元格中获得的百分比。在

^{pr2}$

对于scale=0.8这看起来像

enter image description here

注意对于一个pcolormesh图或一个海生热图(内部使用pcolormesh),需要在数据中添加0.5,即

np.argwhere(data_masked.T < 0.4)+0.5

相关问题 更多 >

    热门问题