Introduction

In this tutorial, we want to create a Pie Chart. In order to do this, we use the pie() function of Matplotlib.

Import Libraries

First, we import the Pyplot submodule. The Pyplot submodule contains a collection of important functions such as the plot() function or the pie() function.

import matplotlib.pyplot as plt

Define Data

Now, let's define our example data. We consider the average amount of hours a person spends per week in learning various Data Science Topics.

labels = ["Python", "SQL", "Deep Learning", "Data Visualization"]
hours = [7, 6, 5, 2]

Basic Pie Chart

We would like to draw a Pie Chart, that visualizes the amount of hours spent per data science topic. In order to do this, we use the pie() function. We also want to display the labels.

plt.pie(hours, labels=labels)
plt.show()

Highlight a Category in the Pie Chart

Now, we want to make a configuration on the chart. We want to highlight the hours spent on Python. In order to do this, we use the explode parameter.

explode = (0.1, 0, 0, 0)

plt.pie(hours, explode=explode, labels=labels)
plt.show()

Change Style of the Pie Chart

Next, we want to change the style of the Pie Chart. We want to change the colors, add the percentages of each data point and add a legend.

explode = (0.1, 0, 0, 0)

plt.pie(hours, explode=explode, labels=labels)
plt.show()

Conclusion

Congratulations! Now you are one step closer to become an AI Expert. You have seen that it is very easy to create a Pie Chart. We can simply use the pie() function of Matplotlib. Try it yourself!

Instagram

Also check out our Instagram page. We appreciate your like or comment. Feel free to share this post with your friends.