用垫子在行和/或列之间插入间隙

2024-10-02 02:39:29 发布

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

我希望在热图中引入与R的pheatmapgaps_rowgaps_col参数类似的特定行/列之间的间隙。但是,粗略搜索后,此功能似乎不可用。我已经考虑过通过创建不同大小的轴来模拟这种行为,但是要使它们的位置正确是很困难的。有没有更简单的方法来模拟这个功能?在

例如:

pheatmap gap example

从这个question


Tags: 方法功能参数colrow热图question间隙
1条回答
网友
1楼 · 发布于 2024-10-02 02:39:29

下面是在matplotlib中复制上图的代码。在

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

a = np.random.poisson(lam=5, size=(10*5, 4*3))

fig, axes = plt.subplots(nrows=5, ncols=4+1, figsize=(6.5,7),
                         gridspec_kw={"width_ratios":4*[1] + [0.2]})

kw = dict(aspect="auto",vmin=a.min(), vmax= a.max())
for i in range(5):
    for j in range(4):
        im = axes[i,j].imshow(a[10*i:10*i+10,3*j:3*j+3], **kw )
        axes[i,j].tick_params(axis=u'both', which=u'both',length=0)
        axes[i,j].set_xticklabels([])
        axes[i,j].set_yticklabels([])

ylabels=["gene {:02d}".format(i+1) for i in range(50)]
xlabels=["treatment {:02d}".format(i+1) for i in range(12)]
clabels=["{:02d}".format(i+1) for i in range(5)]
for i in range(5):
    axes[i,4].set_facecolor(plt.cm.Set2(i/8.))
    axes[i,4].text(0.5,.5, clabels[i], rotation=-90, color="w",
                   transform=axes[i,4].transAxes,
                   ha="center", va="center", fontweight="bold", fontsize=9)
    axes[i,4].tick_params(axis=u'both', which=u'both',length=0)
    axes[i,4].set_xticklabels([])
    axes[i,4].set_yticklabels([])
    axes[i,0].set_yticks(range(10))
    axes[i,0].set_yticklabels(ylabels[i*10:i*10+10], fontsize=7)

for j in range(4):
    axes[4,j].set_xticks(range(3))
    axes[4,j].set_xticklabels(xlabels[j*3:j*3+3], fontsize=9, rotation=90)
axes[4,4].set_xticks([0.5])
axes[4,4].set_xticklabels(["category"], fontsize=9, rotation=90, fontweight="bold")    

cax = fig.add_axes([0.9,0.5,0.03,0.44])
fig.colorbar(im, cax=cax) 


plt.subplots_adjust(bottom=0.2, top=0.94, right=0.86)   
plt.show()

enter image description here

如果不是所有的行或列都具有相同数量的数据点,但是如果数据点在一行或一列中都相同,则此解决方案仍然有效。然后需要调整gridspec的height_ratioswidth_ratios,例如,如果第二列在x方向上有5个数据点,而不是3个,则需要调用

^{pr2}$

当然,循环需要进行调整,以适应不同数量的滴答声等

相关问题 更多 >

    热门问题