python - 在 Plotly Indicator python 中显示相对和绝对差异

标签 python plotly plotly-dash

我正在尝试使用plotly.graph_objs.Indicator来显示输出。

go.Indicator(mode = "number+delta", value = 200,
             number = {'font': {'size': 30}},
             title = {'text': 'HEAD', 'font':{'size':20, 'color':'green'}},
             domain = {'x': [0, 1], 'y': [0, 1]},
             delta = {'reference':300, 'relative':False})

在这里,如果我传递relative=True,则会显示相对差异,例如。 30%。

如果我通过relative=False,将显示绝对差异,例如。 -20

有没有办法同时获得绝对差和相对差。

最佳答案

似乎相对和绝对变化是由 go.Indicator 方法内部计算的(这意味着您无法自定义要在 delta 符号旁边显示的文本),并且您必须在相对或绝对之间进行选择更改为显示。

最简单的解决方案是添加文本注释。但是,独立文本不是 graph_object,而是在图形上呈现。这意味着文本可以与图中的其他图形对象重叠。

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Indicator(
    mode = "number+delta", 
    value = 200,
    number = {'font': {'size': 30}},
    title = {'text': 'HEAD', 'font':{'size':20, 'color':'green'}},
    domain = {'x': [0, 1], 'y': [0, 1]},
    delta = {'reference':300, 'relative':False})
)

fig.add_annotation(x=0.5, y=0.4, text="-30%", font=dict(color="red"), showarrow=False)

fig.show()

该图在相当大的浏览器窗口中看起来不错:

enter image description here

但是当您调整浏览器窗口大小时,文本注释可能会与 go.Indicator 对象重叠,这很丑陋:

enter image description here

一个更好但hacky的解决方案是添加三个不同的go.Indicator对象作为痕迹:前两个go.Indicator对象将显示相对和绝对变化,但我们将数字的字体颜色设置为背景颜色,以便它们不可见。然后第三个 go.Indicator 对象添加数字 200 和标题 HEAD ,但不显示增量。最后一件事是相应地设置每个 go.Indicator 对象的域,以便在您调整浏览器窗口大小时它们不会重叠。

您可以调整每条轨迹的域,以使您更接近您正在寻找的结果。

fig = go.Figure()

# the first two traces are just the delta with the value set to the background color
fig.add_trace(go.Indicator(
    mode = "number+delta", 
    value = 200,
    number = {'font': {'size': 30, 'color':'rgb(255,255,255,0)'}},
    domain = {'x': [0, 0.8], 'y': [0, 0.8]},
    delta = {'reference':300, 'relative':False})
)

fig.add_trace(go.Indicator(
    mode = "number+delta", 
    value = 200,
    number = {'font': {'size': 30, 'color':'rgb(255,255,255,0)'}},
    domain = {'x': [0.2, 1], 'y': [0, 0.8]},
    delta = {'reference':300, 'relative':True})
)

## this adds the title and value
fig.add_trace(go.Indicator(
    mode = "number", 
    value = 200,
    number = {'font': {'size': 30}},
    title = {'text': 'HEAD', 'font':{'size':20, 'color':'green'}},
    domain = {'x': [0, 1], 'y': [0, 1]})
)

fig.show()

这是带有大浏览器窗口的图的样子:

enter image description here

当您调整浏览器窗口大小时,go.Indicator 痕迹保持清晰:

enter image description here

关于python - 在 Plotly Indicator python 中显示相对和绝对差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67285902/

相关文章:

python - 安装 Numpy 和 Scipy - 找不到系统 python 2.6

python - Plotly-Dash Graph - 显示在形状后面的文本注释

python - 通知用户 Dash 中的输入无效

python 破折号 : Get the edited cell value from filterable table

python - Dash CallBacks 一种获取函数的输出 Id 列表或静态变量的方法

python - 我无法在 jupyter 笔记本中读取 EAST : An Efficient and Accurate Scene Text Detector as . pb 文件

python - firebase admin pip install 出现错误后

python - 读取未知数量的行,其中有一些空行

python - Plotly:如何绘制多个 y 轴?

plotly - 如何在绘图破折号中的日期范围选择器之前获取文本标签?