Saturday, October 12, 2024

How to create pie chart?

As you probably know the pie charts are type of circular chart used in data visualization to show the relative proportions or percentages of different categories with a whole. The pie chart consist of slices and each slice of the pie represents the category, and the size of each slice is proportional to the quantity/percentage that category contributes to the total. The key characteristics of pie charts are:
  • Circular shape - the entire plot is circular shaped where each slice corresponds to a portion of the data. The circle represents 100\% of data, where slices sum up to 100%.
  • Slices - each slice of the pie represents a category or part of the data. The angle of each slice is proportional to the represented quantity. Larger slices represent larger portions of the data, while smaller slices represents smaller portions.
  • Use of percentages - the pie charts are used to represent data in percentages. Labels often show category and the percentage that slice represents.
  • single data series - The pie charts are usually used to represent a single series of the data. If you need to compare multiple data series better avoid the pie charts since they are less effective.
  • limited categories - if data has 6 to 7 categories (small number) the pie charts are most effective. Anything beyond the 7 categories will make pie chart cluttered.
  • focus on proportion - The pie charts main strength is the ability to show the proportion of parts to the whole.
To create a pie chart in matplotlib you have to use matplotlib.pyplot.pie() function and if you have imported the matplotlib.pyplot module in Python script as the plt then the function can be written as plt.pie(). The full form of the pie function can be defined as:
matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=0, radius=1, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, normalize=True, data=None)
The x represents the data for which you want to create a pie chart and is required in the pie function while other parameters are optional which means that if they are not defined the default values will be used. The parameters of the pie function are:
  • x (required) - is an array like value. The data values that determine the size of each pie slice. This array like have to be numerical values and the sum does not need to be equal to 1 since Matplotlib normalizes them.
  • explode (optional) - determines the fraction by which each slice is offset from the center. This is useful for highlighting specific slices. It is an array-like parameter type and the default value is None which means that none of the pie fractions will be exploded. For example if the explod = [0,0.1,0,0] the pie will explode the second slice.
  • labels (optional) - a sequence of strings providing the labels for each slice. The labels is an array-like parameter and the default value is None.
  • colors (optional) - this optional array like parameter is very useful since defining it create a sequence of colors for the slices. If the sequence is not defined the default value is None and this means that the Matplotlib will use the default color cycle.
  • autopct (optional) - is usedto label each slice whit its value or percentage. For example if autopct = '\%1.1f\%\%' labels each slice with its percentage to one decimal place.
  • pctdistance (optional) - represents the radial distance at which the 'autopct' labes are drawn. It is expressed as the fraction of the radius. The default value is 0.6 - the distance will be 0.6*radius of the pie chart.
  • shadow (optinal) - it is a boolean variable type and default value is False. If the value is True a shadow will be created beneath the pie chart to give the 3D effect.
  • labeldistance (optional) - represents the radial distance at which the labels are drawn, expressed as the fraction of the radius. The default value is 1.1.
  • startangle (optional) - represents the angle at which the pie chart begins. This option is very useful to rotate the chart for better readability or emphasis.
  • radius (optional) - represents the radius of the pie chart, which affects the size of the chart. The default value is set to 1.
  • counterclock (optional) - it is a boolean parameter type and the default value is True. In case this parameter is True the slices will be drawn counterclockwise and if set to False the slices are drawn clockwise.
  • wedgeprops (optional) - is a dictionary of properties to customize the appearance of slices such as edgecolor or linewidth. The default value is None
  • textprops (optional) - is a parameter (dictionary-type) of properties to customize the appearance of the text labels. The default value is None.
  • center (optional) - The center of the pie chart. It is useful to adjust if you want to create multiple pie charts in one figure. This parameter is a tuple of floats and default value is (0,0).
  • frame (optional) - is a boolean parameter type and default value is False. If set to True a rectangular frame will be created around the chart.
  • rotatelabels (optional) - also boolen type parameter and default is False. If set to True it will rotate labels to the angle of the corresponding slice.
  • normalize (optional) - another boolean type parameter and default value is True. If this value is True the pie chart will normalize the data to sum to 1. In case it is false it will use raw data values.
  • data (optional) - this parameter will allow you to pass a dataset as dictionary to use with x and labels parameter. This parameter is dict type and default value is None.

Example 1 - Basic pie charts

This example demonstrates how to create a basic pie chart using Matplotlib in python. A pie chart is a circular graph that displays the relative proportions of different categories in a dataset. Each slice of the pie represents a category, and the size of each slice corresponds to the proportion of that category relative to the total. The example consist of the following steps:
  • Importing Matplotlib's Python Module
  • Defining the Sizes and Labels
  • creating and Figure and Adjusting the Size
  • Creating the Pie Chart
  • Adding a Title
  • Displaying the Pie Chart

Importing Matplotlib's Pyplot Module

