Proportion Matters: How to Use pie chart for visualization.
What is a Pie Chart?
A pie chart shows how much each part makes up of a whole. It looks like a circle that is cut into pieces, like a pie. Each piece stands for a different thing that is part of the whole. For example, you can use a pie chart to show how many people like different kinds of fruits. The circle is the total number of people, and the pieces are the number of people who like apples, bananas, oranges, etc. The bigger the piece, the more people like that fruit. You can also write the number or percentage of people on each piece to make it clearer. Pie charts are good for showing things like sales, surveys, or resources in a simple way.
Example
The pie chart shows how people voted in a made-up city election. The first blue piece is for Reyes, who got almost half of the votes. The yellow piece is for Chu, who got about one out of three votes. The purple piece is for Williams, who got about one out of five votes. The numbers on the top right tell us the exact percentages, but the pie chart shows us the big picture of who got more or less votes.
How to read a Pie Chart?
To understand a pie chart, you must consider the area, arc length, and angle of every slice. Because it can be hard to compare the slices, meaningful organization is key. Slices in a pie chart should be organized in a coherent way, usually the biggest to smallest, to make it easier for the user to interpret. Start at the biggest piece and work your way down to the smallest to properly digest the data. The colors of the slices should match their respective blocks in the legend, so that viewers do not need to consult the legend as much.
When to use a pie chart
Pie charts are good for showing how parts make up a whole. You need a whole amount that can be split into different parts. You want to see how much each part adds to the whole, not how they compare to each other. If not, use a different chart.
There are two main types of data that can make up a whole and be split into categories. One type is when the whole is a total number. For example, how many votes each person got in an election, or how many times different users bought something (like guests, new users, or existing users).
Another type of data that can make up a whole and be split into categories is when the whole is a total value. For example, we might care more about the money from all the transactions than how many transactions there were. We can split this money by things like user type, age group, or place to see where the business makes more money.
What type of analysis do Pie Charts support
Pie charts should be used to show the relationship of different parts to the whole. They work best with dimensions that have a limited number of categories. If you need to emphasize that one section of the whole is overrepresented or underrepresented, a pie chart can make that story within the data stand out. Pie charts do not work well for comparing exact numbers
Tips for creating a good pie chart.
include annotations.
Write the numbers or percentages on the pie chart or next to it. Pie charts are hard to read because you can’t tell how big each piece is, unless it is half, a third, or a quarter. Also, if you want to show amounts instead of percentages, pie charts don’t have marks to show that. That’s why you need to write the numbers or percentages. You can use arrows or lines to connect them to the pieces.
Consider the order of slices.
A good order for slices can make it much easier for a reader to understand what the plot is saying. A typical ordering goes from the largest slice to the smallest slice, very useful when there are categories with very similar values. However, if the category levels have an inherent ordering, then plotting slices in that order is usually better.
As for choosing a start point, it’s a good idea to plot slices from a cardinally oriented direction. Visualization tools will usually start from the right or from the top. While starting from the right has a mathematical basis regarding conventions on measuring angles, starting from the top feels more intuitive, since it matches how we read from top to bottom, and how we think about progression of time on a clock or watch face.
Limit the number of pie slices.
Pie charts with a large number of slices can be difficult to read. It can be difficult to see the smallest slices, and it can be difficult to choose enough colors to make all of the slices distinct. Recommendations vary, but if you have more than about five categories, you might want to think about using a different chart type. As another option, you might consider lumping small slices into a single ‘other’ slice, colored in a neutral gray.
Avoid distorting effects.
Reading a pie chart accurately requires that the slices’ areas, arc lengths, and angles all accurately represent the data. Avoiding 3D effects is important for any plot, but especially for pie charts. Squashing or stretching the circle or adding unnecessary depth can distort how large each slice compares to the whole. Another distortion can come from an ‘exploded’ pie chart, where slices are pulled out from the center for emphasis. This can make it more difficult to gauge the part-to-whole comparison.
Pie Chart for Visual Analysis
If you have a dimension with just a couple of categories to compare, then a pie chart can help display each value of a category within the whole. The chart should read as a comparison of each group to each other, forming a whole category. The “whole” could be anything so long as the category can be split into separate slices that are distinguishable from each other.
Creating Pie Chart Using matplotlib
Matplotlib has pie () function in its pyplot module which create a pie chart representing the data in an array.
Example
# Import libraries
from matplotlib import pyplot as plt
import numpy as np
# Creating dataset
cars = ['TOYOTA', 'HONDA', 'PEUGEOUT',
'VOLKSWAGEN', 'OPEL', 'BMW']
data = [23, 17, 35, 29, 12, 41]
# Creating plot
fig = plt.figure(figsize =(10, 7))
plt.pie(data, labels = cars)
# show plot
plt.show()
Output
How to Create Pie Chart from Pandas DataFrame using python
A pie chart is a circular graph that represents data. It is frequently used in research, engineering, and business. The segments of the pie depict the relative strength of the data and provide a graphical representation. A pie chart requires a list of categories and numerical variables. The term “pie” refers to the whole, while “slices” refers to the individual components. The pie is divided into segments and sectors, each representing a portion of the whole chart (percentage). All of the data adds up to 360 degrees and the entire value of the pie is always 100 percent.
Let us first create a simple Pie chart.
Simple Pie chart
For this first, all required modules are imported and a dataframe is initialized. To plot a pie chart plot () function is used and the kind attribute is set to pie.
Syntax
plot(kind='pie')
Example
import pandas as pd
# DataFrame of each student and the votes they get
dataframe = pd.DataFrame({'Name': ['Faridah', 'Faridah', 'Faridah',
'Faridah', 'Faridah', 'Nasir',
'Nasir', 'Nasir', 'Nasir', 'Nasir',
'Mubarak', 'Mubarak', 'Mubarak',
'Mubarak', 'Mubarak'],
'votes_of_each_class': [12, 9, 17, 19, 20,
11, 15, 12, 9, 4,
22, 19, 17, 19, 18]})
# Plotting the pie chart for above dataframe
dataframe.groupby(['Name']).sum().plot(kind='pie', y='votes_of_each_class')
Output
Pie Chart as Percentage
To add percentage autopct attribute is set to an appropriate value, this automatically adds percentages to each section.
Syntax:
plot(kind='pie', autopct)
Example
import pandas as pd
# DataFrame of each student and the votes they get
dataframe = pd.DataFrame({'Name': ['Faridah', 'Faridah', 'Faridah',
'Faridah', 'Faridah', 'Nasir',
'Nasir', 'Nasir', 'Nasir', 'Nasir',
'Mubarak', 'Mubarak',
'Mubarak', 'Mubarak',
'Mubarak'],
'votes_of_each_class': [12, 9, 17, 19,
20, 11, 15, 12,
9, 4, 22, 19, 17,
19, 18]})
# Plotting the pie chart for above dataframe
dataframe.groupby(['Name']).sum().plot(kind='pie', y='votes_of_each_class', autopct='%1.0f%%')
Output
How to Create Pie Chart using Seaborn
Seaborn is a library for making graphs in Python. It is based on another library called matplotlib and makes it easy to create nice-looking and informative graphs. Matplotlib is a library for making many different types of graphs in Python. It can make static, animated, and interactive graphs and works with other libraries in the SciPy stack.
Seaborn is a library for data visualization in python . it is based on another library called matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.
Syntax to install seaborn.
pip install seaborn
If we want to create a pie chart using seaborn in Python, we have to use the pie attribute of Matplotlib
and the color pallets of Seaborn. We have to pass the input data and the color pallet to create a pie chart.
Example
let’s create a pie chart of some random data
import matplotlib.pyplot as plt
import seaborn as sns
data = [35, 21, 29, 39, 11]
colors = sns.color_palette('pastel')
plt.pie(data, colors = colors)
plt.show()
Output
In the above code, we have used the pastel color pallet of Seaborn, but we can change the color pallet as we like. Seaborn has a lot of color pallet options available like deep, muted, colorblind, bright, and dark.
We can also add text inside the slices using the autopct
parameter. For example, let’s make the above pie chart more readable by adding labels, changing the color pallet, and adding the slice percentage inside each slice.
Example
import matplotlib.pyplot as plt
import seaborn as sns
data = [35, 21, 29, 39, 11]
labels = ['slice 1', 'slice 2', 'slice 3', 'slice 4', 'slice 5']
colors = sns.color_palette('bright')
plt.pie(data, labels=labels,colors = colors, autopct = '%0.0f%%')
plt.show()
Output
Example
Let’s take 6 classes with some students in it and plot a pie chart on the basic number of students in each class.
# importing libraries
import matplotlib.pyplot as plt
import seaborn
# declaring data
data = [44, 45, 40, 41, 39, 30]
keys = ['jss 1', 'jss2 2', 'jss3', 'ss1', 'ss2', 'ss3']
# define Seaborn color palette to use
palette_color = seaborn.color_palette('bright')
# plotting data on chart
plt.pie(data, labels=keys, colors=palette_color, autopct='%.0f%%')
# displaying chart
plt.show()
Output
we can also plot a pie chart with different palette and explode.
# importing libraries
import matplotlib.pyplot as plt
import seaborn
# declaring data
data = [44, 45, 40, 41, 39, 30]
keys = ['jss 1', 'jss2 2', 'jss3', 'ss1', 'ss2', 'ss3']
# declaring exploding pie
explode = [0, 0.1, 0, 0, 0, 0.1]
# define Seaborn color palette to use
palette_color = seaborn.color_palette('dark')
# plotting data on chart
plt.pie(data, labels=keys, colors=palette_color,
explode=explode, autopct='%.0f%%')
# displaying chart
plt.show()
Output
Pie charts can be useful for showing data that has a clear and meaningful whole, such as total revenue, votes, or population. However, they are not suitable for showing data that has no natural whole, such as averages, rates, or ratios. They are also not good for showing changes over time or comparing multiple groups of data. In those cases, other chart types such as bar charts, line charts, or scatter plots might be more appropriate.
In conclusion, pie charts are a popular and easy way of presenting data that shows how a whole is divided into parts. However, they also have some drawbacks and challenges that require careful design and interpretation. By following some simple guidelines and choosing the right data for pie charts, they can be an effective tool for data visualization.
Thanks for reading.
Reference
Understanding and using Pie Charts | Tableau
A Complete Guide to Pie Charts | Tutorial by Chartio
Pie Charts: Using, Examples, and Interpreting — Statistics By Jim