Linux

How To Create Donut Chart in Python

Create Donut Chart in Python

Data visualization is an essential skill for any data scientist or analyst. Among the various chart types available, donut charts stand out as an effective way to represent proportional data. In this comprehensive guide, we’ll explore how to create donut charts in Python, covering everything from basic concepts to advanced techniques.

Donut charts, also known as doughnut charts, are circular statistical graphics that display data in ring-shaped segments. They are similar to pie charts but with a hollow center, which gives them their distinctive “donut” appearance. This central space can be utilized to display additional information or simply to enhance the chart’s visual appeal.

While pie charts show the relationship of parts to a whole, donut charts offer a slight advantage by allowing for easier comparison between segments. They are particularly useful for displaying percentages or proportional data, such as market share, budget allocation, or survey results.

In this tutorial, we’ll primarily use Matplotlib, a powerful plotting library in Python, to create our donut charts. Let’s dive in and learn how to craft these visually appealing and informative graphics!

Understanding Donut Charts

Before we start coding, it’s crucial to understand the basic structure and use cases of donut charts:

  • Structure: A donut chart consists of a circular chart divided into segments, with a hole in the center.
  • Data representation: Each segment represents a category, and its size is proportional to the category’s value.
  • Center space: The hollow center can be used to display total values, labels, or other relevant information.

Donut charts excel in situations where you need to show the composition of a whole and the relationship between its parts. They are particularly effective when:

  • You have a small number of categories (ideally 5-7)
  • The values sum up to a meaningful whole (e.g., 100%)
  • You want to emphasize individual values while still showing the total

However, it’s important to note that donut charts may not be suitable for comparing values across different charts or when dealing with many categories, as it can become difficult to distinguish small segments.

Prerequisites

Before we begin creating donut charts, ensure you have the following prerequisites in place:

Required Python Libraries

We’ll primarily use Matplotlib for this tutorial. You can install it using pip:

pip install matplotlib

Additionally, we’ll use NumPy for data manipulation:

pip install numpy

Basic Python Knowledge

This tutorial assumes you have a basic understanding of Python programming, including concepts like variables, lists, and functions. If you’re new to Python, consider brushing up on these fundamentals before proceeding.

Environment Setup

You can use any Python environment of your choice, such as Jupyter Notebook, PyCharm, or VS Code. Ensure your environment is properly set up and can run Python scripts.

Basic Donut Chart Creation

Let’s start by creating a simple donut chart using Matplotlib. We’ll go through this process step-by-step:

Step 1: Import Required Libraries

import matplotlib.pyplot as plt
import numpy as np

Step 2: Prepare Your Data

For this example, let’s use a simple dataset representing sales of different products:

products = ['Laptops', 'Smartphones', 'Tablets', 'Accessories']
sales = [35, 30, 20, 15]

Step 3: Create the Donut Chart

# Create a figure and axis
fig, ax = plt.subplots()

# Create the donut chart
wedges, texts, autotexts = ax.pie(sales, labels=products, autopct='%1.1f%%', 
                                  startangle=90, pctdistance=0.85)

# Create the center circle
centre_circle = plt.Circle((0, 0), 0.70, fc='white')
fig.gca().add_artist(centre_circle)

# Equal aspect ratio ensures that pie is drawn as a circle
ax.axis('equal')  

plt.title("Product Sales Distribution")
plt.show()

Let’s break down this code:

  • fig, ax = plt.subplots() creates a new figure and axis.
  • ax.pie() creates the pie chart. The autopct parameter adds percentage labels, and startangle rotates the chart.
  • plt.Circle() creates the center circle, giving us the donut shape.
  • ax.axis('equal') ensures the chart is circular, not elliptical.

Customization Techniques

Now that we have a basic donut chart, let’s explore various customization options to make it more visually appealing and informative.

Color Schemes

You can customize the colors of your donut chart segments using Matplotlib’s color maps or by specifying a list of colors:

colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99']
plt.pie(sales, labels=products, colors=colors, autopct='%1.1f%%', startangle=90)

Label Formatting

Adjust the appearance of labels and percentage values:

wedges, texts, autotexts = plt.pie(sales, labels=products, autopct='%1.1f%%', 
                                   startangle=90, pctdistance=0.85)

for text in texts:
    text.set_fontsize(12)
for autotext in autotexts:
    autotext.set_fontsize(10)
    autotext.set_color('white')

Adding a Legend

For a cleaner look, you might prefer to use a legend instead of labels on the chart:

