Custom Annotation Seaborn Heatmap

前端 未结 3 981
醉梦人生
醉梦人生 2020-12-01 09:30

I\'m using Seaborn in Python to create a Heatmap. I\'m able to annotate the cells with the values passed in, but I\'d like to add annotations that signify what the cell mean

3条回答
  •  遥遥无期
    2020-12-01 10:23

    This feature has just been added in the recent version of Seaborn 0.7.1.

    From Seaborn update history:

    The annot parameter of heatmap() now accepts a rectangular dataset in addition to a boolean value. If a dataset is passed, its values will be used for the annotations, while the main dataset will be used for the heatmap cell colors

    Here is an example

    data = np.array([[0.000000,0.000000],[-0.231049,0.000000],[-0.231049,0.000000]])
    labels =  np.array([['A','B'],['C','D'],['E','F']])
    fig, ax = plt.subplots()
    ax = sns.heatmap(data, annot = labels, fmt = '')
    

    Note, fmt = '' is necessary if you are using non-numeric labels, since the default value is fmt='.2g' which makes sense only for numeric values and would lead to an error for text labels.

提交回复
热议问题