如何制作 Plotly Indicator 仪表的网格? 情节完整代码:

问题描述

我正在尝试使用 plotly 在网格布局中排列 gauges(为我们提供一个 Streamlit 仪表板,显示一组团队的 SLO 仪表)。

我不能使用子图,因为它们与指标不兼容:

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

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

解决方法

  • 已合成数据以匹配您所描述的内容
  • 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

,

go.Indicator 是一个 gauge chart 并且有一个特殊的属性 domain,它允许您通过在 x and y 之间指定 [0,1] 将指标放置在图形对象中。看看这个例子的 Indicators in 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,'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 | - 一个字典 将传递的字符串/值属性 |到域 构造函数| |支持的字典属性:|
|专栏 |如果有布局网格, 将域用于 |网格中的这一列 该指标|痕迹 。 |行 |
如果有布局网格,请使用域为 |这 此指标跟踪的网格中的行。 | x |
设置该指标的水平域 |痕迹 (在情节分数)。 |是 |设置 该指标的垂直领域|跟踪(在情节 分数)。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...