如何像R语言一样在热图单元上添加叉号(X)? [英] How to add cross (X) on a heatmap cells like with R language?

查看:323
本文介绍了如何像R语言一样在热图单元上添加叉号(X)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在热图单元格上添加叉号(X)(取决于显着性水平,但问题是添加X).

I would like to add cross (X) on heatmap cells (depending on significance level, but the question is on adding the X).

类似于R语言(信号等级= XXX).

Like in R-language (sig.level = XXX).

请参阅所用的Python和R代码以及相应的输出图像.

See the Python and R code used and the corresponding output images.

谢谢您的帮助.

# 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),


         )

```

推荐答案

简单的解决方案是添加带有X形标记的散点图,以删除不需要的单元格.

The easy solution is to add a scatter plot with an X-shaped marker to cross out the unwanted cells.

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()

其缺点是标记大小(s)与单元格数量无关,需要针对不同的图形大小进行调整.

The drawback of this is that the markersize (s) is independent of the number of cells and needs to be adjusted for different figure sizes.

因此,另一种选择是在相应位置绘制一些线(X是两条交叉线).在这里,我们创建一个函数crossout(points, ax=None, scale=1, **kwargs),其中scale是线从每个单元格中所占的百分比.

An alternative is hence to draw some lines (an X are two crossed lines) at the respective positions. Here we create a function crossout(points, ax=None, scale=1, **kwargs), where scale is the percentage the lines shall take from each cell.

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

def crossout(points, ax=None, scale=1, **kwargs):
    ax = ax or plt.gca()
    l = np.array([[[1,1],[-1,-1]]])*scale/2.
    r = np.array([[[-1,1],[1,-1]]])*scale/2.
    p = np.atleast_3d(points).transpose(0,2,1)
    c = LineCollection(np.concatenate((l+p,r+p), axis=0), **kwargs)
    ax.add_collection(c)
    return c

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)


crossout(np.argwhere(data_masked.T < 0.4), ax=ax, scale=0.8, color="black")


plt.show()

对于scale=0.8,它看起来像

请注意,对于pcolormesh绘图或季节性热图(内部使用pcolormesh),需要将0.5添加到数据中,即

Note that for a pcolormesh plot or a seaborn heatmap (which uses pcolormesh internally), one would need to add 0.5 to the data, i.e.

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

这篇关于如何像R语言一样在热图单元上添加叉号(X)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