我的最终图形式
- 元素注释为该元素的name+value
- 无color bar
- 颜色分明
创建带注释的热图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