Matplotlib: Create Pie Chart Using plt.pie()

In this tutorial, we will use an example to show you how to create a pie graph using plt.pie() in matplotlib.

Matplotlib - Create Pie Chart Using plt.pie() for Beginners

1.Import library

import matplotlib.pyplot as plt

2.Prepare data

# defining labels 
time_table = ['write', 'exercise', 'eat', 'study'] 
  
# portion covered by each label 
time = [50, 10, 10, 80] 
  
# color for each label 
colors = ['r', 'lightblue', 'pink', 'b']

3.Draw pie graph

# plotting the pie chart 
plt.pie(time, labels = time_table, colors=colors,  
        startangle=90, shadow = True, autopct = '%1.1f%%')

4.Display pie chart

# plotting legend 
plt.legend() 
  
# showing the plot 
plt.show()

Run this code, you may see this pie chart.

Matplotlib: Create Pie Chart Using plt.pie()