Learn how to create a pie chart using the Matplotlib library in Python, presented in a straightforward manner.
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
Labels are added by setting the labels parameter in the pie function to the list of salespeople.
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
For a more three-dimensional appearance, the shadow effect is enabled by setting the shadow parameter to True.
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()