简体   繁体   English

Plotly - 带有百分比和一位小数的饼图

[英]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.我在将饼图(绘图全局对象)的标签设置为仅小数点后 1 位的百分比时遇到问题。

When I set texttemplate = "%{percent}"当我设置 texttemplate = "%{percent}"

with texttemplate = "%{percent:.1f}" 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:也许只需添加 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.感谢您抽出时间 Waket Zheng,但由于未定义percent ,我遇到了错误。

After reading texttemplate documentation I've found out that it was needed to change the syntax of the parameter:阅读texttemplate 文档后,我发现需要更改参数的语法:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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