plt.pie(sales, autopct='%1.1f%%', startangle=90, pctdistance=0.85)
plt.legend(products, title="Products", loc="center left", bbox_to_anchor=(1, 0, 0.5, 1))

Center Text

Utilize the center space to display additional information:

total_sales = sum(sales)
plt.text(0, 0, f'Total\n${total_sales}K', ha='center', va='center', fontsize=20)

Advanced Features

Let’s explore some advanced features to create more complex and informative donut charts.

Multiple Rings

You can create a multi-ring donut chart to represent hierarchical data:

# Outer ring data
outer_sizes = [33, 52, 15]
outer_labels = ['Fruits', 'Vegetables', 'Meat']

# Inner ring data
inner_sizes = [15, 30, 45, 10]
inner_labels = ['Apples', 'Bananas', 'Oranges', 'Berries']

# Create the chart
fig, ax = plt.subplots()

# Outer ring
ax.pie(outer_sizes, labels=outer_labels, radius=1, wedgeprops=dict(width=0.3, edgecolor='white'))

# Inner ring
ax.pie(inner_sizes, labels=inner_labels, radius=0.7, wedgeprops=dict(width=0.3, edgecolor='white'))

plt.title("Food Categories and Subcategories")
plt.show()

Exploded Segments

Highlight specific segments by “exploding” them:

explode = (0.1, 0, 0, 0)  # only "explode" the first slice
plt.pie(sales, explode=explode, labels=products, autopct='%1.1f%%', startangle=90)

Data Integration

In real-world scenarios, you’ll often work with data from external sources. Let’s see how to integrate data from a pandas DataFrame into our donut chart.

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({
    'Category': ['A', 'B', 'C', 'D'],
    'Value': [25, 30, 15, 30]
})

# Create the donut chart
plt.pie(df['Value'], labels=df['Category'], autopct='%1.1f%%', startangle=90)
centre_circle = plt.Circle((0, 0), 0.70, fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
plt.title("Data from DataFrame")
plt.show()

Best Practices

When creating donut charts, keep these best practices in mind:

  • Limit categories: Use 5-7 categories maximum for clarity.
  • Color selection: Choose contrasting colors for easy differentiation.
  • Label placement: Ensure labels are readable and don’t overlap.
  • Data accuracy: Double-check that your data sums to 100% or your intended total.
  • Use sparingly: Don’t overuse donut charts; they’re best for showing composition, not comparisons over time.

Common Challenges and Solutions

Here are some common issues you might encounter when creating donut charts, along with their solutions:

Overlapping Labels

For charts with many small segments, labels may overlap. Solution: Use a legend instead of direct labeling, or adjust label positions programmatically.

Unreadable Small Segments

Very small segments can be hard to see or label. Solution: Group small segments into an “Other” category, or use a different chart type for data with many small values.

Color Differentiation

Similar colors can make segments hard to distinguish. Solution: Use a color palette generator or built-in color maps to ensure contrasting colors.

Example Projects

Let’s look at a real-world example of using a donut chart to visualize budget allocation:

import matplotlib.pyplot as plt

# Budget allocation data
categories = ['Housing', 'Food', 'Transportation', 'Entertainment', 'Savings']
percentages = [35, 25, 15, 10, 15]
colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99', '#ff99cc']

# Create donut chart
fig, ax = plt.subplots()
wedges, texts, autotexts = ax.pie(percentages, labels=categories, colors=colors, 
                                  autopct='%1.1f%%', startangle=90, pctdistance=0.85)

# Create center circle
centre_circle = plt.Circle((0, 0), 0.70, fc='white')
fig.gca().add_artist(centre_circle)

# Equal aspect ratio
ax.axis('equal')  

plt.title("Monthly Budget Allocation")

# Add total in center
total = sum(percentages)
plt.text(0, 0, f'Total\n{total}%', ha='center', va='center', fontsize=20)

plt.show()

Troubleshooting Guide

If you encounter issues while creating your donut charts, here are some common problems and their solutions:

  • Chart not displaying: Ensure you’ve called plt.show() at the end of your script.
  • Elliptical shape: Add ax.axis('equal') to ensure a circular shape.
  • Missing labels: Check that your labels list matches the length of your data list.
  • Performance issues: For large datasets, consider aggregating data before plotting.

Alternative Approaches

While Matplotlib is excellent for creating donut charts, other libraries offer alternative approaches:

  • Plotly: Offers interactive donut charts with hover information.
  • Seaborn: Built on Matplotlib, it provides a high-level interface for statistical graphics.
  • Bokeh: Creates interactive visualizations for modern web browsers.

Consider these alternatives when you need specific features like interactivity or when working with particular data structures.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button