Matplotlib Pie Chart
In this post, we will discuss the Matplotlib pie chart using the Python matplotlib.
The pie chart is a circular statistical graph that represents the values in slices. Pie charts are divided into segments to represent values of different sizes. Each sector is a proportion of the whole. It is generally used to show percentage values. It is best to summarise large datasets in visual form when we are trying to compare parts of a whole.
Matplotlib provides the pie() function to plot pie charts using the pyplot module. It has the following syntax-
matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, shadow=False)
x- The wedge sizes,
explode (optional)- Default None, If not None, is a len(x) array which specifies the fraction of the radius with which to offset each wedge,
labels (optional)- Default None, A sequence of strings providing the labels for each wedge,
colors (optional)- Default None, A sequence of matplotlib color args through which the pie chart will cycle,
shadow ( bool, optional)- Draw a shadow beneath the pie.
Matplotlib Basic Pie Chart
Here is a very simple example of Matplotlib pie chart. First, we import plt from the matplotlib module. Then, we define the data to plot. We define a labels array that contains food items and order quantity in orders variable. Then we can use the method plt.pie() to create a plot.
import matplotlib.pyplot as plt
labels = 'Sandwich','Pizza','Donuts','Noodles','Burger'
orders = [24, 32, 34, 30, 40]
fig1, ax = plt.subplots()
ax.pie(orders, labels=labels, shadow=True)
ax.axis('equal')
plt.show()
The output of the above code-
Matplotlib Pie Chart with Explode
The above example shows only a few optional features. In the given example, we have added a few more optional features that are auto-labeling the percentage, offsetting a slice with "explode" and "shadow".
import matplotlib.pyplot as plt
labels = 'Sandwich','Pizza','Donuts','Noodles','Burger'
sizes = [24, 32, 34, 30, 40]
fig1, ax = plt.subplots()
ax.pie(sizes, labels=labels, shadow=True,
autopct='%.0f%%',explode=(0, 0.12, 0, 0, 0))
ax.axis('equal')
plt.show()
The output of the above code-
Matplotlib Donut Pie Chart
Donut Chart is a variation of a Pie chart except it has a round hole in the middle, which makes it look like a donut. This hole can also be utilized to display additional data.
import matplotlib.pyplot as plt
# Data to plot
labels = ['Delhi', 'MP', 'UP', 'Tamil', 'Assam']
medals = [32, 15, 12, 29, 25]
colors = ['#f70d1a', '#7d0552', '#461b7e', '#e8a317', '#4cc552']
# Donut Pie Chart
plt.pie(medals, labels=labels, colors=colors, startangle=90,frame=True)
centre_circle = plt.Circle((0,0),0.5,color='black', fc='white',linewidth=0)
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
plt.axis('equal')
plt.tight_layout()
plt.show()
The output of the above code-
Matplotlib Nested Pie Chart
A nested pie chart or multi-level pie chart allows us to incorporate multiple levels or layers into the pie. The chart is visualised as a series of concentric circles arranged like a pie. The circles are divided into segments that represent each of the data values. The ratio of each segment is determined by the corresponding data value.
import matplotlib.pyplot as plt
# Data to plot
labels = ['Delhi', 'MP', 'UP', 'Tamil', 'Assam']
medals = [32, 15, 12, 29, 25]
genders = ['Man','Woman']
sizes = [15,17,10,5,5,7,17,12,12,13]
colors = ['#f70d1a', '#7d0552', '#461b7e', '#e8a317', '#4cc552']
colors_gender = ['#3BB9FF','#FF7F50']
# Plot Nested Pie
plt.pie(medals, labels=labels, colors=colors, startangle=90,frame=True)
plt.pie(sizes,colors=colors_gender,radius=0.75,startangle=90)
centre_circle = plt.Circle((0,0),0.5,color='black', fc='white',linewidth=0)
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
plt.axis('equal')
plt.tight_layout()
plt.show()
The output of the above code-
Matplotlib add shadow to Pie Chart
In the given Python program, we add a shadow to the pie chart by setting the shadows parameter to 'True'.
import matplotlib.pyplot as plt
mylabels = 'Apple','Orange','Banana','Coconut','Guava'
sizes = [34, 30, 24, 30, 42]
fig1, ax = plt.subplots()
ax.pie(sizes, labels=mylabels,
autopct='%.0f%%',explode=(0, 0.12, 0, 0, 0), shadow=True)
ax.axis('equal')
plt.show()
The output of the above code-
Matplotlib pie chart label color
In matplotlib pie chart, we can set the color of each wedge with the colors parameter. It must be an array with one value for each wedge.
import matplotlib.pyplot as plt
mylabels = 'Apple','Orange','Banana','Coconut','Guava'
sizes = [34, 30, 24, 30, 42]
mycolors = ["#FEA9B8","#BEFD85","#30F1D1","#D296FC","#FDFD85"]
fig1, ax = plt.subplots()
ax.pie(sizes, labels=mylabels, colors = mycolors)
plt.show()
The output of the above code-
Matplotlib pie chart legend
A legend is an area describing the elements of the graph. We can use the legend() function to add a list of explanation for each wedge.
import matplotlib.pyplot as plt
mylabels = 'Apple','Orange','Banana','Coconut','Guava'
sizes = [34, 30, 24, 30, 42]
mycolors = ["#FEA9B8","#BEFD85","#30F1D1","#D296FC","#FDFD85"]
fig1, ax = plt.subplots()
ax.pie(sizes, labels=mylabels, colors = mycolors)
plt.legend()
plt.show()
Output of the above code:
Related Articles
Simple Matplotlib Animation ExampleMatplotlib Pie Chart
Python Line Plot Using Matplotlib
Python Matplotlib 3D Plot
How to save figure in matplotlib pyplot
Python Matplotlib Scatter Plot
Python Pandas Plotting
How to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Human Body Detection Program In Python OpenCV
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples