Udit Vashisht
Author: Udit Vashisht


Pie Charts in Python | Matplotlib Tutorial in Python | Chapter 3

  • 5 minutes read
  • 78 Views
Pie Charts in Python | Matplotlib Tutorial in Python | Chapter 3

Matplotlib Tutorial in Python

Chapter 3 | Creating Pie Charts in Python

In this chapter, we will learn to create Pie Charts in Matplotlib in Python. Pie Charts are an intiutive way of showing data, wherein each category is shown as a portion of the pie. Let us get started.

Matplotlib Tutorials in Python | Creating a simple Pie Chart in Matplotlib

In Pie Chart data is shown as slices for each category. A pie chart is plotted using plt.pie() method.

# piechart_in_matplotlib.py

import matplotlib.pyplot as plt

slices = [70, 30]
plt.pie(slices)
plt.title("A Simple Pie Chart")
plt.show()

matplotlib_tutorial_pie_chart.png

Matplotlib Tutorials in Python - Adding labels and edges to Pie Chart

We can add labels to our pie chart using the following code :-

# piechart_in_matplotlib.py

labels = ['seventy', 'thirty']
plt.pie(slices, labels = labels)

If we want to add edges to each portion/slice of the pie we will be passing wedgeprops as an argument to plt.pie() which takes a dictionary with keys like edgecolor, linewidth etc. and their values.

# piechart_in_matplotlib.py

slices = [70, 30]
labels = ['Seventy', 'Thirty']
plt.pie(slices, labels = labels, wedgeprops={'edgecolor' :'blue'})

matplotlib_tutorial_piechart.png

Matplotlib Tutorials in Python - Adding custom colors to the Pie Chart in Matplotlib

Instead of using automatic colors, you can also use customs colors for the data in your Matplotlib Pie Chart. For that, you will have to pass a list of custom colors (having as many colors as items) to plt.pie() method. Instead of common color names, you can also pass hex values to Matplotlib Pie Chart.

# piechart_in_matplotlib.py

import matplotlib.pyplot as plt

slices = [70, 30, 20, 40]
labels = ['Seventy', 'Thirty', 'Other', 'Another']
colors = ['red', 'yellow', 'blue', 'green']
plt.pie(slices, labels=labels, wedgeprops={'edgecolor': 'black'})
plt.title("A Simple Pie Chart")
plt.show()

matplotlib_tutorial_piechart_.png

Matplotlib Tutorials in Python - Creating Pie Chart of Population Data

In chapter 1 and chapter 2 we plotted total population of India for ages 12 to 21. Now, we will be using that data to plot Python Pie Chart.

# piechart_in_matplotlib.py

import matplotlib.pyplot as plt

ages = [12, 13, 14, 15, 16, 17, 18, 19, 20, 21]

total_population = [27877307, 24280683, 25258169, 25899454, 24592293, 21217467, 27958147, 20859088, 28882735, 19978972]

plt.pie(total_population, labels=ages, wedgeprops={'edgecolor': 'black'})
plt.title("A Simple Pie Chart")
plt.show()

matplotlib_tutorial_piechart__.png

Since, our data is uniformly distribued i.e. each age group has almost similar population, this pie chart looks a bit descriptive. However, a pie-chart is suitable only to analyse 3-5 items.

In the above chart, you might have noticed that the values/items are moving in counter-clockwise direction from 12 to 21. If for some reason, you want to move it in clockwise direction. You have to set counterclock parameter of plt.pie() as False.You can check rest of the parameters here.

# piechart_in_matplotlib.py

plt.pie(total_population, labels=ages, wedgeprops={'edgecolor': 'black'}, counterclock=False)

matplotlib_tutorial_piechart_clockwise.png

Making certain slice of Pie Chart explode in Matplotlib

If you want to put emphasise on certain slice and want it to look different or look-out of the whole data, you can pass explode argument to the plt.pie(), which takes a list of floats, where each item represents the fraction of radius by which you want your slice to move away from the center. 0 means no explode and 0.1 will mean the slice will move away from the centre by 10% of the radius.

# piechart_in_matplotlib.py

import matplotlib.pyplot as plt

ages = [17, 18, 19, 20, 21]

total_population = [21217467, 27958147, 20859088, 28882735, 19978972]
explode = [0, 0, 0, 0, 0.1]

plt.pie(total_population, labels=ages, explode=explode, wedgeprops={'edgecolor': 'black'})
plt.title("A Simple Pie Chart")
plt.show()

matplotlib_tutorial_piechart_explode.png

In the above chart we wanted to make age 21 stand-out so we have set its corresponding values as 0.1 in explode and then passed explode as explode list in plt.pie().

Matplotlib Tutorials in Python - Customizing the Pie Chart

You can add shadow to the Pie Chart by using shadow = True, also we can add percentage to our pie chart

# piechart_in_matplotlib.py

plt.pie(total_population, labels=ages, explode=explode,
        shadow=True,
        autopct='%1.1f%%',
        wedgeprops={'edgecolor': 'black'})

matplotlib_tutorial_piechart_shadow.png

Table of Contents of Matplotlib Tutorial in Python

Matplotlib Tutorial in Python | Chapter 1 | Introduction

Matplotlib Tutorial in Python | Chapter 2 | Extracting Data from CSVs and plotting Bar Charts

Pie Charts in Python | Matplotlib Tutorial in Python | Chapter 3

Matplotlib Stack Plots/Bars | Matplotlib Tutorial in Python | Chapter 4

Filling Area on Line Plots | Matplotlib Tutorial in Python | Chapter 5

Python Histograms | Matplotlib Tutorial in Python | Chapter 6

Scatter Plotting in Python | Matplotlib Tutorial | Chapter 7

Plot Time Series in Python | Matplotlib Tutorial | Chapter 8

Python Realtime Plotting | Matplotlib Tutorial | Chapter 9

Matplotlib Subplot in Python | Matplotlib Tutorial | Chapter 10

Python Candlestick Chart | Matplotlib Tutorial | Chapter 11

If you have liked our tutorial, there are various ways to support us, the easiest is to share this post. You can also follow us on facebook, twitter and youtube.

In case of any query, you can leave the comment below.

If you want to support our work. You can do it using Patreon.

In the next chapter we will learn about drawing Stack Plots in Python



Related Posts

Plot Time Series in Python | Matplotlib Tutorial | Chapter 8
By Udit Vashisht

Plot Time Series in Python using Matplotlib

Plot Time Series in Python | Chapter 8

In this tutorial we will learn to plot time series data in Python using Matplotlib. We will also be using pandas dataframe to plot time series data in python from ...

Read More
Matplotlib Tutorial in Python | Chapter 1 | Introduction
By Udit Vashisht

Matplotlib Tutorial in Python

Chapter 1 | Introduction

In this series of Matplotlib Tutorials in Python, we will cover all concepts of matplotlib from beginners to expert. Starting with how to install Matplotlib to how to create matplotlib plots, this series is the only matplotlib tutorial which you will need. ...

Read More
Python object-oriented programming (OOP) - A complete tutorial
By Udit Vashisht

Python object-oriented programming (OOP)

Object-oriented programming

Object-oriented programming also known as OOP is a programming paradigm that is based on objects having attributes (properties) and procedures (methods). The advantage of using Object-oriented programming(OOP) is that it helps in bundling the attributes and procedures into objects or modules. We can ...

Read More
Search