In This Article, You Will Learn About Python Matplotlib Pie Charts.
Python Matplotlib Pie Charts – Before moving ahead, let’s take a look at Python Matplotlib Histogram
Table of Contents
Creating Pie Charts
To make a Pie Chart using Matplotlib, we can use the pie() function.
The pie() function uses an array of numbers and creates Pie Chart. Later, this array will be passed to the Pyplot function as an input.
Example: Creating a simple Pie Chart.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([15, 20, 25, 50])
plt.pie(x)
plt.show()
As shown above, it returned a Pie Chart from given data.
You can see, the pie chart is drawn as one piece (called wedge) for each value of the range in this example is [10, 40, 50].
The graphing of the initial wedge begins at the x-axis and then moves clockwise:
NOTE: The size of each wedge is determined by comparing its value against all the other wedges, applying this formula:
The value divided by the sum of all values: x/sum(x)
Labels
To add a label for each wedge, we can use the label parameter.
The label parameter must be an array carrying separate names for each wedge.
Later, this array will be passed to the Pyplot function as an argument.
Example: Creating a Pie Chart with Label names.
import matplotlib.pyplot as plt
import numpy as np
data = np.array([15, 20, 25, 50])
label_names = ["A", "B", "C", "D"]
plt.pie(data, labels = label_names)
plt.show()
As a result, it returned a Pie Chart with its wedges name.
Start Angle
By default, Pie Charts starts with the x-axis, i.e., from angle 0, but we can change the start angle using the startangle parameter.
The default start angle is 0, defined in terms of degrees.
The startangle parameter will be passed to the Pyplot function as an argument.
Example: Creating a Pie Chart starting from 120 degrees.
import matplotlib.pyplot as plt
import numpy as np
data = np.array([15, 20, 25, 50])
label_names = ["A", "B", "C", "D"]
plt.pie(data, labels = label_names, startangle = 120)
plt.show()
As shown clearly, it returned a Pie Chart whose starting angle is 120 degrees.
Explode
What if you want one of the wedges pulled out?
To pull out any wedge in Pie Chart, we can use explode parameter.
The explode parameter takes an array having a different value for each wedge.
If explode parameter is not defined, it should be None.
The explode parameter will be passed to the Pyplot function as an argument.
Example: Pull out the second wedge by 0.3.
import matplotlib.pyplot as plt
import numpy as np
data = np.array([15, 20, 25, 50])
label_names = ["A", "B", "C", "D"]
explode_number = [0, 0.3, 0, 0]
plt.pie(data, labels = label_names, explode = explode_number)
plt.show()
As it has been noticed, it returned a Pie Chart with pulling its one of wedges out by 0.3.
Shadow
To show a shadow in Pie Chart, we can use the shadow parameter as setting it to True.
The shadow parameter will be passed to the Pyplot function as an argument.
Example: Add shadow to Pie Chart.
import matplotlib.pyplot as plt
import numpy as np
data = np.array([15, 20, 25, 50])
label_names = ["A", "B", "C", "D"]
explode_number = [0, 0.3, 0, 0]
plt.pie(data, labels = label_names, explode = explode_number, shadow = True)
plt.show()
As a result, it returned a Pie Chart showing shadow.
Colors
To add colors for each wedge, we can use colors parameter.
The colors parameter takes an array as a separate color name for each wedge.
The colors parameter will be passed to the Pyplot function as an argument.
Example: Setting color name separately for each wedge.
import matplotlib.pyplot as plt
import numpy as np
data = np.array([15, 20, 25, 50])
label_names = ["A", "B", "C", "D"]
color_names = ["green", "red", "blue", "orange"]
explode_number = [0, 0.3, 0, 0]
plt.pie(data, labels = label_names, explode = explode_number, colors = color_names, shadow = True)
plt.show()
As a result, it returned a Pie Chart with customized colors.
You can make use of 140 color names supported by the HTML or any of the following shortcuts:
‘r’ – Red
‘g’ – Green
‘b’ – Blue
‘c’ – Cyan
‘m’ – Magenta
‘y’ – Yellow
‘k’ – Black
‘w’ – White
Click to Download File for 140 color names.
Example: Setting color names separately for each wedge using shortcut color names.
import matplotlib.pyplot as plt
import numpy as np
data = np.array([15, 20, 25, 50])
label_names = ["A", "B", "C", "D"]
color_names = ["g", "r", "b", "y"]
plt.pie(data, labels = label_names, colors = color_names, shadow = True)
plt.show()
As a result, it returned a Pie Chart with customized colors.
Legend
To include a list of the reasons for each wedge, utilize the legend() function.
Example: Adding legend for each wedge.
import matplotlib.pyplot as plt
import numpy as np
data = np.array([15, 20, 25, 50])
label_names = ["A", "B", "C", "D"]
color_names = ["g", "r", "b", "y"]
plt.pie(data, labels = label_names, colors = color_names, shadow = True)
plt.legend()
plt.show()
As shown above, it returned a Pie Chart with the label names of its wedges.
Legend with Header
For adding a heading to the legend, simply add a title parameter into the legend() function.
Example: Adding a title for legend.
import matplotlib.pyplot as plt
import numpy as np
data = np.array([15, 20, 25, 50])
label_names = ["A", "B", "C", "D"]
color_names = ["g", "r", "b", "y"]
plt.pie(data, labels = label_names, colors = color_names, shadow = True)
plt.legend(title = "Student Names")
plt.show()