Adjust thousands separator to comma with Plotly library in Python

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

Adjust thousands separator to comma with Plotly library in Python

问题

我正在使用Plotly库处理子图。然而,我遇到了一些无法解决的问题。这些问题包括:

  • 我需要y轴的值以0.00%的格式显示(0,00%; 50,00%; 100,00%);
  • 我需要柱形图的值以逗号分隔而不是句点;
  • 我需要将技术的图例(1D-CNN; FT-Transformer等)放在图表上方。

到目前为止开发的代码如下:

import plotly.subplots as sp
import plotly.graph_objects as go
import numpy as np

tecnicas = ['1D-CNN', 'FT-Transformer', 'MLP', 'NODE', 'TabNet', 'TabTransform']
medidas_desempenho = ['T.A/HI', 'PCA', 'S.A', 'UNDER', 'OVER']

dados = np.array([
    [59.11, 65.7, 63.1, 63.47, 65.89, 63.38],
    [60.13, 28.71, 61.15, 53.15, 63.47, 21.56],
    [59.57, 64.21, 60.5, 65.42, 66.45, 65.89],
    [57.43, 65.7, 58.27, 62.36, 65.25, 62.54],
    [57.8, 63.75, 63.38, 65.15, 66.72, 35.03]
])

cores = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b']

fig = sp.make_subplots(rows=len(medidas_desempenho), cols=1, subplot_titles=medidas_desempenho, shared_xaxes=True)

dados_formatados = np.around(dados, decimals=2)

for i, medida in enumerate(medidas_desempenho):
    fig.add_trace(go.Bar(
        x=tecnicas,
        y=dados_formatados[i],
        name='',
        text=['{:.2f}%'.format(value) for value in dados_formatados[i]],
        textposition='outside',
        marker=dict(color=cores),
        hovertemplate=None
    ), row=i+1, col=1)

fig.update_layout(
    height=900,
    barmode='group',
    showlegend=False,
    plot_bgcolor='rgba(240,240,240,0.8)',
    paper_bgcolor='rgba(240,240,240,0.8)',
    margin=dict(t=50, b=50),
    font=dict(family='Arial', size=12, color='black')
)

fig.update_yaxes(range=[0, 100], tickformat=".2f%", separatethousands=True)
fig.show()

希望这能帮助你解决问题。

英文:

I'm working with subplots with the Plotly library.
However, I stopped at some issues that I cannot get around. These are:

  • I need the y-axis values to be in the format 0.00% (0,00%; 50,00%; 100,00%);
  • I need the values of the bars to be separated by commas and not by a period;
  • I need to put the legend of the techniques (1D-CNN; FT-Transformer etc) above the graph.

The code developed so far is below:

import plotly.subplots as sp
import plotly.graph_objects as go
import numpy as np
tecnicas = ['1D-CNN', 'FT-Transformer', 'MLP', 'NODE', 'TabNet', 'TabTransform']
medidas_desempenho = ['T.A/HI', 'PCA', 'S.A', 'UNDER', 'OVER']
dados = np.array([
[59.11, 65.7, 63.1, 63.47, 65.89, 63.38],
[60.13, 28.71, 61.15, 53.15, 63.47, 21.56],
[59.57, 64.21, 60.5, 65.42, 66.45, 65.89],
[57.43, 65.7, 58.27, 62.36, 65.25, 62.54],
[57.8, 63.75, 63.38, 65.15, 66.72, 35.03]
])
cores = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b']
fig = sp.make_subplots(rows=len(medidas_desempenho), cols=1, subplot_titles=medidas_desempenho, shared_xaxes=True)
dados_formatados = np.around(dados, decimals=2)
for i, medida in enumerate(medidas_desempenho):
fig.add_trace(go.Bar(
x=tecnicas,
y=dados_formatados[i],
name='',
text=['{:.2f}%'.format(value) for value in dados_formatados[i]],
textposition='outside',
marker=dict(color=cores),
hovertemplate=None
), row=i+1, col=1)
fig.update_layout(
height=900,
barmode='group',
showlegend=False,
plot_bgcolor='rgba(240,240,240,0.8)',
paper_bgcolor='rgba(240,240,240,0.8)',
margin=dict(t=50, b=50),
font=dict(family='Arial', size=12, color='black')
)
fig.update_yaxes(range=[0, 100], tickformat=".2f%", separatethousands=True)
fig.show()

