0

我正在使用情节 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()
4

1 回答 1

0

To update the x-axis ticks you can add 'tickformat': '.0%' in the gauge axis dictionary, while to update the number on the right side you can add 'valueformat': '.0%' in the number dictionary, see the code below.

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)

enter image description here

于 2021-10-04T10:50:37.923 回答