You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly全局对象饼图:百分比保留一位小数的标签设置问题

解决Plotly饼图标签显示一位小数百分比的问题

你的问题出在update_traces里的textinfo='value'配置——这个参数会强制饼图标签显示原始数值,直接覆盖了你设置的texttemplate百分比格式规则。

要实现仅显示一位小数的百分比标签,需要做两处调整:

  • 把textinfo改成'percent'(只显示百分比)或者'label+percent'(同时显示标签和百分比)
  • 调整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='percent',  # 改为percent,让texttemplate生效
                  textfont_size=10, 
                  sort=True,
                  direction='clockwise', 
                  rotation = 0)
fig.show()

如果需要同时显示标签和百分比,就把textinfo设为'label+percent',texttemplate可以改成"%{label}: %{percent:.1f}%",这样标签和百分比会组合显示。

内容的提问来源于stack exchange,提问作者vitordj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 17:24:29