如何在 Plotly 饼图中以数字而不是百分比显示值?

How to display values in numbers instead of precentages in Plotly Pie chart?

有没有办法在 plotly 饼图上显示实际值而不是百分比?

下面是示例代码,它是 views.py 文件的一部分。然后将图形变量传递给 HTML 文件以显示从 plotly 生成的交互式图像。

import plotly.express as px
import pandas as pd
def my_view(request):

    esd = df.groupby('ProjectStatus', as_index=False).agg({"ProjectID": "count"})
    fig = px.pie(esd, values=Tasks, names=my_labels, color_discrete_sequence=px.colors.sequential.Blugrn)
    graph = fig.to_html(full_html=False, default_height=350, default_width=500)
    context = {'graph': graph}

以上将生成附加的饼图。悬停时提及的值应显示在饼图中或工具提示中。

Pie chart generated from above code

您与预期输出相差 fig.update_traces

import plotly.express as px
df = px.data.tips()
fig = px.pie(df, values='tip', names='day')
# You should add this
fig.update_traces(hoverinfo='label+percent', textinfo='value')
fig.show()