Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

matplotlib
pie
chart
communitycreator

How to create a pie chart using Matplotlib in Python

Onyejiaku Theophilus Chidalu

Overview

A pie chart is a pictorial graph in the shape of a circle, with different wedges or segments representing different data values.

We draw pie charts in matplotlib using the method pie(), which is found in the pyplot module.

Syntax

pyplot.pie()

Parameter value

The pie() method takes the data array which represents the wedge sizes of the pie chart.

Code example

from matplotlib.pyplot import pie, show
from numpy import array

data_set = array([25, 20, 50, 15])

pie(data_set)
show() 
Implementing the pie() function

Code explanation

  • Line 1: We import pie() and show() from the matplotlib.pyplot module.

  • Line 2: We import array() from NumPy library.

  • Line 4: We use the array() method to create a data array.

  • Line 6: We use the pie() method to plot a pie chart.

  • Line 7: We use the show() method to show our plot.

RELATED TAGS

matplotlib
pie
chart
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring