如何在python中设置plotly go子弹图中的标记符号和标记文本?

问题描述 投票:0回答:1

我想实现这样的目标
enter image description here

我尝试了plotly库,但确实得到了预期的结果。 任何线索将不胜感激。 感谢世界!!

import plotly.graph_objects as go
fig = go.Figure()
bullet_data = [
    {
        "title": "Revenue",
        # "subtitle": "2023 YTD",
        "ranges": [150, 225, 300],  # Minimum, satisfactory, and maximum values
        "measures": [200],  # Current value
        "markers": [250],  # Target value
    }
]


fig.add_trace(go.Indicator(
    mode="gauge",
    gauge={
        "shape": "bullet",
        "axis": {"range": [None, 400]},
        "threshold": {
            "line": {"color": "red", "width": 2},
            "thickness": 0.75,
            "value": 280,  # Threshold value
        },
        "steps": [
            {"range": [0, 150], "color": "gray"},
            {"range": [150, 225], "color": "lightgray"},
            {"range": [225, 300], "color": "gray"},
        ],
    },
    value=200,  # Current value
    delta={"reference": 250, "position": "top"},
    domain={"x": [0.1, 1], "y": [0.2, 0.8]},
    title={"text": "Revenue"},
))

fig.show()
python charts plotly
1个回答
0
投票

您可以调整主条和步长的相对粗细,以获得更接近所需的子弹图,且阈值线比子弹更宽。

import plotly.graph_objects as go
fig = go.Figure()
bullet_data = [
    {
        "title": "Revenue",
        # "subtitle": "2023 YTD",
        "ranges": [150, 225, 300],  # Minimum, satisfactory, and maximum values
        "measures": [200],  # Current value
        "markers": [250],  # Target value
    }
]

x_upper_range = bullet_data[0]["ranges"][-1]
current_val = bullet_data[0]["measures"][0]
target_val = bullet_data[0]["markers"][0]

fig.add_trace(go.Indicator(
    mode="gauge",
    gauge={
        "shape": "bullet",
        "axis": {"range": [0, x_upper_range]},
        "threshold": {
            "line": {"color": "#404040", "width": 4,},
            "thickness": 0.75,
            "value": target_val,  # target value
        },
        "steps": [
            {
                "range": [current_val, x_upper_range], 
                "color": "lightgray", "thickness": 0.5
            },
        ],
        "bar": {'thickness': 0.5, 'color': '#404040'},
    },
    value=200,  # Current value
    delta={"reference": 250, "position": "top"},
    domain={"x": [0.1, 1], "y": [0.2, 0.8]},
    title={"text": "Revenue"},
))

fig.update_layout(height=400)

fig.show()

© www.soinside.com 2019 - 2024. All rights reserved.