How can you remove the decimals of a px.bar plot with the library pyplot?
With the following dataframe:
Categoria Importe
0 OTROS 1498.60
1 alquiler 150.00
2 compra_internet 740.36
3 deportes 909.87
4 recurrente 142.55
5 restaurantes 131.75
6 servicios 405.98
7 supermercado 592.26
8 transporte 663.31
I execute the next cell to crear a bar grapich with pyplot.
fig = px.bar(df_cuatri[df_cuatri['Tipo']=='gasto'].groupby(['Categoria'], as_index = False).sum(),
x='Categoria', y='Importe', title='Último cuatrimestre', color='Categoria', text='Importe')
fig.update_layout(xaxis={'categoryarray' : ['alquiler', 'servicios', 'supermercado', 'deportes']})
fig.show()
With the parameter text='Importe' I am able to set the value on each bar, but it has many decimal, like: 663.310000000001. How can I round those values? The column of the dataframe is already round.
The format of the annotation text is specified by update_traces(texttemplate='').
import pandas as pd
import numpy as np
import io
data = '''
Categoria Importe
0 OTROS 1498.60
1 alquiler 150.00
2 compra_internet 740.360000004
3 deportes 909.8700000000001
4 recurrente 142.55
5 restaurantes 131.75
6 servicios 405.98
7 supermercado 592.259999999
8 transporte 663.310000000001
'''
df_cuatri = pd.read_csv(io.StringIO(data), delim_whitespace=True)
import plotly.express as px
fig = px.bar(df_cuatri,
x='Categoria', y='Importe', title='Último cuatrimestre', color='Categoria', text='Importe')
fig.update_layout(xaxis={'categoryarray' : ['alquiler', 'servicios', 'supermercado', 'deportes']})
fig.update_traces(texttemplate='%{text:.2f}')
fig.show()