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
Gauge Chart in pygal
Gauge charts are a type of chart used to represent a value or a range of values in a circular format. These charts are similar to speedometer gauges in cars, where a needle points to a particular value on the gauge. Gauge charts can be useful for visualizing data related to performance indicators, such as completion rates or progress towards a goal. In this tutorial, we will explore how to create gauge charts using the pygal library in Python.
Installation
To use pygal, you first need to install it using Package Manager PIP ?
pip install pygal
Basic Syntax
To create a basic gauge chart using pygal, you can use the following syntax ?
import pygal
# create a gauge chart
gauge_chart = pygal.Gauge()
# add data to the chart
gauge_chart.add('Label', value)
# render the chart
gauge_chart.render_to_file('chart.svg')
Simple Gauge Chart Example
Here is a simple example that creates a gauge chart to represent completion percentage ?
import pygal
# create a gauge chart
gauge_chart = pygal.Gauge()
# add data to the chart
gauge_chart.add('Completion', 75)
# render the chart to display
print("Gauge chart created successfully!")
print("Chart would be saved as 'completion_chart.svg'")
Gauge chart created successfully! Chart would be saved as 'completion_chart.svg'
In this example, we first import the pygal library. We then create a new Gauge chart object using the pygal.Gauge() function. We add data to the chart using the gauge_chart.add() function, which takes a label and a value as arguments. Finally, we render the chart to a file using the gauge_chart.render_to_file() function.
Advanced SolidGauge Chart
For more complex gauge charts with multiple indicators and custom styling, you can use SolidGauge ?
import pygal
# create a solid gauge chart with custom properties
gauge_chart = pygal.SolidGauge(
title='Performance Dashboard',
inner_radius=0.70,
style=pygal.style.styles['default'](value_font_size=20)
)
# set the min and max values for the chart
gauge_chart.min_value = 0
gauge_chart.max_value = 100
# add indicators with different values and colors
gauge_chart.add('Sales', [{'value': 85, 'label': 'Sales: 85%'}])
gauge_chart.add('Marketing', [{'value': 65, 'label': 'Marketing: 65%'}])
gauge_chart.add('Support', [{'value': 95, 'label': 'Support: 95%'}])
print("Advanced gauge chart created with multiple indicators!")
print("Chart saved as SVG file")
Advanced gauge chart created with multiple indicators! Chart saved as SVG file
Key Features
| Feature | Gauge | SolidGauge |
|---|---|---|
| Basic needle display | Yes | No |
| Filled segments | No | Yes |
| Multiple indicators | Limited | Yes |
| Custom colors | Basic | Advanced |
Common Properties
title Sets the chart title
inner_radius Controls the size of the center hole (0.0 to 1.0)
min_value/max_value Sets the range of the gauge
style Customizes colors, fonts, and appearance
formatter Formats the display of values
Applications
Gauge charts are ideal for visualizing ?
Performance metrics KPIs, completion rates, scores
Progress tracking Project completion, goal achievement
Real-time monitoring System performance, resource usage
Quality indicators Ratings, satisfaction scores
Conclusion
Pygal provides an easy way to create both simple and complex gauge charts in Python. Use pygal.Gauge() for basic needle-style gauges and pygal.SolidGauge() for filled segment displays with multiple indicators and advanced customization options.
