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

如何将Plotly Gauge(非Indicator)作为子图插入图表

核心问题解答

  1. daq.Gauge本身不兼容make_subplots方法,和specs参数设置无关
    二者属于完全不同的技术栈:make_subplots生成的Plotly Figure对象仅支持添加plotly.graph_objs下的Trace类型实例;而daq.Gauge是Dash生态的前端交互组件,专门用于Dash应用的布局配置,不属于Plotly Trace范畴,根本无法通过append_trace插入到Figure的子图中。

你误解了go.Indicator的能力,它的gauge模式完全支持配置老式转速表的指针样式,完全满足你远距离读取的辨识度需求,不需要使用daq的Gauge组件。

  1. 为什么需要提前指定子图类型
    make_subplots需要提前为每个子图预分配对应的坐标系资源,不同Trace依赖的坐标系完全不同:比如散点图需要xy笛卡尔坐标系,Indicator/Gauge类Trace需要基于画布相对位置的无坐标轴domain坐标系。如果不提前指定,添加Trace时的自动识别逻辑很容易出现坐标系不匹配的渲染异常,提前声明specs是为了保障布局的稳定性。

可运行的实现代码

你原来的specs参数配置是正确的,只需要将第三张子图替换为带指针的go.Indicator实例即可:

from plotly.subplots import make_subplots
import plotly.graph_objs as go

trace1 = go.Scatter(
    x = [0, 1, 2],
    y = [0, 8, 6]
)

indicator1 = go.Indicator(
    value = 6,
    mode = "gauge+number",
)

# 带指针的Gauge样式Indicator,完全符合老式转速表的显示需求
gauge_style_indicator = go.Indicator(
    value = 6,
    mode = "gauge+number",
    gauge = {
        "axis": {"range": [0, 10]},
        "bar": {"thickness": 0}, # 隐藏默认的填充条
        "steps": [
            {"range": [0, 10], "color": "#e6e6e6"}
        ],
        "threshold": {
            "line": {"color": "red", "width": 4},
            "thickness": 0.75,
            "value": 6
        }
    }
)

fig = make_subplots(rows=1, cols=3, specs=[[
    {'type':'xy'},
    {'type':'domain'},
    {'type':'domain'}]]
)

fig.append_trace(trace1, row=1, col=1)
fig.append_trace(indicator1, row=1, col=2)
fig.append_trace(gauge_style_indicator, row=1, col=3)

fig.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 12:24:04