Plotly / Plotly-Dash:如何为图表中的每个图例嵌入数字+增量指标?

Plotly / Plotly-Dash: How to embed number+delta indicators for each legend inside a graph?

我正在尝试使用 Plotly 包在每个图例下方嵌入一个数字和一个增量指示器。

这是所需结果的示例(图片右侧):

我试着查看 plotly.graph_objects / plotly.express 的文档,但没有找到任何内容。

指标文档:https://plotly.com/python/indicator/

上面附带的文档中的第四个示例很接近,但这不是我的用例:

import plotly.graph_objects as go

fig = go.Figure(go.Indicator(
    mode = "number+delta",
    value = 492,
    delta = {"reference": 512, "valueformat": ".0f"},
    title = {"text": "Users online"},
    domain = {'y': [0, 1], 'x': [0.25, 0.75]}))

fig.add_trace(go.Scatter(
    y = [325, 324, 405, 400, 424, 404, 417, 432, 419, 394, 410, 426, 413, 419, 404, 408, 401, 377, 368, 361, 356, 359, 375, 397, 394, 418, 437, 450, 430, 442, 424, 443, 420, 418, 423, 423, 426, 440, 437, 436, 447, 460, 478, 472, 450, 456, 436, 418, 429, 412, 429, 442, 464, 447, 434, 457, 474, 480, 499, 497, 480, 502, 512, 492]))

fig.update_layout(xaxis = {'range': [0, 62]})
fig.show()

我搜索了我们的社区,也没有发现任何关于此的内容。

这是可以实现的吗?

谢谢!

您可以使用 Plotly Subplots 执行此操作,请参阅下面的代码作为示例。

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

fig = make_subplots(rows=3, cols=2,
                    column_widths=[0.9, 0.1],
                    specs=[[{'rowspan': 3}, {'type': 'indicator'}],   # first row
                           [None,           {'type': 'indicator'}],   # second row
                           [None,           {'type': 'indicator'}]])  # third row

# first column
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2],
                         mode='lines',
                         line=dict(color='blue')),
              row=1, col=1)

fig.add_trace(go.Scatter(x=[1, 2], y=[2, 3],
                         mode='lines',
                         line=dict(color='purple')),
              row=1, col=1)

fig.add_trace(go.Scatter(x=[1, 2], y=[3, 4],
                         mode='lines',
                         line=dict(color='green')),
              row=1, col=1)

# second column
fig.add_trace(go.Indicator(mode='number+delta',
                           value=2,
                           delta={'reference': 1,
                                  'valueformat': '.0f',
                                  'increasing': {'color': 'blue'},
                                  'decreasing': {'color': 'gray'}},
                           title={'text': '28-Day'},
                           domain={'y': [0, 1], 'x': [0.25, 0.75]}),
              row=1, col=2)

fig.add_trace(go.Indicator(mode='number+delta',
                           value=3,
                           delta={'reference': 2,
                                  'valueformat': '.0f',
                                  'increasing': {'color': 'purple'},
                                  'decreasing': {'color': 'gray'}},
                           title={'text': '7-Day'},
                           domain={'y': [0, 1], 'x': [0.25, 0.75]}),
              row=2, col=2)

fig.add_trace(go.Indicator(mode='number+delta',
                           value=4,
                           delta={'reference': 3,
                                  'valueformat': '.0f',
                                  'increasing': {'color': 'green'},
                                  'decreasing': {'color': 'gray'}},
                           title={'text': '1-Day'},
                           domain={'y': [0, 1], 'x': [0.25, 0.75]}),
              row=3, col=2)

fig.update_layout(showlegend=False,
                  plot_bgcolor='white',
                  paper_bgcolor='white',
                  xaxis=dict(linecolor='gray',
                             mirror=True,
                             showgrid=False),
                  yaxis=dict(linecolor='gray',
                             mirror=True,
                             showgrid=False))

fig.show()