Topic Overview and Actualization

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.

Introduction to Pie Charts

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.

Your First Pie Chart: Dataset

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:

Your First Pie Chart: Sharing the Pie

Let's start crafting a pie chart for this dataset.

Pie Chart Components:

  • Slices: Portions of each fruit preference.
  • Labels: Names of the fruits.

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.

Result:

This chart helps us visualize the division of preferences, much like how market shares are illustrated in business.

Using 'explode' to Highlight a Slice

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:

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:

Result:

Now our pie chart highlights the apples, or the first slice in our pie.

Assigning Custom Colors

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:

Result:

On top of highlighting the apple slice, we have now added distinct colors to each slice of our pie chart!

Displaying Percentages with autopct

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:

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!

Lesson Summary and Practice

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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal