Changing the pie chart to the donut chart to look more efficient and cool

Jahid Hasan
4 min readJul 10, 2019

--

In this article, I will discuss the donut chart for data visualization. This article is related to my previous note which link is https://bit.ly/32hR9Ps. Please read this tutorial to better understand this concept. This article is inspired by this source (Link). There is not much difference between link post and this post. The only difference is that I have posted this post with my data and for the maintenance of sequence. Now we go to our topics.

First of all, what is the donut chart?

According to Datavizcatalogue, A donut chart is essentially a Pie Chart with an area of the center cut out. Pie Charts are sometimes criticized for focusing readers on the proportional areas of the slices to one another and to the chart as a whole. This makes it tricky to see the differences between slices, especially when you try to compare multiple Pie Charts together. A Donut Chart somewhat remedies this problem by de-emphasizing the use of the area. Instead, readers focus more on reading the length of the arcs, rather than comparing the proportions between slices. Also, Donut Charts are more space-efficient than Pie Charts because the blank space inside a Donut Chart can be used to display information inside it.

Now going to our previous tutorial mathematical example whose data table is given back once again,

>>> import matplotlib.pyplot as plt
>>> name = ['Islam', 'Christians', 'Hindu', 'Buddha', 'Others']
>>> people = [2000, 2700, 2400, 2100, 1200]
>>> plt.pie(people, labels=name, autopct='%0.f%%', colors = colors, shadow=True, startangle=90)
>>> circle = plt.Circle((0,0),0.85,fc='white') #draw circle for look like donut chart
>>> donut = plt.gcf()
>>> donut.gca().add_artist(circle)
>>> plt.axis('equal')
>>> plt.tight_layout()

You can change border size we use 0.85 now we will change 0.85 to 0.65, then you will see what is the difference,

>>> import matplotlib.pyplot as plt
>>> name = ['Islam', 'Christians', 'Hindu', 'Buddha', 'Others']
>>> people = [2000, 2700, 2400, 2100, 1200]
>>> plt.pie(people, labels=name, autopct='%0.f%%', colors = colors, shadow=True, startangle=90)
>>> circle = plt.Circle((0,0),0.65,fc='white')
>>> donut = plt.gcf()
>>> donut.gca().add_artist(circle)
>>> plt.axis('equal')
>>> plt.tight_layout()

Change both outer and percent labels

We can change both outer and percent labels. And look more efficient

>>> import matplotlib.pyplot as plt
>>> name = ['Islam', 'Christians', 'Hindu', 'Buddha', 'Others']
>>> people = [2000, 2700, 2400, 2100, 1200]
>>> plt.pie(people, labels=name, autopct='%0.f%%', colors = colors, shadow=True, startangle=90, pctdistance=0.90, explode =(0.10,0.10,0.10,0.10,0.10)) #By default pectdistance is 0.6
>>> circle = plt.Circle((0,0),0.65,fc='white')
>>> donut = plt.gcf()
>>> donut.gca().add_artist(circle)
>>> plt.axis('equal')
>>> plt.tight_layout()

Multiple donut charts awesome visualization

Now we will some little bit of change in our dataset. Add to the dataset how many men and how many women are there for every religion.

Pie charts for above Male and Female data will be as follows:

Instead of visualizing different pie charts of gender (male and female) for various religion individuals, we can actualize all pie chart in a single outline like given underneath:

>>> import matplotlib.pyplot as plt
>>> name = ['Islam', 'Christians', 'Hindu', 'Buddha', 'Others']
>>> people = [2000, 2700, 2400, 2100, 1200]
>>> gender = [1200,800,1300,1400,1250,1150,1000,1100,700,500]
>>> labels_gender = ['Male','Female']
>>> colors_religion=['blue','green','red','cyan','magenta']
>>> colors_gender = ['yellow','black']
>>> plt.pie(people, labels=name, colors=colors_religion, startangle=90,frame=True)
>>> plt.pie(gender,colors=colors_gender,radius=0.75,startangle=90)
>>> centre_circle = plt.Circle((0,0),0.5,color='black', fc='white',linewidth=0)
>>> fig = plt.gcf()
>>> fig.gca().add_artist(centre_circle)
>>> plt.legend(name,title="indicates", loc="lower right")
>>> plt.axis('equal')
>>> plt.tight_layout()
>>> plt.show()

If you want more efficient this chart. You can just use explosion property of slices of the pie chart to make it cooler. Let’s do it,

>>> import matplotlib.pyplot as plt
>>> name = ['Islam', 'Christians', 'Hindu', 'Buddha', 'Others']
>>> people = [2000, 2700, 2400, 2100, 1200]
>>> gender = [1200,800,1300,1400,1250,1150,1000,1100,700,500]
>>> labels_gender = ['Male','Female']
>>> colors_religion=['blue','green','red','cyan','magenta']
>>> colors_gender = ['yellow','black']
>>> plt.pie(people, labels=name, colors=colors_religion,explode = (0.2,0.2,0.2,0.2,0.2), startangle=90,frame=True,radius=3)
>>> plt.pie(gender,colors=colors_gender,explode= (0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2),radius=2,startangle=90)
>>> centre_circle = plt.Circle((0,0),0.5,color='black', fc='white',linewidth=0)
>>> fig = plt.gcf()
>>> fig.gca().add_artist(centre_circle)
>>> plt.legend(name,title="indicates", loc="lower right")
>>> plt.axis('equal')
>>> plt.tight_layout()
>>> plt.show()

Thank you so much guys and especially thanks to Mr. Kevin Amipara.

--

--

Jahid Hasan

Enthusiast of Data Science | Data Analyst | Machine Learning | Artificial intelligence | Deep Learning | Author || ps: https://msjahid.github.io/