How to change point separator for the y-axis of a Plotly figure?

Question:

I’ve been working on a graph for days to summarize some results. However, I just got stuck trying to change the y-axis separator to "," instead of "."

I resorted to ChatGPT, as I don’t have that much skill with the Plotly library, in Python, however, the solutions he presented still did not correct the problem.

In any case, here’s the complete code for anyone who can help me. The graph is generated right after execution.

Greetings!

import plotly.graph_objects as go
from plotly.offline import plot

data = [
    ['TabNet', 0.6589, 'FT-Transformer', 0.6570, 'NODE', 0.6347, 'TabTransform', 0.6338, 'MLP', 0.6310, '1D-CNN', 0.5911],
    ['TabNet', 0.6347, 'MLP', 0.6115, '1D-CNN', 0.6013, 'NODE', 0.5315, 'FT-Transformer', 0.2871, 'TabTransform', 0.2156],
    ['TabNet', 0.6645, 'TabTransform', 0.6589, 'NODE', 0.6542, 'FT-Transformer', 0.6421, 'MLP', 0.6050, '1D-CNN', 0.5957],
    ['FT-Transformer', 0.6570, 'TabNet', 0.6525, 'TabTransform', 0.6254, 'NODE', 0.6236, 'MLP', 0.5827, '1D-CNN', 0.5780],
    ['TabNet', 0.6672, 'NODE', 0.6515, 'FT-Transformer', 0.6375, 'MLP', 0.6338, '1D-CNN', 0.5743, 'TabTransform', 0.3503]
]

strategies = ['TA/HI', 'PCA', 'SA', 'UNDER', 'OVER']

architectures = ['1D-CNN', 'FT-Transformer', 'MLP', 'NODE', 'TabNet', 'TabTransform']

colors = {
    '1D-CNN': '#8c564b',
    'FT-Transformer': '#ff7f0e',
    'MLP': '#9467bd',
    'NODE': '#2ca02c',
    'TabNet': '#1f77b4',
    'TabTransform': '#d62728'
}

fig = go.Figure()

for i in range(len(architectures)):
    y_values = []
    text_values = []
    color_values = []
    for j in range(len(strategies)):
        strategy = strategies[j]
        for k in range(len(data[j]) // 2):
            if data[j][k*2] == architectures[i]:
                y_values.append(data[j][k*2+1])
                text_values.append("{:.2%}".format(data[j][k*2+1]).replace('.', ','))
                color_values.append(colors[architectures[i]])
                break
    fig.add_trace(go.Bar(
        name=architectures[i],
        x=strategies,
        y=y_values,
        text=text_values,
        textposition="outside",
        textangle=0,
        textfont=dict(size=14),
        marker=dict(color=color_values)
    ))

fig.update_layout(
    font=dict(size=10),
    plot_bgcolor='rgba(0,0,0,0)',
    barmode='group',
    bargap=0.15,
    bargroupgap=0.1,
    legend=dict(orientation='h', yanchor='bottom', y=1.02, xanchor='center', x=0.5),
    margin=dict(t=100)
)

fig.update_layout(yaxis=dict(tickformat='.2%'))

for annotation in fig['layout']['annotations']:
    annotation['text'] = annotation['text'].replace('.', ',')

fig.show()
Asked By: PM92

||

Answers:

Update the separators of the figure layout:

fig.update_layout(yaxis=dict(tickformat='.2%'), separators=",")

separators – Sets the decimal and thousand separators. For example, *. * puts a ‘.’ before decimals and a space between thousands. In English locales, dflt is “.,” but other locales may alter this default.

https://plotly.com/python-api-reference/generated/plotly.graph_objects.Layout.html

Answered By: 5eb
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.