Pie charts are perfect for showing proportions and percentage distribution of data in a simple and visually appealing way.

In this guide, you’ll learn how to create and customize pie charts using Matplotlib.


πŸ”Ή What is a Pie Chart?

A pie chart is a circular graph divided into slices, where each slice represents a proportion of the whole.

πŸ‘‰ It answers:

  • What percentage does each category contribute?
  • How is data distributed in parts?

πŸ”Ή When to Use Pie Chart?

Use a pie chart when:

βœ” You want to show percentage distribution
βœ” You have limited categories (ideally < 5)
βœ” You want a simple visual representation


πŸ”Ή Basic Example

import matplotlib.pyplot as pltlabels = ['Python', 'Java', 'C++']
sizes = [45, 30, 25]plt.pie(sizes, labels=labels)
plt.title("Pie Chart Example")plt.show()

πŸ”Ή Output Explanation

  • labels β†’ Categories
  • sizes β†’ Values (proportions)
  • plt.pie() β†’ Creates the pie chart

πŸ”Ή Real-Life Use Cases

πŸ“Š Market share analysis
πŸ’» Programming language usage
πŸ’° Budget distribution
πŸ“¦ Product category contribution


πŸ”Ή Adding Percentages

plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.title("Pie Chart with Percentages")plt.show()

πŸ‘‰ Displays percentage values on each slice.


πŸ”Ή Customizing Pie Chart

colors = ['gold', 'lightblue', 'lightgreen']plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%')
plt.title("Customized Pie Chart")plt.show()

πŸ”Ή Customization Options

FeatureExampleDescription
Colorscolors=['red','blue']Set slice colors
autopct'%1.1f%%'Show percentages
startanglestartangle=90Rotate chart
explode[0.1, 0, 0]Highlight a slice

πŸ”Ή Exploding a Slice

explode = [0.1, 0, 0]plt.pie(sizes, labels=labels, explode=explode, autopct='%1.1f%%')
plt.title("Exploded Pie Chart")plt.show()

πŸ‘‰ Highlights important category.


πŸ”Ή Donut Chart (Advanced)

plt.pie(sizes, labels=labels, autopct='%1.1f%%')
centre_circle = plt.Circle((0,0), 0.70, fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)plt.title("Donut Chart")plt.show()

πŸ”Ή Saving the Chart

plt.savefig("pie_chart.png")

πŸ”Ή Best Practices

βœ” Use only a few categories
βœ” Ensure total = 100%
βœ” Avoid too many colors
βœ” Use labels clearly
βœ” Prefer bar chart if data is complex


πŸ”— Useful Resources


πŸ”š Conclusion

Pie charts are simple yet powerful for showing proportions. When used correctly, they make your data easy to understand at a glance.

However, use them wiselyβ€”too many slices can make them hard to read.


πŸ”– Hashtags

#Matplotlib #Python #DataVisualization #PieChart #DataScience #MachineLearning #Coding #Analytics #Programming #AI #BigData

Learn Python