If you want to make something visual and colorful as project in Python, then making a pie chart in Python using Matplotlb library is a really great first step. It doesn’t need many lines of code, and it helps you understand how Python libraries work.
In this blog, I will show you how I made a pie chart in Python using Matplotlib library and explain everything in very simple words so even a complete beginner can follow.
What is a Pie Chart, and what does it represent?
A pie chart is a round chart that shows data in the form of slices.
Imagine a pizza that is cut into different sizes of slices based on how much each person likes a topping. Each slice shows a portion of the total.
In our case, we will be showing different programming languages, just as an example.
Step by Step Explained: Pie Chart in Python
1) Import the Matplotlib library
To make a pie chart in Python, we use a library called matplotlib
.
What is matplotlib? Matplotlib
is one of the most popular libraries when it comes to drawing graphs and charts. To use it, we must first import it in our Python file.
We import it and gave a short name let’s say ‘pyplot’
<strong>import matplotlib.pyplot as pyplot</strong>
2) Define labels and values
For the pie chart, we need two things: labels and values.
Labels are the names of each part, like Python, Java, etc. and Values are the sizes, like 45, 20, 15, 20 — showing how big each part is.
It’s just example data, like how much people like each language.
labels = ('Python', 'Java', 'Scala', 'C#')
sizes = [45, 20, 15, 20]
3) Create pie() function to create Pie Chart
We create the pie chart using the pyplot.pie()
function. This function takes several inputs.
The first input is sizes
, which tells it how big each slice should be. Then we give labels=labels
to say which name goes with each slice. Then we use autopct='%1.1f%%'
which tells Python to show percentages on the chart, like 45.0%, 20.0%, and so on.
That’s helpful to see the actual number. Then there’s counterclock=False
, which means the chart will draw slices in a clockwise direction.
Finally, startangle=105
just tells the chart where to start drawing the first slice. You can try changing this number to see how the chart rotates.
4) Show the pie chart on Screen
The last step is to show the chart. We use pyplot.show()
for that. This line opens a window with the pie chart we made. It looks clean, colorful, and easy to understand.
Source Code: Pie Chart in Python Using Matplotlib
import matplotlib.pyplot as pyplot labels = ('Python', 'Java', 'Scala', 'C#') sizes = [45, 20, 15, 20] pyplot.pie( sizes, labels=labels, autopct='%1.1f%%', counterclock=False, startangle=105 ) pyplot.show()
Once you run this code, you will see a circle divided into slices, each one showing a language and its percentage.
Making this chart helps you to learn how functions in Python work, and also how to use libraries. It gives you more confidence in writing code.
Another good thing about this chart is that it works for many kinds of data. You can make a pie chart to show how much time you spend on each subject in school or college, how much budget you spend on different things, etc.
Final Conclusion
This was the entire explanation about making a simple pie chart using Python and matplotlib. If you are just starting, I highly recommend trying this. You’ll learn how to import a library, how to use its functions, and how to show data visually. It’s a small project, but it teaches a lot.
Next you should learn about Pencil Sketch in Python usng OpenCV or Weather APP etc.