Rishabh Aher
Rishabh Aher

Reputation: 71

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

Is there a way to display the actual values instead of percentage on the plotly pie chart?

Below is the sample code which is a part of views.py file. The graph variable is then passed to HTML file to display the interactive image generated from 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}

The above would generate the attached pie chart. The values mentioned on hover should be displayed inside pie chart or on tooltip.

Pie chart generated from above code

Upvotes: 3

Views: 5217

Answers (1)

rpanai
rpanai

Reputation: 13437

You are a fig.update_traces away from your expected output.

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

Upvotes: 4

Related Questions