How to insert Pie Chart in Matplotlib?

Learn how to create a pie chart using the Matplotlib library in Python, presented in a straightforward manner.

matplotlib pie chart edgecolor
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.

new empty matplotlib 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()

matplotlib pie chart title

Title is here. Now let’s add labels.

See also  How to create a BarPlot in SeaBorn?

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()

matplotlib pie chart labels

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()

matplotlib pie chart autopercentage

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()

matplotlib pie chart explode slice

A slice is exploded out now.

See also  How to Make a Kdeplot in Seaborn

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()

matplotlib pie chart startangle

This is much better! What can I do to make it more beutiful?

See also  How to create Seaborn Heatmap

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()

matplotlib pie chart shadow

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.

matplotlib pie chart enlarge

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()

matplotlib pie chart edgecolor