In this article, we will take a look at how we can plot a pie chart in Python using Matplotlib. But we wont stop there. Because we will also be looking at how to create labels & it’s explode feature.
Table of Contents
Plot Pie Chart In Python Using Matplotlib – The Basics
But before we do any of this, we first need to learn what a Pie Chart is. What is it used for. Because only after knowing this will be able to play with it. Does that make sense? Great! So then let us start from there!
What Is A Pie Chart?
A Pie Chart is a circular representation of data. Where we will show each data in the data set as a sector in the pie chart. Wait! How are we going to show the value of that data then? Well it is simple. Each of the sector’s arc size will show the value of that data!
Where Do We Use A Pie Chart?
Okay all that is well and good. But where do we use a Pie Chart? Well, a Pie Chart is very useful when we want to compare the sector size to the size of the Pie Chart. So in other words, it is useful when we want to compare a value against the total sum of all the values! Does that make sense?
How To Plot A Pie Chart In Python
Okay. Now that we know what a Pie Chart is, let us see how we can plot one using Python. Once again, Matplotlib library comes to our rescue!
So we can use our Matplotlib library to plot a Pie chart in Python. But how do we do that? Well, take a look at the code below for that:
import matplotlib.pyplot as plt x = [10, 25, 18] plt.pie(x) plt.show()
So as you can see from the above code, we have 3 numbers in our data set x. Their values are 10, 25 & 18. So now we want to use these values to plot a Pie chart. To do that, we just call the plt module’s pie( ) function and pass x to it. It is as simple as that! So when we call the plt module’s show( ) function we get the following Pie Chart:
So as you can see above, we have a basic Pie Chart with 3 sectors – Orange, Green & Blue. But did you notice something here? Their size is not the same, In fact their size corresponds to the value of our x dataset. Right?
So that is what I meant when I said “Each of the sector’s arc size will show the value of that data”. Aha! Now it all makes sense. Right?
But there is something not right in the above Pie chart. Can you see what it is? Yes, the Pie chart is not exactly circular. Right? How do we fix that?
Plotting A Circular Pie Chart Using Python
Well, fixing it is actually quite simple. We just need to define our Plotting area to be a square and then it will correct itself! So if we modify the above code as follows:
import matplotlib.pyplot as plt plt.figure(figsize=(4,4)); x = [10, 25, 18] plt.pie(x) plt.show()
It will get fixed!
Now it looks like a perfect circle. Great!
How Is the Sector Size Of A Pie Chart In Python Calculated
So we can see three sectors in the above Pie Chart. But how is it’s width calculated to fit into the circle? Well it is actually quite simple. The pie( ) function in Matplotlib uses the formula:
to find the sector size. Here x is the data value this sector represents & sum(X) is the sum of all the values in the data set. So in other words, we are calculating the percentage of this value with respect to the whole data set. Hope that made sense to you!
But even though we now have a good circular Pie chart, there are a few things missing. One are the labels. Won’t it be great if we could label each of the sectors in the Pie chart? Second, how about it display the percentage of each sector as well?
That is what we will fix in the next section.
How To Add Labels To Pie Chart In Python
So first, let us see how we can add labels to this Pie chart. Alright? So how do we do that. Well, it so happens that our Matplotlib library has a way to do that too! We just need to use the “labels” parameter when calling the plt.pie( ) function with appropriate labels. So how does the code then look like? Take a look at it yourself:
import matplotlib.pyplot as plt plt.figure(figsize=(4,4)); x = [10, 25, 18] labels = ['Bus', 'Car', 'Train'] plt.pie(x, labels=labels) plt.show()
And the Pie chart created by this code looks like this:
So now we have our labels around the sectors of this Pie Chart. Perfect!
How To Add Percentage Values To Pie Chart In Python
Adding percentage values to this Pie Chart is also quite simple. We just need to call the autopct parameter and it will fill in the values for us! So the code to do that looks like this:
import matplotlib.pyplot as plt plt.figure(figsize=(4,4)); x = [10, 25, 18] labels = ['Bus', 'Car', 'Train'] plt.pie(x, labels=labels, autopct='%1.1f%%') plt.show()
Here the value ‘%1.1f%% is the format specifier that tells Matplotlib to print the percentages in x.x% format!
So here is how the plot with percentage now looks like:
Finally, we will take a look at the Explode feature of Matplotlib.
Explode Feature Of Pie Chart Using Matplotlib
This feature will allow us to split the sector and move it slightly above it’s center. It is difficult to explain what I mean by this using words, so let me show you by directly using it:
import matplotlib.pyplot as plt plt.figure(figsize=(4,4)); x = [10, 25, 18] labels = ['Bus', 'Car', 'Train'] explode = [0.1, 0.3, 0.2] plt.pie(x, labels=labels, explode=explode, autopct='%1.1f%%') plt.show()
Here the “explode” values that we are passing tells the Pie chart to offset the sector by so much fraction from the center of the pie.
So here is how the final Pie Chart looks like:
So there you have it. That is how we can draw a Pie chart in Python using Matplotlib library. I hope I have covered all the things relevant to drawing these Pie charts. But if you have any doubts regarding these, do not hesitate to ask in the comments. I will be more than happy to help!
So until next time, take care! 🙂