答案1

得分: 1

I see three ways to address this challenge: first, add a comma for percentages at the data stage and convert to commas; second, switch to a locale that displays commas; third, convert to commas at the annotation stage; and fourth, add a comma to the data and convert to percentages at the annotation stage. The same technique can be used to change the scale of the y-axis. As for the legend, the legend can be created by converting the current data into a data frame and then changing the loop process to draw a bar graph for each column. Currently, Plotly does not allow you to create your own legend.

fig = sp.make_subplots(rows=len(medidas_desempenho), cols=1, subplot_titles=medidas_desempenho, shared_xaxes=True)

dados_formatados = np.around(dados, decimals=2)

for i, medida in enumerate(medidas_desempenho):
    fig.add_trace(go.Bar(
        x=tecnicas,
        y=dados_formatados[i],
        name='',
        text=[x.replace('.', ',') for x in ['{:.2f}%'.format(value) for value in dados_formatados[i]]],
        textposition='outside',
        marker=dict(color=cores),
        hovertemplate=None,
        showlegend=True
    ), row=i+1, col=1)

fig.update_layout(
    height=1100,
    barmode='group',
    showlegend=False,
    plot_bgcolor='rgba(240,240,240,0.8)',
    paper_bgcolor='rgba(240,240,240,0.8)',
    margin=dict(t=50, b=50),
    font=dict(family='Arial', size=12, color='black')
)

# fig.update_yaxes(range=[0.0, 100.0], tickformat=".2f%", separatethousands=True)
fig.update_yaxes(range=[0, 100], tickvals=[0,50,100], ticktext=[x.replace('.', ',') for x in ['{:.2f}%'.format(x) for x in np.arange(0,101,50)]])
fig.show()

Adjust thousands separator to comma with Plotly library in Python

英文:

I see three ways to address this challenge: first, add a comma for percentages at the data stage and convert to commas; second, switch to a locale that displays commas; third, convert to commas at the annotation stage; and fourth, add a comma to the data and convert to percentages at the annotation stage. The same technique can be used to change the scale of the y-axis. As for the legend, the legend can be created by converting the current data into a data frame and then changing the loop process to draw a bar graph for each column. Currently, Plotly does not allow you to create your own legend.

fig = sp.make_subplots(rows=len(medidas_desempenho), cols=1, subplot_titles=medidas_desempenho, shared_xaxes=True)
dados_formatados = np.around(dados, decimals=2)
for i, medida in enumerate(medidas_desempenho):
fig.add_trace(go.Bar(
x=tecnicas,
y=dados_formatados[i],
name='',
text=[x.replace('.',',') for x in ['{:.2f}%'.format(value) for value in dados_formatados[i]]],
textposition='outside',
marker=dict(color=cores),
hovertemplate=None,
showlegend=True
), row=i+1, col=1)
fig.update_layout(
height=1100,
barmode='group',
showlegend=False,
plot_bgcolor='rgba(240,240,240,0.8)',
paper_bgcolor='rgba(240,240,240,0.8)',
margin=dict(t=50, b=50),
font=dict(family='Arial', size=12, color='black')
)
#fig.update_yaxes(range=[0.0, 100.0], tickformat=".2f%", separatethousands=True)
fig.update_yaxes(range=[0, 100], tickvals=[0,50,100], ticktext=[x.replace('.',',') for x in ['{:.2f}%'.format(x) for x in np.arange(0,101,50)]])
fig.show()

Adjust thousands separator to comma with Plotly library in Python

huangapple
  • 本文由 发表于 2023年7月3日 08:09:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601221.html
匿名

发表评论

匿名网友

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

确定