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

如何为Plotly Gauge仪表盘的Threshold阈值添加标签?

给Plotly Gauge仪表盘的阈值添加标签

当然可以给阈值添加标签,Plotly的go.Indicator组件的threshold配置本身没有内置标签选项,但可以通过布局的annotations来手动实现。

以下是修改后的完整代码,添加了阈值标签:

import plotly.graph_objects as go

fig = go.Figure(go.Indicator(
    mode = "gauge+number+delta",
    value = 420,
    domain = {'x': [0, 1], 'y': [0, 1]},
    title = {'text': "Speed", 'font': {'size': 24}},
    delta = {'reference': 400, 'increasing': {'color': "RebeccaPurple"}},
    gauge = {
        'axis': {'range': [None, 500], 'tickwidth': 1, 'tickcolor': "darkblue"},
        'bar': {'color': "darkblue"},
        'bgcolor': "white",
        'borderwidth': 2,
        'bordercolor': "gray",
        'steps': [
            {'range': [0, 250], 'color': 'cyan'},
            {'range': [250, 400], 'color': 'royalblue'}],
        'threshold': {
            'line': {'color': "red", 'width': 4},
            'thickness': 0.75,
            'value': 490}}))

# 添加阈值标签
fig.update_layout(
    paper_bgcolor = "lavender", 
    font = {'color': "darkblue", 'family': "Arial"},
    annotations = [dict(
        x=0.5,
        y=0.75,  # 可根据仪表盘布局调整位置,让标签贴合阈值线
        xref='paper',
        yref='paper',
        text='阈值: 490',
        showarrow=False,
        font=dict(color="red", size=16)
    )]
)

fig.show()

关键说明

  • annotations通过字典定义标签属性:
    • x/y:用0-1的相对坐标设置标签在画布上的位置(xref='paper'和yref='paper'表示基于画布的相对位置)
    • text:自定义标签显示内容,可直接关联阈值数值
    • font:设置标签颜色(和阈值线颜色保持一致更协调)、大小
    • showarrow=False:隐藏箭头,让标签简洁呈现

如果需要更精准的位置,可根据阈值数值计算对应比例调整y值,比如仪表盘范围是0-500,阈值490对应的比例为490/500,再结合仪表盘在画布上的垂直范围微调,让标签完全对齐阈值线。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 13:50:01