【问题标题】:Plotly - Pie chart with percentage and one decimal casePlotly - 带有百分比和一位小数的饼图
【发布时间】:2022-07-22 22:39:37
【问题描述】:

我在将饼图的标签(全局对象)设置为仅保留 1 个小数位的百分比时遇到问题。

When I set texttemplate = "%{percent}"

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

我现在的代码:

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

【问题讨论】:

  • 不要将相关信息作为图片链接发布。

标签: python plotly percentage


【解决方案1】:

也许只是添加一个 f:

% 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'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-17
    • 2019-10-25
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    相关资源
    最近更新 更多