6. Making Pie and Donut Charts#
A pie chart, also known as a circle chart, is a graphic used to illustrate numerical proportions in a visually intuitive way. The chart is divided into slices, where the size of each slice—determined by its arc length, central angle, and area—corresponds to the relative quantity it represents. This makes pie charts especially useful for comparing parts of a whole. Named for its resemblance to a sliced pie, there are several variations in how the data can be presented, such as exploded pie charts or the donut charts.
The pie chart first appeared in 1801 in a publication named “Statistical Breviary” by Scottish engineer William Playfair. In this publication, Playfair used a variety of graphs to present geographical areas, populations, and revenues of European states.
Pie and donut charts are widely used in different industries suchas as finance, economics, and media. However, they have been criticised, and many experts recommend avoiding them, as research has shown it is difficult to compare different sections of a given pie chart, or to compare data across different pie charts.
🚀 When to use them:
Pie charts are particularly effective when displaying the composition of a whole and illustrating proportions in a simple, intuitive manner. They work best when there are a limited number of categories (typically no more than five or six) and when the differences between the segments are distinct. Because they provide an immediate visual impression of relative sizes, they are an excellent choice for non-technical audiences who need a quick understanding of data distribution.
⚠️ Be aware:
Despite their advantages, pie charts have significant limitations that can make them less effective in certain situations. For example, when there are many categories or the differences between values are small, the slices become difficult to compare accurately.Also, pie charts should be avoided when data needs to be compared across multiple categories or over time, as they do not effectively show trends or relationships between values. Last but not least, in cases where precise numerical values are important, a table or a bar chart often provides a clearer and more accurate representation of the data.
It is also worth mentioning that human perception struggles with assessing angles and areas precisely, making bar charts or column charts a better alternative for highlighting subtle differences.
Getting ready#
Create a DataFrame
containing the data that we need for this recipe
import pandas as pd
data = {'Source': ['Other renewables', 'Biofuels', 'Solar', 'Wind', 'Hydropower', 'Nuclear',
'Gas', 'Oil', 'Coal', 'Traditional biomass'],
'Consumption': [2427.8613 ,1317.6246, 4264.261, 6040.359, 11014.117,
6824.1772, 40101.74, 54564, 45564.93,11111]
}
df = pd.DataFrame(data)
df.head()
Source | Consumption | |
---|---|---|
0 | Other renewables | 2427.8613 |
1 | Biofuels | 1317.6246 |
2 | Solar | 4264.2610 |
3 | Wind | 6040.3590 |
4 | Hydropower | 11014.1170 |
How to do it#
Using plotly Express
#
Import the
plotly.express
module aspx
import plotly.express as px
Make a minimal pie chart by calling the function
pie
fig = px.pie(df, values='Consumption', names='Source')
fig.show()
Add a title to your chart by passing a string as the input
title
into the functionpie
; and customise the size of the figure by using the inputsheight
andwidth
. Both have to be integers and correspond to the size of the figure in pixels.
fig = px.pie(df, values='Consumption', names='Source',
width=800, height=500,
title='Global primary energy consumption by source 2023')
fig.show()
Customise the colors used in the scatter by using the input
color_discrete_sequence
fig = px.pie(df, values='Consumption', names='Source',
color_discrete_sequence=px.colors.sequential.deep,
width=800, height=500,
title='Global primary energy consumption by source 2023')
fig.show()
my_colors = ["gold", "mediumturquoise", "darkorange", "lightgreen", "pink",
"cyan", "purple", "green", "orange", "white"]
fig = px.pie(df, values='Consumption', names='Source',
color_discrete_sequence=my_colors,
width=800, height=500,
title='Global primary energy consumption by source 2023')
fig.show()
Customise the hover data and add labels
fig = px.pie(df, values='Consumption', names='Source',
hover_data=['Consumption'],
labels={'Consumption':'TWh, Substituted energy'},
color_discrete_sequence=px.colors.sequential.deep,
width=800, height=500,
title='Global primary energy consumption by source 2023')
fig.update_traces(textposition='inside', textinfo='percent+label')
fig.show()
Transform the pie into a donut by adding the input
hole
. This receives a double between zero and one which corresponds to the percentage of the hole.
fig = px.pie(df, values='Consumption', names='Source',
hole=0.5,
color_discrete_sequence=px.colors.sequential.deep,
width=800, height=500,
title='Global primary energy consumption by source - 2023')
fig.show()
Using Plotly Go#
Import the Plotly Graph Objects library as
go
import plotly.graph_objects as go
fig = go.Figure(
data=[
go.Pie(
labels=df.Source,
values=df.Consumption,
hole=0.5,
marker = dict(colors=px.colors.sequential.deep,)
)
],
layout=dict(title="Global primary energy consumption by source - 2023",
width=800, height=500)
)
fig.show()
df = df.sort_values(by='Consumption', ascending=False)
df
Source | Consumption | |
---|---|---|
7 | Oil | 54564.0000 |
8 | Coal | 45564.9300 |
6 | Gas | 40101.7400 |
9 | Traditional biomass | 11111.0000 |
4 | Hydropower | 11014.1170 |
5 | Nuclear | 6824.1772 |
3 | Wind | 6040.3590 |
2 | Solar | 4264.2610 |
0 | Other renewables | 2427.8613 |
1 | Biofuels | 1317.6246 |
fig = go.Figure(
data=[
go.Pie(
labels=df.Source,
values=df.Consumption,
hole=0.5,
marker = dict(colors=px.colors.sequential.deep,)
)
],
layout=dict(title="Global primary energy consumption by source - 2023",
width=800, height=500)
)
fig.show()
my_colors = ["gold", "mediumturquoise", "darkorange", "lightgreen", "pink",
"cyan", "purple", "green", "orange", "white"]
fig = go.Figure(
data=[
go.Pie(
labels=df.Source,
values=df.Consumption,
hole=0.5,
marker=dict(colors=my_colors)
)
],
layout=dict(title="Global primary energy consumption by source - 2023",
width=800, height=500)
)
fig.show()