DIY Pie Chart Maker (Python and Matplotlib)

Pie Charts in Python are a quick yet interesting way to show data in the form of a graphical circle with divisions, each division showing one subdivision of the topic of the chart and of angle depending on the number of units under that subdivision. It is very useful in explaining data in an understandable way without confusing people with too many numbers. Drawing pie charts manually may be a bit of a headache, but the module, matplotlib has an inbuilt function that allows you to DIY Pie Charts 

Since we are talking about pies, let us assume that there are a group of 50 people who like different varieties of pies. Let us assume that 20 people like Apple Pie, 5 people like Pumpkin Pie, 15 people like Strawberry Pie and the remaining 10 people like Blueberry Pie. 

First things first, let us import pyplot from matplotlib with the statement: from matplotlib import pyplot.

Now, we can create a list called say numbers with all the values. Once we do that, let's use pyplot.pie(numbers) to create the pie chart and pyplot.show() to show us the pie chart we created now. It would look something like this.

diy pie chart maker with matplotlib and python



Looks good, right ? It would have been good if we could tell people what division stands for what group of people. Here is where labels come into play. All we need to do is create another list with all the division names corresponding to the number in the first list. Now, we need to edit the pyplot.pie() function by adding another attribute called label under the function. The final code snippet would look something like this: 

from matplotlib import pyplot

numbers = [20,5,15,10]
names = ['Apple','Pumpkin','Strawberry','Blueberry']

pyplot.pie(numbers, labels = names)

pyplot.show()

This would give us an output that looks something like this:


plotting pie charts in python


Exactly what we were looking for ! 

Is that the only other attribute under the pie function ? Not at all. Why don't we make the chart look a little better with a shadow. Just add shadow = True to the function to get a pie chart that looks a lot more stylish, like this...

3d pie charts in Python with Matplotlib

What if you want to tell people to look at one division specifically ? The pie function has a solution for that too. An attribute called explode splits divisions from the center by a user-specified amount. Let's split the Apple division and the Strawberry division by values of 0.2 and 0.1 respectively. The code for that would look like this: 

from matplotlib import pyplot

numbers = [20,5,15,10]
names = ['Apple','Pumpkin','Strawberry','Blueberry']
split_amount = [0.2,0,0.1,0]

pyplot.pie(numbers, labels = names, shadow = True, explode = split_amount)

pyplot.show()

and the final pie-chart would look like this:

How to make Pie-Charts in Python


We can clearly see that the Apple bit and the Strawberry bit are separated from the centre by different amounts, according to our entry in the split_amount list.

So, here you are. These are some of the most commonly used attributes under the pie function in matplotlib's pyplot. Do use the comment section to tell me your experiences with the pie function. 

If you have any comments or queries, please feel free to use the comment section to let me know and I will try to get back with the answer as soon as I can.

Do follow ThePygrammer if you like the posts and found them useful. Just click the blue button in the menu to follow. 

If you want to check out how to plot graphs with matplotlib, check this out: Drawing Graphs in Python with Matplotlib (thepygrammer.blogspot.com) or if you want to check out some other module, have a look at this: Meet the Math Module (thepygrammer.blogspot.com)

QUICK POLL: 


Comments