Pie chart is a visualization which displays the numerical proportion of each attribute in the data set. A donut chart is a pie chart, except there is a hole in the center of the pie. In this folder, we will go over how to create bar charts with Python and Plotly.
The following scripts are used in this chapter:
- Simple_pie.py
- Simple_donut.py
This chapter requires the following packages for the scripts used:
- Pandas
- Plotly
This chapter may use the following data from the Data folder:
- revenue_dept.csv
- expense_dept.csv
revenue.csv is a data set to display the revenue of a hypothetical department store by category, without a time dimension.
expense.csv is a data set to display the expense of a hypothetical department store by category, without a time dimension.
Data is a list of go.Pie(), each go.Pie() represents a pie chart. Each go.Pie() you added in the data list, one more pie chart will be displayed. If you want a donut chart, add a hole parameter in go.Pie() to convert the pie chart to a donut chart
go.Pie() has the following parameters:
- label: Attribute of the chart
- value: Value for the given attribute, takes the values (not percentage, unless you insists)
- name: Category, which will be used for identifying the pie chart (More useful when you have more than 1 pie chart)
- hole: The size of the hole in the center, takes between 0 and 1. It represents the hole radius relative to the radius of the pie. It defaults at 0, as a pie chart. When hole is greater than 0, it becomes a donut chart
- hoverinfo: What information to be displayed when user hover over the pie slices, all the options are:
- percent
- label+percent
- label
- name
- textinfo: What information to be displayed on the pie slices, default as percent, all the options are:
- percent
- label+percent
- label
- name
- textfont_size: Font of the text on the pie
- marker (Dictionary): To define colour of each pie and borders
- colors: A list of colours to be used for pies
- line (Dictionary): To specify color and width for border
- color
- width
- insidetextorientation: Controls the orientation of the text inside chart sectors
- auto: Oriented in any direction to have the largest font possible
- horizontal: Parallel with the bottom line (Horizaontal)
- radial: Oriented along the radius
- tangential: Perpendicular to the radius
- pull: "Pull-out" the pie from the center, take 0-1 for each pie
Genetic Layout parameters suggested to use:
- title (Dictionary): Chart title and fonts
- text: Chart title to be displayed
- x: text location on x-dimension, from 0-1
- y: text location on y-dimension, from 0-1
- uniformtext_minsize: Threhold of font size to be displayed on the pie, if not, will be handled with instruction stated in uniformtext_mode, default to be 0
- uniformtext_mode: How to handle text if the font is less than uniformtext_minsize
- hide: Do not display
- show: Display without downscaling
- texttemplate: What to display on/outside of the bars
Pie/Donut Chart Exclusive parameters:
- hole: The size (hole radius) of the hole in the center
- pull: "Pull-out" the pie from the center
- insidetextorientation: Controls the orientation of the text inside chart sectors
Require files: revenue_dept.csv in the Data folder.

data = []
data.append(go.Pie(labels=df['category'], values=df['revenue']))
# Layout
layout = {'title':{'text':'Department Store Revenue', 'x':0.5}}
Require files: revenue_dept.csv in the Data folder.

# Data
data = []
data.append(go.Pie(labels=df['category'], values=df['revenue'], hole=0.4))
# Layout
layout = {'title':{'text':'Department Store Revenue', 'x':0.5}}
Require files: revenue_dept.csv in the Data folder.
# Data
colours = ['blue','gold','red','green']
data = []
data.append(go.Pie(labels=df['category'], values=df['revenue'],
hoverinfo='label+percent', textinfo='value', textfont_size=25,
marker={'colors':colours, 'line':{'color':'black','width':3}},
insidetextorientation='radial',pull=[0, 0, 0.3, 0]))
# Layout
layout = {'title':{'text':'Department Store Revenue', 'x':0.5}}
Require files: revenue_dept.csv and expense_dept.csv in the Data folder.

# Define fig
fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])
# Data
# marker colors may added in go.Pie
fig.add_trace(go.Pie(labels=df_revenue['category'],
values=df_revenue['revenue'],name='Revenue'),1,1)
fig.add_trace(go.Pie(labels=df_expense['category'],
values=df_expense['expense'],name='Expense'),1,2)
# Layout
fig.update_layout(title_text='Department Store Revenue',title_x=0.5)
Multiple Pie charts required users to declare subplots in figure first that it requires different style of code to do so, see Multiple_pies.py in this folder for the code.
Plotly Documentation Pie Chart