Plotly Dash 饼图 - 段文本颜色和小数位

Plotly Dash Pie Chart - Segment Text Color and Decimal Places

我创建了一个饼图(下面的代码)。即使我在布局中将字体设置为白色,每个饼图段顶部的 % 值的字体颜色也会有所不同(黑色或白色)。我希望它在所有饼图段上都是白色文本。

此外,百分比值的小数位数因饼图段而异,从一位小数到三位小数。我想将它设置为小数点后一位。有什么建议吗?

我无法分享饼图的图像,因为它包含一些敏感数据。

fig =  dcc.Graph(
            id='graph',
            figure={
                'data': [{
                        'values': df['count'],
                        'labels': df['labels'],
                        'type': 'pie'
                        }],
                'layout': {
                    'margin': {'l': 30,'r': 0,'b': 30,'t': 30,},
                     "plot_bgcolor": "#111111",
                    "paper_bgcolor": "#111111",
                    "font": {"color": 'white'},
                    'legend': {'x': 0, 'y': 1},
                }
            }
        )

对于段内的文本颜色,您可以设置 insidetextfont:

Code: fig.update_traces(insidetextfont=dict(...), selector=dict(type='pie')) Type: dict containing one or more of the keys listed below. Sets the font used for textinfo lying inside the sector.

对于百分比精度,您可以设置 texttemplate

Code: fig.update_traces(texttemplate=, selector=dict(type='pie')) Type: string or array of strings Default: ""

Template string used for rendering the information text that appear on points. Note that this will override textinfo. Variables are inserted using %{variable}, for example "y: %{y}". Numbers are formatted using d3-format's syntax %{variable:d3-format}, for example "Price: %{y:$.2f}". https://github.com/d3/d3-3.x-api-reference/blob/master/Formatting.md#d3_format for details on the formatting syntax. Dates are formatted using d3-time-format's syntax %{variable|d3-time-format}, for example "Day: %{2019-01-01|%A}". https://github.com/d3/d3-time-format#locale_format for details on the date formatting syntax. Every attributes that can be specified per-point (the ones that are arrayOk: True) are available. variables label, color, value, percent and text.

基于您的代码的示例:

fig = {
    "data": [
        {
            "values": df["count"],
            "labels": df["labels"],
            "type": "pie",
            "insidetextfont": {"color": "white"},
            "texttemplate": "%{percent:.2%f}",
        }
    ],
    "layout": {
        "margin": {
            "l": 30,
            "r": 0,
            "b": 30,
            "t": 30,
        },
        "plot_bgcolor": "#111111",
        "paper_bgcolor": "#111111",
        "font": {"color": "white"},
        "legend": {"x": 0, "y": 1},
    },
}