Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Donut Chart in Pygal
Pygal is a Python library used for creating interactive graphs and charts for data visualization. A donut chart is a variation of a pie chart with a hollow center, making it easier to read values and add additional information in the middle space.
Installing Pygal
Before creating donut charts, install the Pygal library using pip ?
pip install pygal
Basic Donut Chart
Create a simple donut chart using pygal.Pie() with the inner_radius parameter ?
import pygal
# Create a Donut chart
donut_chart = pygal.Pie(inner_radius=0.4)
# Add data to the Donut chart
donut_chart.add('Carbohydrates', 50)
donut_chart.add('Protein', 25)
donut_chart.add('Fat', 25)
# Set title
donut_chart.title = 'Macronutrient Distribution'
# Display the chart content
print("Chart created successfully with segments:")
print("- Carbohydrates: 50%")
print("- Protein: 25%")
print("- Fat: 25%")
Chart created successfully with segments: - Carbohydrates: 50% - Protein: 25% - Fat: 25%
The inner_radius=0.4 parameter creates the hollow center. Values range from 0 (pie chart) to 1 (complete hollow).
Customizing Colors and Labels
Add custom colors, titles, and formatting options ?
import pygal
# Create a customized Donut chart
donut_chart = pygal.Pie(inner_radius=0.4)
# Add data with custom colors
donut_chart.add('Carbohydrates', 50)
donut_chart.add('Protein', 25)
donut_chart.add('Fat', 25)
# Customize the chart
donut_chart.title = 'Daily Macronutrient Breakdown'
donut_chart.legend_at_bottom = True
donut_chart.legend_box_size = 16
donut_chart.print_values = True
donut_chart.value_font_size = 18
# Show configuration
print("Chart Configuration:")
print(f"Title: {donut_chart.title}")
print(f"Legend at bottom: {donut_chart.legend_at_bottom}")
print(f"Values displayed: {donut_chart.print_values}")
Chart Configuration: Title: Daily Macronutrient Breakdown Legend at bottom: True Values displayed: True
Advanced Styling with Custom Colors
Use the style parameter for advanced customization ?
import pygal
# Create a Donut chart with custom style
custom_style = pygal.style.Style(
colors=['#FF6B6B', '#4ECDC4', '#45B7D1'],
font_family='Arial',
value_font_size=20,
label_font_size=16
)
donut_chart = pygal.Pie(inner_radius=0.5, style=custom_style)
# Add data
donut_chart.add('Mobile', 45)
donut_chart.add('Desktop', 35)
donut_chart.add('Tablet', 20)
donut_chart.title = 'Website Traffic by Device'
# Display style information
print("Custom Style Applied:")
print("Colors: Red, Teal, Blue")
print("Font: Arial")
print("Inner radius: 0.5 (larger hole)")
Custom Style Applied: Colors: Red, Teal, Blue Font: Arial Inner radius: 0.5 (larger hole)
Key Configuration Options
| Parameter | Purpose | Example Value |
|---|---|---|
inner_radius |
Size of center hole | 0.4 (40% hollow) |
print_values |
Show values on chart | True/False |
legend_at_bottom |
Legend position | True/False |
value_font_size |
Font size for values | 18 |
Multiple Data Series
Create donut charts with multiple data points per category ?
import pygal
# Create chart with multiple series
donut_chart = pygal.Pie(inner_radius=0.3)
# Add multiple data points per category
donut_chart.add('Q1 Sales', [100, 120, 110])
donut_chart.add('Q2 Sales', [150, 140, 160])
donut_chart.add('Q3 Sales', [120, 130, 125])
donut_chart.title = 'Quarterly Sales Data'
print("Multi-series donut chart created")
print("Each category contains 3 data points")
print("Useful for comparing trends across categories")
Multi-series donut chart created Each category contains 3 data points Useful for comparing trends across categories
Conclusion
Pygal's donut charts provide an elegant way to visualize proportional data with a clean, professional appearance. The hollow center distinguishes them from pie charts and can accommodate additional information or simply create a more modern look.
