PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, April 23, 2022

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

 April 23, 2022     pie-chart, plotly, python     No comments   

Issue

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


Solution

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


Answered By - rpanai
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing