,

Doughnut Plot with Python? – How to Create it?

Let us go like this, imagine you have a dataset that represents different categories, each with its own proportion or percentage. A doughnut plot elegantly displays this information, with each category represented by a segment of the doughnut’s ring just like a real doughnut which most of us love to eat. Just like one’s below

Sounds interesting, But what about the size? – The size of each segment corresponds to the proportion of that category in the dataset, allowing viewers to quickly grasp the relative importance of each category.

In this article i will show you how you can create a doughnut plot or a chart to impress your audience or boss and boost your data science career.

What all things can a doughtnut plot visulaize?

It can visualize market share, survey responses, or any other categorical data, the plot offers a visually striking and informative way to present your findings. Let’s get started with the coding part to start this foody journey of learning.

First of all create a file in your editor and save it with a .py extension and copy paste the code below:

Note: You can make changes according due your data and needs.

Full Code:

import matplotlib.pyplot as plt

# Data to plot
labels = 'A', 'B', 'C', 'D'
sizes = [15, 30, 45, 10]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
 
# Plot
plt.pie(sizes, labels=labels, colors=colors, startangle=90, counterclock=False)
plt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle

# Add a circle at the center to create a doughnut plot
centre_circle = plt.Circle((0,0),0.70,fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)

plt.title('Doughnut Plot')
plt.show()

Output:

Explanation of the above code:

  1. import matplotlib.pyplot as plt: Imports the matplotlib.pyplot module and aliases it as plt, which is a common convention.
  2. labels = 'A', 'B', 'C', 'D': Defines the labels for the categories in the doughnut plot.
  3. sizes = [15, 30, 45, 10]: Defines the sizes or proportions of each category. These values determine the size of each segment in the doughnut plot.
  4. colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']: Defines the colors for each category segment in the doughnut plot.
  5. plt.pie(sizes, labels=labels, colors=colors, startangle=90, counterclock=False): Creates a pie chart with the specified sizes, labels, colors, starting angle (startangle), and direction (counterclock). This function plots the initial pie chart.
  6. plt.axis('equal'): Sets the aspect ratio of the plot to ensure that the pie chart is drawn as a circle.
  7. centre_circle = plt.Circle((0,0),0.70,fc='white'): Defines a white circle (fc='white') at the center of the plot to create the doughnut effect. The circle is positioned at (0,0) and has a radius of 0.70.
  8. fig = plt.gcf(): Gets the current figure (the pie chart).
  9. fig.gca().add_artist(centre_circle): Adds the white circle to the current figure, effectively creating the doughnut plot.
  10. plt.title('Doughnut Plot'): Sets the title of the plot to ‘Doughnut Plot’.
  11. plt.show(): Displays the final doughnut plot.

In conclusion, when you want to show the relationship between different parts and the whole dont forget to think about doughnut plot. By using the matplotlib.pyplot library, you can easily generate doughnut plots that are both informative and aesthetically pleasing.

With the ability to customize colors, labels, and sizes, you can create engaging visualizations that effectively convey your data. Whether you’re illustrating market share, budget allocations, or any other segmented data, the doughnut plot provides a clear and concise representation.

By following the example provided, you can quickly create your own doughnut plots in Python and enhance your data visualization capabilities. Don’t forget to Experiment with different data sets and customizations to create unique and impactful visualizations for your projects.

Author

Sona Avatar

Written by

One response to “Doughnut Plot with Python? – How to Create it?”

  1. […] random: This line imports the random module, which provides functions for generating random […]

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>