如何制作Plotly指标仪表格?

4
我想使用plotly将仪表盘以网格布局排列(为我们展示一组团队的SLO仪表盘的Streamlit仪表板)。由于指标不兼容,所以我无法使用子图:
ValueError: Trace type 'indicator' is not compatible with subplot type 'xy' at grid position (1, 1)
See the docstring for the specs argument to plotly.subplots.make_subplots for more information on subplot types

最优雅(代码最少)的方式在网格中排列仪表盘指示器是什么?


你可以使用子图来创建非xy网格,但需要提供适当的参数。请编辑你的问题并包含一个可重现的示例。没有更多的上下文,我无法开始回答。 - Rob Raymond
@quantoid,提供的建议对您有帮助吗? - vestland
2个回答

3
  • 我们已经合成了与您所描述的相匹配的数据
  • subplots() 可以使用,subplot types 我们使用列表推导式来构建这个需求
  • 显然,这个示例需要更多美化,因为标题重叠在仪表上
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd

# a bit of data to generate guages from...
df = pd.DataFrame({"team":["sprint 1", "sprint 2", "qa"],"backlog":[45,55,22],"defects":[23,44,33]}).set_index("team")

# need to define types of subplots...
fig = make_subplots(
    rows=len(df),
    cols=len(df.columns),
    specs=[[{"type": "indicator"} for c in df.columns] for t in df.index],
)

for r, team in enumerate(df.index):
    for c, measure in enumerate(df.columns):
        fig.add_trace(
            go.Indicator(mode="gauge+number", value=df.loc[team, measure], title={"text":f"{team} - {measure}"}),
            row=r + 1,
            col=c + 1,
        )

fig.update_layout(margin={"l": 0, "r": 20, "t": 50, "b": 0})

enter image description here


1

go.Indicator 是一个仪表盘图表,具有特殊属性domain,可以通过在[0, 1]之间指定x和y来将指示器放置在图形对象内。查看Python中的指示器以获取此示例:

绘图

enter image description here

完整代码:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Indicator(
    value = 200,
    delta = {'reference': 160},
    gauge = {
        'axis': {'visible': False}},
    domain = {'row': 0, 'column': 0}))

fig.add_trace(go.Indicator(
    value = 120,
    gauge = {
        'shape': "bullet",
        'axis' : {'visible': False}},
    domain = {'x': [0.05, 0.5], 'y': [0.15, 0.35]}))

fig.add_trace(go.Indicator(
    mode = "number+delta",
    value = 300,
    domain = {'row': 0, 'column': 1}))

fig.add_trace(go.Indicator(
    mode = "delta",
    value = 40,
    domain = {'row': 1, 'column': 1}))

fig.update_layout(
    grid = {'rows': 2, 'columns': 2, 'pattern': "independent"},
    template = {'data' : {'indicator': [{
        'title': {'text': "Speed"},
        'mode' : "number+delta+gauge",
        'delta' : {'reference': 90}}]
                         }})

对于那些喜欢详细研究这些内容的人:

|“domain”属性是Domain的一个实例,可以指定为:| - :class:plotly.graph_objs.indicator.Domain的实例 | - 传递给Domain构造函数的字符串/值属性的字典|支持的字典属性:|
| column | 如果有布局网格,请在此列中使用该域进行此指标跟踪。| row | 如果有布局网格,请在此行中使用该域进行此指标跟踪。| x | 设置此指标跟踪的水平域(以绘图分数表示)。| y | 设置此指标跟踪的垂直域(以绘图分数表示)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接