Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Pandas
  • Numpy
  • Seaborn
  • Ploty
  • Data visualization
  • Data Analysis
  • Power BI
  • Tableau
  • Machine Learning
  • Deep Learning
  • NLP
  • Computer Vision
  • Data Science for Beginners
  • Data Science interview questions
  • Data analysis interview questions
  • NLP Interview questions
Open In App
Next Article:
Line chart in Matplotlib - Python
Next article icon

Donut Chart using Matplotlib in Python

Last Updated : 27 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisites: 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 Simple Donut Chart

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 Chart Employee = ['Roshni', 'Shyam', 'Priyanshi',             'Harshit', 'Anmol']  # Setting size in Chart based on  # given values Salary = [40000, 50000, 70000, 54000, 44000]  # colors colors = ['#FF0000', '#0000FF', '#FFFF00',            '#ADFF2F', '#FFA500'] # explosion explode = (0.05, 0.05, 0.05, 0.05, 0.05)  # Pie Chart plt.pie(Salary, colors=colors, labels=Employee,         autopct='%1.1f%%', pctdistance=0.85,         explode=explode)  # draw circle centre_circle = plt.Circle((0, 0), 0.70, fc='white') fig = plt.gcf()  # Adding Circle in Pie chart fig.gca().add_artist(centre_circle)  # Adding Title of chart plt.title('Employee Salary Details')  # Displaying Chart plt.show() 

Output:

Customizing the Donut Chart

Adding Legends to the Donut Chart 

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 values sizes = [100, 500, 70, 54, 440]  # Setting labels for items in Chart labels = ['Apple', 'Banana', 'Mango', 'Grapes', 'Orange']  # colors colors = ['#FF0000', '#0000FF', '#FFFF00', '#ADFF2F', '#FFA500']  # explosion explode = (0.05, 0.05, 0.05, 0.05, 0.05)  # Pie Chart plt.pie(sizes, colors=colors, labels=labels,         autopct='%1.1f%%', pctdistance=0.85,          explode=explode)  # draw circle centre_circle = plt.Circle((0, 0), 0.70, fc='white') fig = plt.gcf()  # Adding Circle in Pie chart fig.gca().add_artist(centre_circle)  # Adding Title of chart plt.title('Favourite Fruit Survey')  # Add Legends plt.legend(labels, loc="upper right")  # Displaying Chart plt.show() 

Output:

Adding Title to the Legend Box in Donut Chart

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 values sizes = [100, 500, 70, 54, 440]  # Setting labels for items in Chart labels = ['Apple', 'Banana', 'Mango', 'Grapes',           'Orange']  # colors colors = ['#FF0000', '#0000FF', '#FFFF00', '#ADFF2F',           '#FFA500']  # explosion explode = (0.05, 0.05, 0.05, 0.05, 0.05)  # Pie Chart plt.pie(sizes, colors=colors, labels=labels,         autopct='%1.1f%%', pctdistance=0.85,         explode=explode)  # draw circle centre_circle = plt.Circle((0, 0), 0.70, fc='white') fig = plt.gcf()  # Adding Circle in Pie chart fig.gca().add_artist(centre_circle)  # Adding Title of chart plt.title('Favourite Fruit Survey')  # Add Legends plt.legend(labels, loc="upper right", title="Fruits Color")  # Displaying Chart plt.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
# library import matplotlib.pyplot as plt  # list of name of students names = ['Manish', 'Atul', 'Priya', 'Harshit']  # list of their respective marks marks = [45, 66, 55, 77]  # Create a circle at the center of # the plot my_circle = plt.Circle((0, 0), 0.7, color='white')  # Give color names plt.pie(marks, labels=names, autopct='%1.1f%%',         colors=['red', 'green', 'blue', 'yellow'])  p = plt.gcf() p.gca().add_artist(my_circle)  # Show the graph plt.show() 

Output:


Next Article
Line chart in Matplotlib - Python
author
srivastavaharshit848
Improve
Article Tags :
  • Python
  • Python-matplotlib
Practice Tags :
  • python

Similar Reads

  • Line chart in Matplotlib - Python
    Matplotlib is a data visualization library in Python. The pyplot, a sublibrary of Matplotlib, is a collection of functions that helps in creating a variety of charts. Line charts are used to represent the relation between two data X and Y on a different axis. In this article, we will learn about lin
    6 min read
  • Python | Basic Gantt chart using Matplotlib
    Prerequisites : Matplotlib IntroductionIn this article, we will be discussing how to plot a Gantt Chart in Python using Matplotlib.A Gantt chart is a graphical depiction of a project schedule or task schedule (In OS). It's is a type of bar chart that shows the start and finish dates of several eleme
    3 min read
  • Plot a Pie Chart in Python using Matplotlib
    A Pie Chart is a circular statistical plot that can display only one series of data. The area of the chart is the total percentage of the given data. Pie charts in Python are widely used in business presentations, reports, and dashboards due to their simplicity and effectiveness in displaying data d
    8 min read
  • Bar chart using Plotly in Python
    Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization librar
    4 min read
  • Errorbar graph in Python using Matplotlib
    Error bars are a graphical overlay used to display the variability or uncertainty of points plotted on a Cartesian graph. They provide a further level of information to data shown, giving an indication of the accuracy of measurements and making a more accurate representation of variability in the da
    3 min read
  • Change plot size in Matplotlib - Python
    Plots are an effective way of visually representing data and summarizing it beautifully. However, if not plotted efficiently it seems appears complicated. Python's Matplotlib provides several libraries for data representation. While making a plot we need to optimize its size. In this article, we wil
    3 min read
  • Filled area chart using plotly in Python
    Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histograms, bar plots, box plots, spread plots, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization
    6 min read
  • How to Draw 3D Cube using Matplotlib in Python?
    In this article, we will deal with the 3d plots of cubes using matplotlib and Numpy. Cubes are one of the most basic of 3D shapes. A cube is a 3-dimensional solid object bounded by 6 identical square faces. The cube has 6-faces, 12-edges, and 8-corners. All faces are squares of the same size. The to
    6 min read
  • Matplotlib.dates.epoch2num() in Python
    Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.dates.epoch2num() The matplotlib.dates.epoch2num() function is used to conver
    2 min read
  • How to import matplotlib in Python?
    Matplotlib is a Python library used to create different types of charts and graphs. It helps to turn data into visual formats like line charts, bar graphs and histograms. This makes it easier to understand and present your data. In this guide you’ll learn how to install and import Matplotlib in Pyth
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences