Python Matplotlib Tutorial

Matplotlib Pie Chart – plt.pie() | Python Matplotlib Tutorial

Matplotlib Pie Chart

In this blog, we will work on how to draw a matplotlib pie chart? To draw pie char use plt.pie() function. The matplotkib plt.pie() function help to plot pie chart of given numeric data with labels. It also support different parameters which help to show better.

Importing Library

import matplotlib.pyplot as plt

Plotting Matplotlib Pie Chart

plt.pie([1]) # Plot pie chart of value [1]
plt.show() # To show Pie chart

Output >>>

Matplotlib Simple pie Chart
Fig 1.1 – Matplotlib Simple pie Chart

Importing Dataset

classes = ["Python", 'R', 'Machine Learning', 'Artificial Intelligence', 
           'Data Sciece']
class1_students = [45, 15, 35, 25, 30]

Plotting pie chart using real dataset.

plt.pie(class1_students, labels = classes)
plt.show()

Output >>>

Matplotlib pie chart using dataset
Fig 1.2 – Matplotlib pie chart using dataset

Below are the different parameters of plt.pie() functions.

Syntax: plt.pie(
[‘x’,
‘explode=None’,
‘labels=None’,
‘colors=None’,
‘autopct=None’,
‘pctdistance=0.6’,
‘shadow=False’,
‘labeldistance=1.1’,
‘startangle=None’,
‘radius=None’,
‘counterclock=True’,
‘wedgeprops=None’,
‘textprops=None’,
‘center=(0, 0)’,
‘frame=False’,
‘rotatelabels=False’,
‘*’,
‘data=None’],

)

x : array-like
explode : array-like, optional, default: None
labels : list, optional, default: None
colors : array-like, optional, default: None
autopct : None (default), string, or function, optional
pctdistance : float, optional, default: 0.6
shadow : bool, optional, default: False
labeldistance : float, optional, default: 1.1
startangle : float, optional, default: None
radius : float, optional, default: None
counterclock : bool, optional, default: True
wedgeprops : dict, optional, default: None
—- example, you can pass in wedgeprops = {‘linewidth’: 3}
textprops : dict, optional, default: None
center : list of float, optional, default: (0, 0)
frame : bool, optional, default: False
rotatelabels : bool, optional, default: False

Plotting pie chart using different parameters.

explode = [0.03,0,0.1,0,0] # To slice the perticuler section
colors = ["c", 'b','r','y','g'] # Color of each section
textprops = {"fontsize":15} # Font size of text in pie chart

plt.pie(class1_students, # Values
        labels = classes, # Labels for each sections
        explode = explode, # To slice the perticuler section
        colors =colors, # Color of each section
        autopct = "%0.2f%%", # Show data in persentage for with 2 decimal point
        shadow = True, # Showing shadow of pie chart
        radius = 1.4, # Radius to increase or decrease the size of pie chart 
       startangle = 270, # Start angle of first section
        textprops =textprops) 

plt.show() # To show pie chart only

Output >>>

Matplotlib pie chart using parameters
Fig 1.3 – Matplotlib pie chart using parameters

Plotting pie chart using a legend – plt.legend()

explode = [0.03,0,0.1,0,0] # To slice the perticuler section
colors = ["c", 'b','r','y','g'] # Color of each section
textprops = {"fontsize":15} # Font size of text in pie chart

plt.pie(class1_students, # Values
        labels = classes, # Labels for each sections
        explode = explode, # To slice the perticuler section
        colors =colors, # Color of each section
        autopct = "%0.2f%%", # Show data in persentage for with 2 decimal point
        shadow = True, # Showing shadow of pie chart
        radius = 1.4, # Radius to increase or decrease the size of pie chart 
       startangle = 270, # Start angle of first section
        textprops =textprops) 
plt.legend() # To show legend
plt.show() # To show pie chart only

Output >>>

Matplotlib pie chart using legend
Fig 1.4 -Matplotlib pie chart using legend

Plotting pie chart using more parameters than above and width = 1.

plt.figure(figsize = (3,2))
wedgeprops = {"linewidth": 4, 'width':1, "edgecolor":"k"} # Width = 1
plt.pie(
        class1_students, 
        labels = classes, 
        explode = explode, 
        colors = colors, 
        autopct = "%0.2f%%", 
        pctdistance = 0.6, 
        shadow =True, 
        labeldistance = 1.6, 
        startangle = 270,
        radius = 1, 
        counterclock = True, 
        wedgeprops = wedgeprops,
        textprops = textprops,
        center=(2, 3),
        frame=True,
        rotatelabels=True
        ) 
plt.show()

Output >>>

Matplotlib pie chart using more parameters
Fig 1.5 – Matplotlib pie chart using more parameters

Change the Value of width by 2,3,4 then you will gote below the pie charts.

Fig 1.6 – Matplotlib pie chart with a width value 2
Fig 1.7 – Matplotlib pie chart with width value 3

Plotting another pie charts for fun.

import numpy as np
plt.figure(figsize=(7,4))
#plt.figure(figsize=(16,9)

colors = ['r','w','r','w','r','w','r','w','r','w','r','w','r','w','r','w','r','w','r','w']
labels = np.ones(20)
#labels = [1.0,1.0,1.0,1.0,1.0,.........,1.0]

plt.pie([1], colors="k", radius = 2.05)
plt.pie(labels, colors=colors, radius = 2.0)

plt.pie([1], colors="g", radius = 1.8)
plt.pie([1], colors="y", radius = 1.6)
plt.pie([1], colors="c", radius = 1.3)
plt.pie([1], colors="b", radius = 1.1)
plt.pie([1], colors="m", radius = 0.9)

plt.pie([1], colors="b", radius = 0.31)
plt.pie(labels, colors=colors, radius = 0.3)

plt.pie([1], colors="w", radius = 0.2)
plt.pie([1], colors="k", radius = 0.1)

plt.show()

Output >>>

Fig 1.8 – Matplotlib pie charts

Conclusion

In the matplotlib plt.pie chart blog, we learn how to plot one and multiple pie charts with a real-time example using the plt.pie() method. Along with that used different method and different parameter. We suggest you make your hand dirty with each and every parameter of the above methods. This is the best coding practice. After completion of the matplotlib tutorial jump on Seaborn.

Download Jupyter file of matplotlib bar chart source code

Visit the official site of matplotlib.org

1 thought on “Matplotlib Pie Chart – plt.pie() | Python Matplotlib Tutorial”

Leave a Reply