Pie charts are great for visualizing the distribution of counts among different values of a categorical field. For example, the distribution of T-shirts sold based on size (small, medium, large, etc). In this tutorial, we’ll look at how to plot a pie chart in python using matplotlib.
How to plot a pie chart using Mapltolib?
Matplotlib is a powerful visualization library in python and comes up with a number of different charting options. You can plot a pie chart in matplotlib using the pyplot’s pie()
function. The following is the syntax:
import matplotlib.pyplot as plt
plt.pie(x, labels)
plt.show()
Here, x
is an array or sequence of counts (the wedge sizes in the pie) and labels
is the sequence of string labels to be used for each portion (wedge). labels
is an optional parameter which is None
by default.
Examples
Let’s look at some examples of using matplotlib’s pie()
function to plot a pie chart.
1. Pie chart with default parameters
Let’s say you have the sales figures for T-shirts of different sizes for a brand and want to plot a pie chart of the distribution across sizes.
import matplotlib.pyplot as plt
# T-shirt sales for sizes Small, Medium, and Large
sales = [450, 800, 760]
lables = ["Small", "Medium", "Large"]
# plot a pie chart
plt.pie(sales, labels=lables)
plt.show()
Output:
Here, we only pass the values and the labels for the pie chart and use all other parameters as default. We can see that Medium and Large had more sales than Small but it’s difficult to differentiate Medium and Large from the above figure.
2. Pie chart showing the percentage distribution of each portion
You can make the above pie chart more informative by adding data labels to each wedge (or portion) using the autopct
parameter. For example, if you want to have the % distribution of each portion you can pass a format string to autopct
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
import matplotlib.pyplot as plt
# T-shirt sales for sizes Small, Medium, and Large
sales = [450, 800, 760]
lables = ["Small", "Medium", "Large"]
# plot a pie chart
plt.pie(sales, labels=lables, autopct="%1.1f%%")
plt.title("T-Shirt sales distribution")
plt.show()
Output:
The labels here provide information on the percentage distribution of sales across the three T-shirt sizes. We can now say that sales for size Medium outperformed that of Large by a small difference. Also notice that we added a title to the chart to make its context more clear.
3. Pie chart with custom colors
You can specify custom colors for your pie chart with the colors
parameter. Pass the colors you want for your pie chart as a sequence to colors
. See the example below:
import matplotlib.pyplot as plt
# T-shirt sales for sizes Small, Medium, and Large
sales = [450, 800, 760]
lables = ["Small", "Medium", "Large"]
colors = ["wheat", "lavender", "lightblue"]
# plot a pie chart
plt.pie(sales, labels=lables, autopct="%1.1f%%", colors=colors)
plt.title("T-Shirt sales distribution")
plt.show()
Output:
You can see that portions small, medium, and large are colored wheat, lavender, and lightblue respectively. For a complete list of matplotlib’s named colors, refer to this guide.
4. Other common pie chart formatting options
There are a number of other formatting options available as well for matplotlib’s pie chart. You can “explode” (separate out) one or more portions of the pie chart with the explode
parameter. You can provide wedge properties like line width, edge color, etc. as a dictionary to the wedgeprops
parameter. There’s also an option to set text properties using the textprops
parameter.
Let’s say we want to apply the following formatting options to the above pie chart:
- Explode the wedge for the Small category.
- Add gray line borders for the chart.
- Make the text bold.
- Add shadow to the plot.
import matplotlib.pyplot as plt
# T-shirt sales for sizes Small, Medium, and Large
sales = [450, 800, 760]
# wedge labels
lables = ["Small", "Medium", "Large"]
# wedge colors
colors = ["wheat", "lavender", "lightblue"]
# explode the wedge for Small
explode = [0.2, 0, 0]
# line properties
wedge_props = {"linewidth": 2, "edgecolor": "gray"}
# text properties
text_props = {"weight": "bold"}
# plot a pie chart
plt.pie(sales,
labels=lables,
autopct="%1.1f%%",
colors=colors,
shadow=True,
explode=explode,
wedgeprops=wedge_props,
textprops=text_props)
plt.title("T-Shirt sales distribution")
plt.show()
Output:
The above is the resulting pie chart. There are a few other formatting options as well, for more refer to the official documentation.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having matplotlib version 3.2.2
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on matplotlib –
- Change Background Color of Plot in Matplotlib
- Change Font Size of elements in a Matplotlib plot
- Matplotlib – Save Plot as a File
- Change Size of Figures in Matplotlib
- Plot a Bar Chart using Matplotlib
- Plot a Pie Chart with Matplotlib
- Plot Histogram in Python using Matplotlib
- Create a Scatter Plot in Python with Matplotlib
- Plot a Line Chart in Python with Matplotlib
- Save Matplotlib Plot with Transparent Background
- Change Font Type in Matplotlib plots