To create a basic pie chart we will need only the matplotlib.pyplot module to be imported. The datast will be defined using lists.
import matplotlib.pyplot as plt 
Matplotlib's pyplot (plt) is imported, which provides functions to create plots and charts, including the pie chart.

Defining the sizes and labels

After the required libraries are imported the first step is to define the datasets. In case of pie chart we have to define the sizes and labels. The sizes contain the slices of the pie chart and labels contain the label of each slice.
size = [15,30,45,10]
labels = ['A', 'B','C', 'D']
sizes is a list that defines the proportions of each category. These numbers represent the relative sizes of the slices in the pie chart. In this case, the four categories will have slices corresponding to 15\%, 30\%, 45\%, and 10\% of the pie. labels is a list of strings representing the names or categories associated with each slice. In this example, the categories are labeled as 'A', 'B', 'C', and 'D'.

Creating a Figure and Adjusting the Size

To create the figure we will use the plt.figure() function and the to set the size we will use the figsize parameter.
plt.figure(figsize=(12,8))
plt.figure(figsize=(12,8)) creates a new figure (the canvas for the plot) with a custom size. The figure will be 12 units wide and 8 units tall. This helps to make the pie chart larger and more readable.

Creating the Pie Chart

To create the pie chart we will use the plt.pie() function. The plt.pie() function reuires values and categories. The values of slices are defined in the sizes variable while the labels are defined in the labels variable.
plt.pie(sizes, labels=labels)
The plt.pie(sizes, labels=labels) creates the pie chart: The sizes argument determines the size of each slice of the pie, with each value in the sizes list representing the percentage of the pie chart for that category. The labels argument assigns the corresponding labels ('A', 'B', 'C', 'D') to the slices in the pie chart, making it easier to identify what each slice represents.

Adding a Title

The title that will be added is "Basic Pie Chart Example" using the plt.title() function
plt.title("Basic Pie Chart Example")
The previous code block will add a title "Basic Pie Chart Example", to the chart.

Displaying the Pie Chart

The plot will be shown using the plt.show() function.
plt.show()
This command renders and displays the pie chart in the output window.The entire code is shown below.
	import matplotlib.pyplot as plt 
	sizes = [15, 30, 45, 10]
	labels = ['A', 'B', 'C', 'D']
	
	plt.figure(figsize=(12,8))
	plt.pie(sizes, labels=labels)
	plt.title("Basic Pie Chart Example")
	plt.show()
The pie chart generated with code in the previous block is shown in Figure.
2024-10-12T23:14:29.761575 image/svg+xml Matplotlib v3.8.0, https://matplotlib.org/
Figure 1 - Pie Chart Example

Exploding slices and customizing labels

Now that we know how to create the pie chart we can create an exploded pie chart, and customize its labels.

Exploding slices

For this example we will import the same library i.e. matplotlib.pyplot, create variables "sizes" and "labels" that are the same as in previous example.
import matplotlib.pyplot as plt 
sizes = [15, 30, 45, 10]
labels = ['A', 'B', 'C', 'D']
As stated previously the idea is to create the exploded pie chart. This chart will have 2nd slice slightly moved out from the pie chart. To do that we will created a tuple with zeros however he second value of the tuple will be 0.1.
explode = (0, 0.1, 0, 0)
The next step is to create the empty figure and set the size of 12 by 8 inches.
plt.figure(figsize=(12,8))
To create the pie chart we have to use the plt.pie() function. Since the idea is to create the explode version of the original pie chart we have to define the explode parameter which in this case will be equal to explode variable. We will aslo provide labels that were defined with the lables variable. The colors that will be provieded are stored in the colors variable.
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
Besides the customized colors we need to define the string format for label slices. This will be defined using the autopct format = '%1.1f%%'. So the plt.pie() function and thier parameters are defined as follows.
plt.pie(sizes, labels=labels, explode=explode, autopct='%1.1f%%')
Finally we will define the title using the plt.title() function and the title is "Exploded Pie Chart with Percentages". The exploded pie chart will be shown with plt.show() function.
plt.title("Exploded Pie Chart with Percentages")
plt.show()
The entire code created in this example is shown below.
import matplotlib.pyplot as plt
sizes = [15, 30, 45, 10]
labels = ['A', 'B', 'C', 'D']
explode = (0, 0.1, 0, 0) # Explode the 2nd slice
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
plt.figure(figsize=(12,8))
plt.pie(sizes, labels=labels, explode=explode, autopct='%1.1f%%')
plt.title("Customized Pie Chart")
plt.show()
After the code is executed the following figure is obtained.
2024-10-12T23:42:40.321126 image/svg+xml Matplotlib v3.8.0, https://matplotlib.org/
Figure 2 - Example of exploded bar plot
From Figure 2 it can be noticed that the second category or category "B" is separted (Exploaded) from the pie chart.

No comments:

Post a Comment