I am going to walk you through an exciting data visualization project in Python creating an Enhanced Donut Chart using matplotlib
. This upgraded version includes several practical features, making it more dynamic and visually appealing.
Why Donut Charts
Donut charts are a variation of pie charts, where the center is removed to make the visualization more engaging. They are commonly used to represent categorical data in a visually appealing way. With enhancements like custom colors, percentage labels, and exploding slices, the chart becomes even more insightful!
Features of This Enhanced Donut Chart
Dynamic Input: Users can easily modify the data.
Percentage Labels: Each segment displays its percentage value.
Exploding Slices: The largest category is highlighted.
Custom Colors: Uses an aesthetic color palette.
Improved Layout & Legend: Better positioning and readability.
Creating a Donut Chart in Python
Install Matplotlib
First, ensure you have matplotlib
installed in your environment. If not, install it using:
pip install matplotlib
Write the Python Code
Below is the full Python script to generate the Enhanced Donut Chart:
import matplotlib.pyplot as plt
# Define data dynamically (Can be modified easily)
labels = ['Category A', 'Category B', 'Category C', 'Category D']
sizes = [30, 20, 35, 15] # Sizes representing different categories
colors = ['#FF5733', '#3366FF', '#33CC33', '#FFD700'] # Custom color palette
# Find the largest segment and explode it slightly for emphasis
explode = [0.05 if size == max(sizes) else 0 for size in sizes]
# Create Donut Chart
fig, ax = plt.subplots(figsize=(7, 7))
wedges, texts, autotexts = ax.pie(
sizes,
labels=labels,
colors=colors,
autopct='%1.1f%%', # Display percentage values
wedgeprops={'width': 0.4}, # Donut thickness
textprops={'fontsize': 12}, # Font size
explode=explode, # Highlight the largest category
startangle=140 # Rotate the chart for better visualization
)
# Add a central white circle to create the donut effect
centre_circle = plt.Circle((0, 0), 0.65, color='white', fc='white', linewidth=1.25)
ax.add_artist(centre_circle)
# Custom Legend
plt.legend(wedges, labels, title="Categories", loc="upper right", bbox_to_anchor=(1.2, 1))
# Title and display
plt.title('Enhanced Donut Chart in Python', fontsize=14, fontweight='bold')
plt.show()
Understanding the Code
Let’s break down the important sections of this script:
Data & Labels
labels = ['Category A', 'Category B', 'Category C', 'Category D']
sizes = [30, 20, 35, 15]
colors = ['#FF5733', '#3366FF', '#33CC33', '#FFD700']
Here, we define the labels, corresponding sizes, and custom colors for each category.
Highlighting the Largest Slice
explode = [0.05 if size == max(sizes) else 0 for size in sizes]
This logic dynamically finds the largest slice and slightly separates it for emphasis.
Creating the Donut Chart
wedges, texts, autotexts = ax.pie(
sizes,
labels=labels,
colors=colors,
autopct='%1.1f%%',
wedgeprops={'width': 0.4},
textprops={'fontsize': 12},
explode=explode,
startangle=140
)
autopct='%1.1f%%'
displays the percentage value of each category.wedgeprops={'width': 0.4}
controls the thickness of the donut.explode=explode
highlights the largest slice.startangle=140
rotates the chart for a better visual appeal.
Adding the Donut Effect
centre_circle = plt.Circle((0, 0), 0.65, color='white', fc='white', linewidth=1.25)
ax.add_artist(centre_circle)
A white circle is added to the center to convert the pie chart into a donut chart.
Custom Legend & Display
plt.legend(wedges, labels, title="Categories", loc="upper right", bbox_to_anchor=(1.2, 1))
plt.title('Enhanced Donut Chart in Python', fontsize=14, fontweight='bold')
plt.show()
The legend is positioned neatly, and a title is added to complete the visualization.
Final Output
After running the code, you’ll get a colorful, well-structured Donut Chart with percentage values and a highlighted largest segment.
Key Takeaways
Better Data Representation: Donut charts are an excellent way to display categorical proportions.
Customization: You can easily modify the colors, labels, or sizes as per your dataset.
Highlighting the Key Data: The script dynamically highlights the largest slice to draw attention.
Professional Visualization: Perfect for business reports, dashboards, and presentations.
Final Thoughts
Creating an Enhanced Donut Chart in Python is a great way to visualize categorical data effectively. With the ability to customize colors, labels, and segment highlights, this visualization is perfect for business reports, presentations, and dashboards.
By implementing percentage labels, exploding slices, and a refined layout, we have transformed a simple pie chart into a more professional and insightful donut chart.
If you’d like to take this further, consider adding interactive input, dataset integration, or even multiple donut charts for comparison. Python’s matplotlib
library offers endless possibilities to refine and enhance your data visualization skills.