Let’s learn to insert a pie chart in the Python Matplotlib library. I’ll show you the easiest way.
To insert a chart, we need to prepare the data for visualization.
import matplotlib.pyplot as plt products_sold = [55, 72, 63, 47, 58] salesman = ['Joe', 'Monica', 'Tom', 'Ana', 'Paul']
I imported matplotlib and prepared a list of salesman and products they sold.
Plt pie
To create a pie chart I need to use pie function and use products_sold as an argument
import matplotlib.pyplot as plt products_sold = [55, 72, 63, 47, 58] salesman = ['Joe', 'Monica', 'Tom', 'Ana', 'Paul'] plt.pie(products_sold) plt.show()
This is the first version of pie chart.
I like it but it needs a few improvements because it is not very useful.
Plt title
Let’s start from adding a title with title function.
import matplotlib.pyplot as plt products_sold = [55, 72, 63, 47, 58] salesman = ['Joe', 'Monica', 'Tom', 'Ana', 'Paul'] plt.pie(products_sold) plt.title('Python course sales') plt.show()
Title is here. Now let’s add labels.
Adding labels
To add labels I added labels as an additional parameter to pie function. I set salesman as a list of labels.
import matplotlib.pyplot as plt products_sold = [55, 72, 63, 47, 58] salesman = ['Joe', 'Monica', 'Tom', 'Ana', 'Paul'] plt.pie(products_sold, labels=salesman) plt.title('Python course sales') plt.show()
It is getting better. Now at least I know what is this pie chart about.
Adding values
Next thing I need are values.
To add values I used autopct argument. To define the format of my values I used %.0f%%’ because I can see digits without decimals (0 decimals).
import matplotlib.pyplot as plt products_sold = [55, 72, 63, 47, 58] salesman = ['Joe', 'Monica', 'Tom', 'Ana', 'Paul'] plt.pie(products_sold, labels=salesman, autopct='%.0f%%') plt.title('Python course sales') plt.show()
With autopercentage I can see the first basic version of pie chart I can see.
Define an explode
Still it is not enough for me. I’d like explode the sales of Monica. To explode a slice of pie chart in Matplotlib I need to define explode list first. 0.2 is how much it needs to be exploded. Then I added explod parameter to pie function.
import matplotlib.pyplot as plt products_sold = [55, 72, 63, 47, 58] salesman = ['Joe', 'Monica', 'Tom', 'Ana', 'Paul'] explode = [0, 0.2, 0, 0, 0] plt.pie(products_sold, labels=salesman, autopct='%.0f%%', explode=explode) plt.title('Python course sales') plt.show()
A slice is exploded out now.
Adding a startangle parameter
I’m still not very pleased with the chart. Exploded slice is too close to the chart’s title. I’d like to move the chart a bit counterclockwise. Adding startangle parameter with value of 20 does the job.
import matplotlib.pyplot as plt products_sold = [55, 72, 63, 47, 58] salesman = ['Joe', 'Monica', 'Tom', 'Ana', 'Paul'] explode = [0, 0.2, 0, 0, 0] plt.pie(products_sold, labels=salesman, autopct='%.0f%%', explode=explode, startangle=20) plt.title('Python course sales') plt.show()
This is much better! What can I do to make it more beutiful?
Enable a shadow effect
I’d like to see it more 3D. To do it I need just to enable shadow effect.
import matplotlib.pyplot as plt products_sold = [55, 72, 63, 47, 58] salesman = ['Joe', 'Monica', 'Tom', 'Ana', 'Paul'] explode = [0, 0.2, 0, 0, 0] plt.pie(products_sold, labels=salesman, autopct='%.0f%%', explode=explode, startangle=20, shadow=True) plt.title('Python course sales') plt.show()
The radius parameter
If you’d like to enlarge your pie chart you can also do it easily. Radius parameter is responsible for pie chart size. By deafault it is 1. To increase the size by 10% just use 1.1 like that.
Also adding edge color would look nice. I’d like to see black. To do it you need to use wedgeprops parameter.
import matplotlib.pyplot as plt products_sold = [55, 72, 63, 47, 58] salesman = ['Joe', 'Monica', 'Tom', 'Ana', 'Paul'] explode = [0, 0.2, 0, 0, 0] plt.pie(products_sold, labels=salesman, autopct='%.0f%%', explode=explode, startangle=20, shadow=True, radius=1.1, wedgeprops={'edgecolor': 'black'}) plt.title('Python course sales') plt.show()
My pie chart is ready. I like it a lot. I hope you do as well.
Key Takeaways
To insert a pie chart in Matplotlib, you can use the plt.pie() function.
The plt.pie() function takes a list of values as its argument.
You can also specify labels for the pie chart slices by using the labels argument.
You can use the autopct argument to display the percentage of each slice in the pie chart.
You can use the explode argument to explode one or more pie chart slices.
You can use the startangle argument to rotate the pie chart.
You can use the shadow argument to add a shadow to the pie chart.
You can use the radius argument to increase or decrease the size of the pie chart.
You can use the wedgeprops argument to specify the properties of the pie chart wedges, such as the edge color.
FAQs
Q: What is the difference between a pie chart and a bar chart?
A pie chart is a circular chart that shows the relative sizes of different parts of a whole. A bar chart is a rectangular chart that shows the values of different variables along a single axis.
Q: When should I use a pie chart?
You should use a pie chart when you want to show the relative sizes of different parts of a whole. For example, you could use a pie chart to show the percentage of students in a class who passed an exam.
Q: What are the limitations of pie charts?
Pie charts can be difficult to read when there are a lot of slices. They can also be difficult to compare different pie charts if they have different sizes.