Donut Chart using Matplotlib in Python - GeeksforGeeks

02 Feb, 2022
Prequsities: Pie Chart in matplotlib
Donut charts are the modified version of Pie Charts with the area of center cut out. A donut is more concerned about the use of area of arcs to represent the information in the most effective manner instead of Pie chart which is more focused on comparing the proportion area between the slices. Donut charts are more efficient in terms of space because the blank space inside the donut charts can be used to display some additional information about the donut chart.
For being a Donut chart it must be necessarily a Pie chart. If we look at the pie chart, we will focus on the center of the chart. Donut charts, on the other hand, eliminates the need to compare the size or area of the slice and shifts the focus on the length of the arc, which in turn is easy to measure.
Creating a Donut Chart involves three simple steps which are as follows :
Create a Pie Chart
Draw a circle of suitable dimensions.
Add circle at the Center of Pie chart
Python3
import matplotlib.pyplot as plt  # Setting labels for items in ChartEmployee = ['Roshni', 'Shyam', 'Priyanshi',            'Harshit', 'Anmol']  # Setting size in Chart based on # given valuesSalary = [40000, 50000, 70000, 54000, 44000]  # colorscolors = ['#FF0000', '#0000FF', '#FFFF00',           '#ADFF2F', '#FFA500']# explosionexplode = (0.05, 0.05, 0.05, 0.05, 0.05)  # Pie Chartplt.pie(Salary, colors=colors, labels=Employee,        autopct='%1.1f%%', pctdistance=0.85,        explode=explode)  # draw circlecentre_circle = plt.Circle((0, 0), 0.70, fc='white')fig = plt.gcf()  # Adding Circle in Pie chartfig.gca().add_artist(centre_circle)  # Adding Title of chartplt.title('Employee Salary Details')  # Displaying Chartplt.show()
Output:
A graph legend generally appears in form of a box to the right or left in the graph. It contains small samples of each color on the graph as well as a short description of what each color means in the graph.
To add legends we will simply write the following code.
plt.legend(labels, loc = "upper right") 
Here plt.legend() takes two parameters the first is labels and loc is used to set the location of legend box.
Example: 
Python3
import matplotlib.pyplot as plt    # Setting size in Chart based on # given valuessizes = [100, 500, 70, 54, 440]  # Setting labels for items in Chartlabels = ['Apple', 'Banana', 'Mango', 'Grapes', 'Orange']  # colorscolors = ['#FF0000', '#0000FF', '#FFFF00', '#ADFF2F', '#FFA500']  # explosionexplode = (0.05, 0.05, 0.05, 0.05, 0.05)  # Pie Chartplt.pie(sizes, colors=colors, labels=labels,        autopct='%1.1f%%', pctdistance=0.85,         explode=explode)  # draw circlecentre_circle = plt.Circle((0, 0), 0.70, fc='white')fig = plt.gcf()  # Adding Circle in Pie chartfig.gca().add_artist(centre_circle)  # Adding Title of chartplt.title('Favourite Fruit Survey')  # Add Legendsplt.legend(labels, loc="upper right")  # Displaying Chartplt.show()
Output:
We can add a title to the Legend Box in Donut Chart by writing the following code:
plt.legend(labels, loc = "upper right",title="Fruits Color")
Example:
Python3
import matplotlib.pyplot as plt    # Setting size in Chart based on # given valuessizes = [100, 500, 70, 54, 440]  # Setting labels for items in Chartlabels = ['Apple', 'Banana', 'Mango', 'Grapes',          'Orange']  # colorscolors = ['#FF0000', '#0000FF', '#FFFF00', '#ADFF2F',          '#FFA500']  # explosionexplode = (0.05, 0.05, 0.05, 0.05, 0.05)  # Pie Chartplt.pie(sizes, colors=colors, labels=labels,        autopct='%1.1f%%', pctdistance=0.85,        explode=explode)  # draw circlecentre_circle = plt.Circle((0, 0), 0.70, fc='white')fig = plt.gcf()  # Adding Circle in Pie chartfig.gca().add_artist(centre_circle)  # Adding Title of chartplt.title('Favourite Fruit Survey')  # Add Legendsplt.legend(labels, loc="upper right", title="Fruits Color")  # Displaying Chartplt.show()
Output:
Example 2: Consider another situation that you have to prepare a report of marks obtained by different students in a test and visualize their performance by using a donut chart. To solve this problem we will use matplotlib library of python. The idea is that we will make a list of names of different students and another list of their respective marks and use this list to make a donut chart.
Python3
# libraryimport matplotlib.pyplot as plt  # list of name of studentsnames = ['Manish', 'Atul', 'Priya', 'Harshit']  # list of their respective marksmarks = [45, 66, 55, 77]  # Create a circle at the center of# the plotmy_circle = plt.Circle((0, 0), 0.7, color='white')  # Give color namesplt.pie(marks, labels=names, autopct='%1.1f%%',        colors=['red', 'green', 'blue', 'yellow'])  p = plt.gcf()p.gca().add_artist(my_circle)  # Show the graphplt.show()
Output:
varshagumber28
Python-matplotlib
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python