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

问题描述 投票:0回答:1

是否有办法在实际饼图中显示实际值而不是百分比?

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

import plotly.express as px
import pandas as pd
def my_view(request):
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    a=os.path.join(BASE_DIR, 'db.sqlite3')
    con = sqlite3.connect(a)
    cur = con.cursor()
    df = pd.read_sql_query("SELECT * from POL", con)
    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}
    response = render(request, 'Home.html', context)

上面将生成所附的饼图。悬停中提到的值应显示在饼图内或工具提示上。

Pie chart generated from above code

python django django-views plotly pie-chart
1个回答
0
投票

您比预期的输出还差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()
© www.soinside.com 2019 - 2024. All rights reserved.