Need to create a pie chart using matplotlib?
If so, you can use the following template to create your pie chart:
import matplotlib.pyplot as plt my_data = [value1,value2,value3,...] my_labels = 'label1','label2','label3',... plt.pie(my_data,labels=my_labels,autopct='%1.1f%%') plt.title('My Title') plt.axis('equal') plt.show()
Next, I’ll review an example with the steps to create different types of pie charts.
Steps to Create a Pie Chart using Matplotlib
Step 1: Gather the Data for the Pie Chart
To start, you’ll need to gather the data for the pie chart.
For example, I gathered the following data about the status of tasks:
Tasks Pending | 300 |
Tasks Ongoing | 500 |
Tasks Completed | 700 |
The goal is to create a pie chart based on the above data.
Step 2: Plot the Pie Chart using Matplotlib
Next, plot the pie chart using matplotlib.
You can use the template below to assist with the plotting of the chart:
import matplotlib.pyplot as plt my_data = [value1,value2,value3,...] my_labels = 'label1','label2','label3',... plt.pie(my_data,labels=my_labels,autopct='%1.1f%%') plt.title('My Title') plt.axis('equal') plt.show()
This is how the complete code would look like for our example:
import matplotlib.pyplot as plt Tasks = [300,500,700] my_labels = 'Tasks Pending','Tasks Ongoing','Tasks Completed' plt.pie(Tasks,labels=my_labels,autopct='%1.1f%%') plt.title('My Tasks') plt.axis('equal') plt.show()
Run the code in Python and you’ll get the following pie chart:
Step 3: Style the Chart
You can further style the pie chart by adding:
- Start angle
- Shadow
- Colors
- Explode component
This is the code that you can use for the styled chart:
import matplotlib.pyplot as plt Tasks = [300,500,700] my_labels = 'Tasks Pending','Tasks Ongoing','Tasks Completed' my_colors = ['lightblue','lightsteelblue','silver'] my_explode = (0, 0.1, 0) plt.pie(Tasks, labels=my_labels, autopct='%1.1f%%', startangle=15, shadow = True, colors=my_colors, explode=my_explode) plt.title('My Tasks') plt.axis('equal') plt.show()
You’ll then get the chart below:
Create a Chart based on Pandas DataFrame
You can also create your pie chart based on pandas DataFrame.
For our example, the DataFrame (with the tasks data) would look like this:
from pandas import DataFrame Data = {'Tasks': [300,500,700]} df = DataFrame(Data,columns=['Tasks']) print (df)
This is the DataFrame that you’ll get:
And here is the complete Python code to create the pie chart based on the data in the DataFrame:
from pandas import DataFrame import matplotlib.pyplot as plt Data = {'Tasks': [300,500,700]} df = DataFrame(Data,columns=['Tasks']) my_labels = 'Tasks Pending','Tasks Ongoing','Tasks Completed' plt.pie(df,labels=my_labels,autopct='%1.1f%%') plt.title('My Tasks') plt.axis('equal') plt.show()
Once you run the code, you’ll get the same pie chart:
You may then choose to style the chart using the following syntax:
from pandas import DataFrame import matplotlib.pyplot as plt Data = {'Tasks': [300,500,700]} df = DataFrame(Data,columns=['Tasks']) my_labels = 'Tasks Pending','Tasks Ongoing','Tasks Completed' my_colors = ['lightblue','lightsteelblue','silver'] my_explode = (0, 0.1, 0) plt.pie(df, labels=my_labels, autopct='%1.1f%%', startangle=15, shadow = True, colors=my_colors, explode=my_explode) plt.title('My Tasks') plt.axis('equal') plt.show()
You’ll then get the following styled chart:
For other types of charts, you may want to check the following source that explains the steps to create scatter, line and bar charts using matplotlib.