【问题标题】:Plotly indicator change color of value绘图指示器更改值的颜色
【发布时间】:2022-01-04 09:56:50
【问题描述】:

我在这段代码中有 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 为蓝色。我不知道该怎么做,也没有在文档中找到任何东西。如果有人可以帮助我,谢谢:)

【问题讨论】:

    标签: python plotly plotly-python


    【解决方案1】:

    你要的参数是: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,
    )
    
    

    【讨论】:

      【解决方案2】:

      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,
      )
      

      这种方式如果你想改变字体大小或字体家族,就更简单了。

      【讨论】:

        【解决方案3】:

        指示器有一个名为“数字”的设置,允许您更改字体颜色、大小等。这里是第一个指示器的更改示例。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-16
          • 2018-03-03
          • 1970-01-01
          • 2017-03-21
          • 2012-04-17
          • 1970-01-01
          • 1970-01-01
          • 2015-10-24
          相关资源
          最近更新 更多