我想实现这样的目标
我尝试了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()
您可以调整主条和步长的相对粗细,以获得更接近所需的子弹图,且阈值线比子弹更宽。
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()