简体   繁体   中英

Plotly - Pie chart with percentage and one decimal case

I'm having trouble while setting the labels of my pie chart (plotly Global Object) to percentage with only 1 decimal place.

When I set texttemplate = "%{percent}"

with texttemplate = "%{percent:.1f}"

My code right now:

fig = go.Figure(data=[go.Pie(labels=X,
    values=Y,
    textfont = {'family': "Montserrat"},
    texttemplate = "%{percent:.1f}",
    marker = {'colors': paleta, 'line':{'color': 'white', 'width':2}}
    )])
fig.update_traces(hoverinfo='label+percent', textinfo='value', textfont_size=10, sort=True,
    direction='clockwise', rotation = 0)
fig.show()

Maybe just add af:

% ipython                                                            22-07-22 - 22:32:24
Python 3.8.1 (v3.8.1:1b293b6006, Dec 18 2019, 14:08:53)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from decimal import Decimal

In [2]: percent = Decimal('5.323')

In [3]: "%{percent:.1f}"
Out[3]: '%{percent:.1f}'

In [4]: f"%{percent:.1f}"
Out[4]: '%5.3'

Thank you for the time Waket Zheng, but I've got an error as percent was not defined.

After reading texttemplate documentation I've found out that it was needed to change the syntax of the parameter:

Instead of texttemplate = "%{percent:.1f}"

Using texttemplate = "%{value:.1%}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM