This post extends the post on pie chart in matplotlib and seaborn. Despite being misunderstood, Pie charts appears frequently in most visualization reports. Pie charts are used to visualize the part of a whole comparison. They show the contribution of each category to the overall value. When properly used pie charts play an important part in presenting insights to users. In this post we will look at when to use pie charts, the best practices and how to create pie charts in plotly.

plotly-logo

When to Use Pie Charts

Pie chart is used to visualize the part of a whole relationship for categorical variable.

How to Use Pie Charts

  1. Use fewer slices as much as possible.
  2. Use annotations to provide actual values and percentage of each pie.
  3. Select colours that show distinct comparison between pies.
  4. Use legends.

Pie Charts in Plotly

Load Required Libraries

                    

import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

Load Data

                    

score_df = pd.DataFrame(
    {
        "Student": ["Tom", "Peter","Simon", "Mary", "Jane","King","Hillary","Ethan","Page"],
        "Math": [79.00, 67.00,80.00, 84.00, 70.00,60.00,90.00,76.00,75],
        "Physics":[63.00, 98, 60.00, 90,84.00, 77.00,55.00,70,66.00],
        "Computer":[84.00,78.00, 57.00, 88.00, 75.00,93.00,92.00,98.00,90.00],
    }
)

score_melt_df=pd.melt(score_df.reset_index(),id_vars=['Student'],value_vars=['Math','Physics','Computer'],
        value_name='Score')
score_melt_df['Course']=score_melt_df['variable']
score_melt_df.head()

plotly-pie-graph-load-data

Simple Pie Chart

                    

fig = px.pie(score_melt_df, values='Score',names='Student', color_discrete_sequence=px.colors.sequential.RdBu)

fig.update_layout(title={'text': 'Students Score','y':0.95,'x':0.5, 'xanchor': 'center','yanchor': 'top'},
                  autosize=True,margin=dict(t=70,b=0,l=0,r=0),
                  font=dict(size=20, family='Times New Romans', color='brown') )

fig.update_xaxes(showline=True, linewidth=1, linecolor='white', gridwidth=3, gridcolor='white', mirror=True)
fig.update_yaxes(showline=True, linewidth=1, linecolor='white', gridwidth=3, gridcolor='white', mirror=True)

fig.show()

plotly-simple-pie-chart

Explode Pie in Pie Chart

                    

fig=go.Figure()
fig.add_trace(go.Pie(labels=score_melt_df['Student'], values=score_melt_df['Score'],
             pull=[0,0,0.2,0,0,0,0,0.3,0]))

fig.update_layout(title={'text': 'Students Score','y':0.95,'x':0.5, 'xanchor': 'center','yanchor': 'top'},
                          legend=dict(yanchor="top",y=0.98,xanchor="right",x=0.95),
                  autosize=True,margin=dict(t=70,b=0,l=0,r=0), 
                  font=dict(size=20, family='Times New Romans', color='brown') )

fig.show()

plotly-explode-pie-chart

Donought Pie Graph

                    

fig=go.Figure()
fig.add_trace(go.Pie(labels=score_melt_df['Student'], values=score_melt_df['Score']))
fig.update_traces(hole=.5)

fig.update_layout(title={'text': 'Students Score','y':0.95,'x':0.5, 'xanchor': 'center','yanchor': 'top'},
                          legend=dict(yanchor="top",y=0.98,xanchor="right",x=0.95),
                  annotations=[dict(text='Score', x=0.50, y=0.5, showarrow=False)], 
                  font=dict(size=20, family='Times New Romans', color='brown') )

fig.show()

plotly-donought-pie-chart

For complete code check the jupyter notebook here.

Conclusion

In this post we have looked at Pie Charts, one of the most unwanted and often branded as confusing types of visualization, pie charts plays an important role in presenting important insights about data when properly used. They are used to show the part of a whole relationship in data. We have looked at what they are, when to use them, how to use pie charts and how to create them in plotly. In the next post we will look at scatter plots in plotly. To learn about the Famous Bar graph in plotly check our previous post here. To learn about Pie Chart in seaborn check our post here.

Pie Charts in Plotly

Post navigation


0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x