【matplotlib复杂热图绘制】自定义元素注释,对数渐变色标,不显示色标,去掉留白,LZW压缩

这篇博客介绍了如何使用matplotlib创建带有注释的热图,特别是针对大范围数据采用对数色标以增强颜色区分度。作者在尝试多种方法后,找到了一种能够实现连续渐变色并且能关联数据的解决方案。通过手动进行log变换来处理数据,以生成形状为14*8的heatmap,并且展示了如何去除色标、调整图片留白以及使用LZW压缩保存TIFF文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的最终图形式

在这里插入图片描述

  1. 元素注释为该元素的name+value
  2. 无color bar
  3. 颜色分明

创建带注释的热图function

详见官方文档,下面是我自己的图,修改了官方例子。

def heatmap(data, ax=None, cbar_kw={
   
   }, cbarlabel = "", **kwargs):
    """
    Create a heatmap from a numpy array and two lists of labels.

    Parameters
    ----------
    data
        A 2D numpy array of shape (N, M).
    row_labels
        A list or array of length N with the labels for the rows.
    col_labels
        A list or array of length M with the labels for the columns.
    ax
        A `matplotlib.axes.Axes` instance to which the heatmap is plotted.  If
        not provided, use current axes or create a new one.  Optional.
    cbar_kw
        A dictionary with arguments to `matplotlib.Figure.colorbar`.  Optional.
    cbarlabel
        The label for the colorbar.  Optional.
    **kwargs
        All other arguments are forwarded to `imshow`.
    """

    if not ax:
        ax = plt.gca()

    # Plot the heatmap
    im = ax.imshow(data, aspect = 'auto', **kwargs)
    # Create colorbar
    cbar = ax.figure.colorbar(im, cax = None, ax = ax, **cbar_kw)
#     cbar.ax.set_ylabel(cbarlabel, font1, rotation = -90, va = "bottom") 因为我不打算显示color bar,我就去掉了~
    return im, cbar
def annotate_heatmap(im, data=None, values = None, valfmt="{x:.2f}",
                     textcolors=("black", "white"),
                     threshold=None, **textkw):
    """
    A function to annotate a heatmap.

    Parameters
    ----------
    im
        The AxesImage to be labeled.
    data
        Data used to annotate.  If None, the image's data is used.  Optional.
    valfmt
        The format of the annotations inside the heatmap.  This should either
        use the string format method, e.g. "$ {x:.2f}", or be a
        `matplotlib.ticker.Formatter`.  Optional.
    textcolors
        A pair of colors.  The first is used for values below a threshold,
        the second for those above.  Optional.
    threshold
        Value in data units according to which the colors from textcolors are
        applied.  If None (the default) uses the middle of the colormap as
        separation.  Optional.
    *
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值