如何更改Plotly图的y轴点分隔符?

huangapple go评论76阅读模式
英文:

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

问题

fig.update_layout(yaxis=dict(tickformat='.2%'.replace('.', ',')))
for annotation in fig['layout']['annotations']:
    annotation['text'] = annotation['text'].replace('.', ',')
英文:

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

答案1

得分: 1

更新图表布局中的separators

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

separators – 设置小数点和千位分隔符。例如,*.*将在小数点前放置一个点号,千位之间放置一个空格。在英语环境中,默认值是“.”,但其他地区可能会更改此默认值。

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

英文:

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

huangapple
  • 本文由 发表于 2023年5月29日 00:13:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定