绘图指示器更改值的颜色

Plotly indicator change color of value

我用这段代码绘制了 4 个指标:

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

fig = make_subplots(
    rows=2,
    cols=2,
    subplot_titles=('Title 1', 'Title 2', 'Title 3','Title 4'),
    specs=[[{'type': 'indicator'}, {'type': 'indicator'}],
 [{'type': 'indicator'}, {'type': 'indicator'}]]
)

fig.add_trace(
    go.Indicator(mode="number", value=2761),
    row=1,
    col=1,
)

fig.add_trace(
    go.Indicator(mode="number", value=281),
    row=1,
    col=2,
)

fig.add_trace(
    go.Indicator(mode="number", value=921),
    row=2,
    col=1,
)

fig.add_trace(
    go.Indicator(mode="number", value=21),
    row=2,
    col=2,
)

fig.layout.annotations[0].update(yanchor='bottom', y=0.6)
fig.layout.annotations[1].update(yanchor='bottom', y=0.6)
fig.layout.annotations[2].update(yanchor='bottom', y=0)
fig.layout.annotations[3].update(yanchor='bottom', y=0)

fig.show()

这就是指标的显示方式:

我正在尝试更改值的颜色,例如我希望值 281 为蓝色。我不知道该怎么做,也没有在文档中找到任何内容。如果有人可以帮助我,谢谢:)

你要的参数是:https://plotly.com/python/reference/indicator/#indicator-number-font-color

使用您的示例的解决方案

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

fig = make_subplots(
    rows=2,
    cols=2,
    subplot_titles=('Title 1', 'Title 2', 'Title 3','Title 4'),
    specs=[[{'type': 'indicator'}, {'type': 'indicator'}],
 [{'type': 'indicator'}, {'type': 'indicator'}]]
)

fig.add_trace(
    go.Indicator(mode="number", value=2761),
    row=1,
    col=1,
)

fig.add_trace(
    go.Indicator(mode="number", value=281, number_font_color="blue"),
    row=1,
    col=2,
)

fig.add_trace(
    go.Indicator(mode="number", value=921),
    row=2,
    col=1,
)

fig.add_trace(
    go.Indicator(mode="number", value=21),
    row=2,
    col=2,
)

指标有一个名为 'number' 的设置,允许您更改字体颜色、大小等。这是第一个指标中的更改示例。

fig.add_trace(
    go.Indicator(mode="number", value=2761, number={'font_color':'red', 'font_size':60}),
    row=1,
    col=1,
)

Rob Raymond 发布的答案将解决您的问题。

但是,对于 plotly,由于最终所有内容都会解析为 html/css,有时使用字典定义绘图会更容易,这样您就不必每次都花费数小时查找每个参数更改属性:

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

fig = make_subplots(
    rows=2,
    cols=2,
    subplot_titles=('Title 1', 'Title 2', 'Title 3','Title 4'),
    specs=[[{'type': 'indicator'}, {'type': 'indicator'}],
 [{'type': 'indicator'}, {'type': 'indicator'}]]
)

fig.add_trace(
    go.Indicator(
        {
        'mode': 'number', 
        'number': {'font': {'color': 'red'}}, 
        'value': 2761
         }
    ),
    row=1,
    col=1,
)

fig.add_trace(
    go.Indicator(
        {
        'mode': 'number', 
        'number': {'font': {'color': 'blue'}}, 
        'value': 281
         }
    ),
    row=1,
    col=2,
)

fig.add_trace(
    go.Indicator(
        {
        'mode': 'number', 
        'number': {'font': {'color': '#ff00ff'}}, 
        'value': 921
         }
    ),
    row=2,
    col=1,
)

fig.add_trace(
    go.Indicator(
          {
        'mode': 'number', 
        'number': {'font': {'color': 'green'}}, 
        'value': 21
         }
    ),
    row=2,
    col=2,
)

如果您想更改字体大小或字体系列,这样就简单多了。