Pie Charts – AICorr.com



Pie Charts

This page covers a matplotlib pie charts tutorial.

A pie chart (also pie graph or circle chart) is a circular statistical graphic that is divided into slices to illustrate numerical proportion. In a pie chart, the arc length of each slice (and consequently its central angle and area), is proportional to the quantity it represents. Pie charts commonly represent percentages or proportions. This makes it easy to visualise the relative sizes of different categories within a dataset. They’re particularly useful for displaying data that can be categorised into a few distinct groups. These groups can be market shares, budget allocations, demographic distributions, and so on.

Why use pie charts?

There are many benefits of using pie charts. For instance, pie charts provide a clear visual representation of proportions or percentages within a dataset. This makes it easy to understand the relative size of each category. They also allow for a quick comparison between different categories. The size of each slice relative to the whole pie gives an immediate sense of the distribution of data. Furthermore, pie charts are easy to understand and interpret, even for people who are not familiar with data analysis or statistics. They present information in a straightforward and intuitive manner.

In addition, if one or a few categories dominate the dataset, a pie chart can effectively highlight this. As a result, making the corresponding slices larger, drawing attention to the most significant elements. Furthermore, circle charts are useful for illustrating the composition of a whole. For example, they can show how a budget is allocated across different spending categories or how a market is divided among competing companies. And finally, pie graphs can be effective tools for storytelling. As such, it allows presenters to convey key messages or trends visually and engagingly.

Implementation

Let’s look at the how to create a pie chart with Matplotlib. The library uses the method pie().

Now, we create a sample pie graph.

import matplotlib.pyplot as plt

# Sample data
labels = ['Apples', 'Bananas', 'Oranges', 'Grapes']
sizes = [12, 28, 25, 20]

# Pie chart
plt.pie(sizes, labels=labels)

# Title
plt.title('Fruit Distribution')

# Display chart
plt.show()
matplotlib-pie-chart

Note that, by default, a pie chart has the starting point on the right hand side. The starting angle is the straight line on the right hand side, between apples (blue) section and grapes (red) section. This is the default value, which equals to an angle of 0. We can change this with the startangle method. The circulation flows anti-clockwise.

The Below is an illustration of the angle degrees.

matplotlib-pie-chart-angle-degrees
import matplotlib.pyplot as plt

# Sample data
labels = ['Apples', 'Bananas', 'Oranges', 'Grapes']
sizes = [12, 28, 25, 20]

# Pie chart
plt.pie(sizes, labels=labels, startangle=90)

# Title
plt.title('Fruit Distribution')

# Display chart
plt.show()
matplotlib-pie-chart-angle-degrees-90

Similarly to other graphs, we can customise a pie chart. For example, below we change the colour of each slice (part) as well as the explosion of each wedge. We do so through the methods colors and explode, respectively. For both techniques, we create separate values for each wedge (section) and store them into variables.

import matplotlib.pyplot as plt

# Sample data
labels = ['Apples', 'Bananas', 'Oranges', 'Grapes']
sizes = [12, 28, 25, 20]
colours = ["#008000", "purple", "pink", "b"]
explosions = [0.0, 0.0, 0.15, 0.0]


# Pie chart
plt.pie(sizes, labels=labels, startangle=90, colors=colours, explode=explosions)

# Title
plt.title('Fruit Distribution')

# Display chart
plt.show()
matplotlib-pie-chart-customise-colours-and-explode

This is an original matplotlib pie charts tutorial. Created by aicorr.com.

Next: Box Plots

We will be happy to hear your thoughts

Leave a reply

0
Your Cart is empty!

It looks like you haven't added any items to your cart yet.

Browse Products
Powered by Caddy
Shopping cart