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
Pie chart in pygal
The Pygal library provides a powerful and intuitive way to create visually appealing pie charts in Python. Pie charts are a popular choice for displaying data distribution, and Pygal makes it easy to generate interactive SVG charts with customizable settings.
Whether we're visualizing sales figures, survey results, or any other categorical data, Pygal's pie charts offer an effective and visually appealing solution. This article explores the creation of pie charts using Pygal, showcasing its versatility and flexibility in presenting data insights.
How to Plot a Pie Chart in Pygal
Below are the steps to create a pie chart in Pygal ?
Import the necessary module ? Import the pygal module for creating interactive SVG charts.
Define the data ? Create a dictionary with category names as keys and values as quantities.
Create a pie chart object ? Create an instance of the
Pieclass.Set the title ? Set the chart title using the
titleattribute.Add data ? Use the
add()method to add each data point to the chart.Render the chart ? Call
render_to_file()to save the chart as an SVG file.
Example
Here's a simple example creating a fruit distribution pie chart ?
import pygal
# Data for the pie chart
data = {
'Apple': 30,
'Banana': 20,
'Orange': 15,
'Grape': 10,
'Mango': 25
}
# Create a pie chart
pie_chart = pygal.Pie()
pie_chart.title = 'Fruit Distribution'
# Add data to the pie chart
for fruit, count in data.items():
pie_chart.add(fruit, count)
# Render the chart to an SVG file
pie_chart.render_to_file('pie_chart.svg')
print("Pie chart created successfully!")
Pie chart created successfully!
Customize a Pygal Pie Chart
Pygal provides extensive customization options for pie charts. Here are the most commonly used attributes ?
Colors ? Customize slice colors using the
colorsparameter with a list of color codes.Inner Radius ? Create a donut chart by setting
inner_radiusto a value between 0 and 1.Half-pie Mode ? Use
half_pie=Trueto display a semi-circle chart.Legend ? Control legend visibility and position with
show_legendandlegend_at_*attributes.Explode ? Emphasize specific slices using the
explosionparameter.Fonts ? Customize title, label, and legend fonts with size and family attributes.
Example
Here's an example with multiple customizations applied ?
import pygal
# Data for the pie chart
data = {
'Red': 40,
'Blue': 25,
'Green': 20,
'Yellow': 15
}
# Create a customized pie chart
pie_chart = pygal.Pie(
inner_radius=0.4, # Creates donut effect
half_pie=True, # Semi-circle chart
show_legend=True,
legend_at_bottom=True,
title='Color Distribution',
title_font_size=24,
label_font_size=14,
colors=['#FF0000', '#0000FF', '#00FF00', '#FFFF00'],
background='#EEEEEE'
)
# Add data to the pie chart
for color, count in data.items():
pie_chart.add(color, count)
# Render the chart to an SVG file
pie_chart.render_to_file('customized_pie_chart.svg')
print("Customized pie chart created successfully!")
Customized pie chart created successfully!
Comparison of Customization Options
| Parameter | Purpose | Example Value |
|---|---|---|
inner_radius |
Creates donut chart | 0.4 (40% inner radius) |
half_pie |
Semi-circle display | True/False |
colors |
Custom slice colors | ['#FF0000', '#00FF00'] |
show_legend |
Display legend | True/False |
Conclusion
Pygal provides an excellent solution for creating interactive pie charts in Python with minimal code. Its extensive customization options allow you to create professional-looking charts suitable for presentations, reports, and web applications. The SVG output format ensures scalability and crisp display across different devices.
