Hello and welcome! In today's lesson, we dive into the world of Pie Charts in Python. Pie charts enable you to depict proportions visually, illustrating populations by continents where each slice represents a part of the total, for instance. Today, you'll learn to create and customize pie charts in Python using Matplotlib
.
Pie charts are a type of graphic where the circle (the "pie") is divided into "slices," each of which represents a different category. Imagine a pizza where each slice is a fruit type - apples, oranges, bananas, and grapes. The size of each slice corresponds to the quantity of each fruit. To create interactive pie charts, we'll be using the well-established Python data visualization library: Matplotlib
.
Let's create a pie chart depicting the favorite fruits of a group of people, which could represent consumer preferences in a market survey. We will use this dataset:
Python1import matplotlib.pyplot as plt 2import pandas as pd 3 4# Dataframe for plotting 5df = pd.DataFrame({'Fruits': ['Apples', 'Bananas', 'Cherries', 'Dates'], 6 'Counts': [15, 30, 45, 10]}) 7labels = df['Fruits'] 8sizes = df['Counts']
Let's start crafting a pie chart for this dataset.
Pie Chart Components:
Crafting a Pie Chart:
With the Matplotlib library, we can create an informative and appealing pie chart. It takes the data array and labels for it. We will also pass the startangle
parameter, which rotates the graph, to make its layout fancier.
Python1plt.pie(sizes, labels=labels, startangle=20) 2plt.title('Fruits Preferences') 3plt.axis('equal') # ensures the pie chart is a perfect circle 4plt.show()
Result:
This chart helps us visualize the division of preferences, much like how market shares are illustrated in business.
To highlight a specific slice in a pie chart, we slightly separate it from the rest of the pie, a process referred to as "exploding" the slice.
Consider an array or a tuple you pass to explode
. Typically, most of the values are zero, except for the slice we want to highlight. For example:
Python1explode = (0.1, 0, 0, 0)
Here, we are exploding the first slice, i.e., moving it out slightly. Let's see what that looks like in a full code bloc:
Python1explode = (0.1, 0, 0, 0) # "explode" the first slice 2 3plt.pie(sizes, labels=labels, explode=explode, startangle=20) 4plt.title('Fruits Preferences') 5plt.axis('equal') # ensures the pie chart is a perfect circle 6plt.show()
Result:
Now our pie chart highlights the apples, or the first slice in our pie.
Our pie charts can become even more visually appealing with custom colors. Matplotlib
allows us to give each slice of our pie a specific color. To achieve this, we provide an array of color names to the colors
parameter in the plt.pie()
function.
Let's look at a full example with the previous explode
feature and the new colors
feature:
Python1explode = (0.1, 0, 0, 0) # "explode" the first slice 2colors = ['lightgreen', 'yellow', 'pink', 'skyblue'] # array of custom colors 3 4plt.pie(sizes, labels=labels, explode=explode, colors=colors, startangle=20) 5plt.axis('equal') # Ensures the pie chart is a perfect circle 6plt.show()
Result:
On top of highlighting the apple slice, we have now added distinct colors to each slice of our pie chart!
The autopct
parameter allows us to promptly add the percentage value to each slice of a pie chart. This can be beneficial when you want to visually convey the proportions at a glance.
To use it, we add the autopct
parameter to the plt.pie()
function. We typically represent the number as a floating-point percentage.
Now, let's put it all together:
Python1explode = (0.1, 0, 0, 0) # "explode" the first slice 2colors = ['lightgreen', 'yellow', 'pink', 'skyblue'] # array of custom colors 3 4plt.pie(sizes, labels=labels, explode=explode, colors=colors, autopct='%1.1f%%', startangle=20) 5plt.axis('equal') # Ensures the pie chart is a perfect circle 6plt.show()
The format string '%1.1f%%'
means the number will be displayed as a floating point with at least one digit before and exactly one digit after the decimal point. The two percentage signs at the end of the format string are needed because the first acts as an escape character, making the second display a literal character on the pie slices.
Result:
With this full code, we have a pie chart that highlights apples, uses custom colors for each slice, and shows the percentage of each slice. Pure magic!
Bravo! You've mastered the basics of pie charts, built one using Python's Matplotlib
library, and personalized it to achieve better visualization. Knowledge enhancement occurs through practice. Our next activity will involve practicing the creation and customization of pie charts. This will reinforce your understanding and improve your Python data visualization skills. Let's keep going!