【问题标题】:Plotly Python Bullet Chart: Format X-Axis as PercentagePlotly Python 子弹图:将 X 轴格式化为百分比
【发布时间】:2021-10-04 18:50:37
【问题描述】:

我正在使用 Ploty 4.4.1。我想将 x 轴和右侧窗格中的数字格式化为百分比。我已经看到了条形图的例子,但它似乎不适用于子弹图。我错过了什么?

import plotly.graph_objects as go

my_score = 0.81
class_avg = 0.75

fig = go.Figure()

fig.add_trace(go.Indicator(
    mode = "number+gauge+delta", value = my_score,
    delta = {'reference': class_avg, 'relative' : True,
             'increasing':{'color':"green"},
             'decreasing':{'color':"red"}
             },
    domain = {'x': [0.1, 1], 'y': [0.2, 0.9]},
    title = {'text': "Math"},
    gauge = {
        'shape': "bullet",
        'axis': {'range': [None, 1]},
        'threshold': {
            'line': {'color': "red", 'width': 2},
            'thickness': 0.75,
            'value': class_avg},
        'bar': {'color': "black"}}))

fig.update_layout(
         title={
               'text': "Black Bar is My Score % Correct <br> Red Line is Class Average",
               'y':0.85,
               'x':0.45,
               'xanchor': 'center',
               'yanchor': 'top'},
         xaxis = dict(
                tickformat = '.1%',
                title = '% Correct',
                fixedrange = True,
                hoverformat = '.1%',
                showgrid = True), 
         yaxis = dict(
                fixedrange = True,
                hoverformat = '.3f',
                showgrid = True),     
         bargap = 0.2, 
         barmode = 'relative')         
fig.show()

【问题讨论】:

    标签: python plotly percentage bullet-chart


    【解决方案1】:

    要更新x轴刻度,您可以在gauge轴字典中添加'tickformat': '.0%',而要更新右侧的数字,您可以在number字典中添加'valueformat': '.0%',请参见下面的代码.

    import plotly.graph_objects as go
    
    my_score = 0.81
    class_avg = 0.75
    
    fig = go.Figure()
    
    fig.add_trace(go.Indicator(
        mode='number+gauge+delta',
        value=my_score,
        number={'valueformat': '.0%'}, # format the number as %
        delta={
            'reference': class_avg,
            'relative': True,
            'increasing': {'color': 'green'},
            'decreasing': {'color': 'red'},
        },
        domain={
            'x': [0.1, 1],
            'y': [0.2, 0.9]
        },
        title={'text': 'Math'},
        gauge={
            'shape': 'bullet',
            'axis': {
                'range': [None, 1],
                'tickformat': '.0%' # format the axis ticks as %
            },
            'threshold': {
                'line': {'color': 'red', 'width': 2},
                'thickness': 0.75,
                'value': class_avg
            },
            'bar': {
                'color': 'black'
            }
        })
    )
    
    fig.update_layout(
         title={
            'text': 'Black Bar is My Score % Correct <br> Red Line is Class Average',
            'y': 0.85,
            'x': 0.45,
            'xanchor': 'center',
            'yanchor': 'top'
         },
    )
    
    fig.write_html('fig.html', auto_open=True)
    

    【讨论】:

      猜你喜欢
      • 2021-05-10
      • 2015-09-30
      • 2017-06-21
      • 2017-11-22
      • 2016-03-13
      • 2020-05-14
      相关资源
      最近更新 更多