Donut Pie-Chart using Matplotlib
By: Dhruv Anurag
Tags: Exploratory Data Analysis, Pie Charts, Matplotlib, Data Visualization, Data Science

Most of the data analysts and data scientists use different sorts of visualization techniques for data analysis and EDA. But after going through a lot of Kaggle notebooks and projects, I couldn’t find data visualized in the form of pie-charts. Although I know, people prefer histograms and bar plots over pie-charts because of the significance they represent in one view. But if pie-charts are used precisely as per their requirement, it can create more sense than most of the bar plots and histograms.

So, we’ll take the pie-chart game to a level ahead and create a custom donut pie-chart. Sounds interesting? Well, go through to find out!

We’ll import matplotlib.pyplot as this is the only required library to generate our donut pie-charts.

Run “ import matplotlib.pyplot as plt” in your first cell.

Now we’ll define two lists namely: list_headings and list_data. We’ll use custom data for visualizations.

After that, we’ll create a solid circle using plt.Circle with custom dimensions which will create a hollow space inside our pie-chart to give it a donut-like shape.

Next, we’ll create a simple pie-chart using plt.pie(). We’ll add our list_data and list_headings as our initial arguments to visualize the data.

After that, we will create a plt.gcf() object. This function in the pyplot module of the matplotlib library is used to get the current axes instance on the current figure matching the given keyword args or create one.

Now. we’ll add our solid circle to pyplot using add_artist method of gca() function.

And finally, we’ll write that simply awesome word combination — plt.show()

And the result would look similar to this:

Full Code:

But this is not the end of our article. We need to customize this donut pie-chart to make it more attractive and visually appealing.

We’ll add an empty space between each segment of our donut pie-chart simply called wedge drops.

We’ll add one more argument to our plt.pie() to achieve the desired output which is wedgedrops.

And your output will look absolutely similar to this:

Full Code:

You can add custom colors to your donut pie chart as well. Define a list of colors that you want to use for visualization. Add the colors as an argument to plt.pie() to implement. Full code:

This generation’s obsession with black color is totally mind-boggling. Everybody wants to wear black, code in dark mode, and own black accessories. So this one’s for them specifically. We’ll add a black background to our donut pie-chart so that it looks more appealing and readable than previous visualizations. We’ll use fig.patch.set_facecolor() to add custom color background to our donut-pie chart. Also, we’ll convert our labels’ text color to white so that it is readable on black background.

And your ultimate result would look like this:

Full Code:

And yes, this is it. Now, you can create your own customized donut pie-charts for visualizations.

You can check this Kaggle notebook to check out how to plot multiple pie-charts in multiple rows and columns and add a legend to pie-charts.

Have a great day of learning!