You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly使用px.imshow生成热图如何在每个瓦片上显示数值

完全可以实现,你可以通过px.imshow()的内置参数快速实现该需求,无需额外复杂配置。

最简实现方案

直接在创建图像时传入text_auto=True即可自动在每个瓦片上渲染对应的像素数值,修改后的代码如下:

import plotly.express as px
import numpy as np
img_rgb = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
                    [[0, 255, 0], [0, 0, 255], [255, 0, 0]]
                   ], dtype=np.uint8)
# 新增text_auto=True参数自动展示数值
fig = px.imshow(img_rgb, text_auto=True)
fig.show()

自定义显示格式

如果需要调整文本的展示内容、样式,可以通过update_traces方法配置:

# 示例:仅显示R通道数值,设置字体大小为16,字体颜色为白色
fig.update_traces(
    texttemplate='R:%{z[0]}',
    textfont_size=16,
    textfont_color='white'
)

参数说明:

  • texttemplate为文本模板,%{z[0]}对应R通道值,%{z[1]}对应G通道值,%{z[2]}对应B通道值,可按需拼接文本
  • textfont_size、textfont_color分别控制文本的字号和颜色

内容的提问来源于stack exchange,提问作者jvels

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 17:45:03