【问题标题】:Making annotations on axis of heatmaps在热图轴上进行注释
【发布时间】:2019-03-16 22:46:18
【问题描述】:

我想在轴上制作一个带有一些附加注释的热图,类似于下图。在右侧垂直轴上,有括号和注释来表示特定标签的行。

在 matplotlib 中会出现这样的情况吗?

【问题讨论】:

  • 情节和括号之间的浅蓝色圆点背后的秘密是什么?

标签: python matplotlib plot heatmap


【解决方案1】:

在下面,我定义了一个函数,它根据输入的左下角、宽度和高度创建一个花括号补丁。
然后可以在循环中使用它以及带的宽度和位置列表及其名称。

请注意:作为第一次尝试,这种方法仍有一些缺点:

  • 基本形式被完全拉伸,这意味着大括号的圆角随着高度(当然还有宽度,这里是恒定的)而变化。这可以在下一步进行优化。
  • 只要您使用 ggplot 样式 (plt.style.use('ggplot')),它就不会再出现了。我认为这是因为 ggplo-style 及其 z-order 使用了一些补丁,但到目前为止,我无法解决这个问题。

代码:

def CurlyBrace(ll_corner=(0, 0), width=1, height=1):
    import matplotlib.path as mpath
    import matplotlib.patches as mpatches
    import numpy as np
    Path = mpath.Path
    verts = np.array([(0, 0), (.5, 0), (.5, .2), (.5, .3), (.5, .5), (1, .5), (.5, .5), (.5, .7), (.5, .8), (.5, 1), (0, 1)])
    verts[:, 0] *= width
    verts[:, 1] *= height
    verts[:, 0] += ll_corner[0]
    verts[:, 1] += ll_corner[1]

    cb_patch = mpatches.PathPatch(
        Path(verts,
             [Path.MOVETO, Path.CURVE3, Path.CURVE3, Path.LINETO, Path.CURVE3, Path.CURVE3, Path.CURVE3, Path.CURVE3, Path.LINETO, Path.CURVE3, Path.CURVE3]),
        fc="none", clip_on=False, transform=ax.transData)
    return cb_patch


import imageio

im = imageio.imread(pic_dir + 'sample_heatmap.png')
fig, ax = plt.subplots()
ax.imshow(im)


bands = np.array([0, 175, 320, 448, 585, 610, 815])
bnames = ['H3K4me3', 'H3K9me3', 'H3K27me3', 'H3K36me3', 'CTCF', 'H3K4me1']
for y, h, bn in zip(bands, np.diff(bands), bnames):
    cb = CurlyBrace([990, y+h*0.025], 30, h*.95)
    ax.add_patch(cb)
    ax.text(1030, y+h/2, bn, va='center')

plt.tight_layout(rect=[0.05, 0, 0.85, 1])

结果:

【讨论】:

    猜你喜欢
    • 2020-04-19
    • 2021-03-14
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多