1. Concept and Uses of Pie Charts
A pie chart, aka the "pie", is a cool way to visually represent the relative sizes of parts of a whole. If you've ever asked 10 of your friends about their favorite programming language and wanted to show it off as a graph, the pie chart is the way to go! It's perfect for showing the breakdown of things like market shares of companies or how you split your daily time between coding and watching funny cat videos.
Using the pie()
Function to Create a Pie Chart
Matplotlib has this awesome pie()
function that makes creating pie charts as easy as... well, pie!
Here's a basic example:
import matplotlib.pyplot as plt
# Data for the pie chart
labels = ['Python', 'JavaScript', 'C++', 'Java']
sizes = [40, 30, 20, 10] # Portions in percent
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue'] # Segment colors
# Creating the pie chart
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140)
# Making the axes equal for a proper circle
plt.axis('equal')
# Show the chart
plt.show()
In this example, we defined the categories (programming languages), their sizes, and the colors for each. Then, we called the pie()
function to create the chart. The parameter autopct='%1.1f%%'
adds percentage labels to the segments, and startangle=140
rotates the chart to make it look cooler.

2. Customizing Pie Charts
A pie chart is like a New Year's Eve dress—you can dress it up and style it however you want. Let’s check out a few ways you can customize it.
Displaying Percentages with autopct
The autopct
parameter lets you show percentage values inside each sector. The value "%1.1f%%"
specifies the format (one decimal place for percentages).
import matplotlib.pyplot as plt
labels = ["Cats", "Dogs", "Birds", "Fish"]
sizes = [35, 30, 20, 15]
plt.pie(sizes, labels=labels, autopct="%1.1f%%")
plt.title("Popularity of Pets")
plt.show()

Highlighting a Pie Slice with explode
The explode
parameter can move one or more slices away from the center, which is great for emphasizing a specific category.
import matplotlib.pyplot as plt
labels = ["Cats", "Dogs", "Birds", "Fish"]
sizes = [35, 30, 20, 15]
explode = (0.1, 0, 0, 0) # Highlight the first slice (Cats)
plt.pie(sizes, labels=labels, autopct="%1.1f%%", explode=explode)
plt.title("Popularity of Pets")
plt.show()
Here, the "Cats" slice is moved 10% away from the center (value 0.1
), making it stand out.

Changing Slice Colors with colors
You can set a custom color for each slice using the colors
parameter by passing a list of colors.
import matplotlib.pyplot as plt
labels = ["Cats", "Dogs", "Birds", "Fish"]
sizes = [35, 30, 20, 15]
colors = ["#ff9999", "#66b3ff", "#99ff99", "#ffcc99"]
plt.pie(sizes, labels=labels, autopct="%1.1f%%", colors=colors)
plt.title("Popularity of Pets")
plt.show()
In this example, each slice gets its own color to improve visualization.

Changing the Starting Angle with startangle
The startangle
parameter sets the starting angle of the chart. This can help place the main category in a specific position, like at the top.
import matplotlib.pyplot as plt
labels = ["Cats", "Dogs", "Birds", "Fish"]
sizes = [35, 30, 20, 15]
plt.pie(sizes, labels=labels, autopct="%1.1f%%", startangle=90)
plt.title("Popularity of Pets")
plt.show()
Setting startangle=90
rotates the chart so that the first slice starts at a 90-degree angle (pointing straight up).

3. Practical Examples
While pie charts are a classic tool, they aren’t suitable for every situation. If you have tons of tiny slices or data that’s hard to interpret on a pie, you might want to go with another visualization like a bar chart. Always think about readability and what your audience needs.
Example: Market Segment Analysis
Suppose we have data on market share distribution among four companies, and we want to create a pie chart to visualize it.
import matplotlib.pyplot as plt
# Market share data
labels = ["Company A", "Company B", "Company C", "Company D"]
sizes = [40, 25, 20, 15]
colors = ["#ff9999", "#66b3ff", "#99ff99", "#ffcc99"]
explode = (0.1, 0, 0, 0) # Highlighting Company A
plt.pie(sizes, labels=labels, autopct="%1.1f%%", startangle=140, colors=colors, explode=explode)
plt.title("Market Share Distribution")
plt.show()

This chart shows the market shares of the companies, with Company A’s segment highlighted to emphasize its leading position.
Useful Tips for Building Pie Charts
- Limit the number of categories: Pie charts work best with a smaller number of categories. Too many slices make the chart hard to read.
- Add percentages: Showing percentage values on the slices helps people understand the data faster.
- Use explode to highlight: Highlight important categories to draw attention to them.
- Pick clear colors: Use easily distinguishable colors, especially if you have more than four categories.
- Consider alternatives: For many categories, consider using a bar chart or histogram instead of a pie chart.
Use this little adventure into pie charts to level up your Matplotlib skills. It's just one step towards becoming a visualization wizard, able to tell any story through graphs and charts. Good luck, and may your charts always look delicious! 🍕
GO TO FULL VERSION