SoFunction
Updated on 2024-10-30

python Pygal library to generate SVG (Scalable Vector Graphics) charts example

Today I'm sharing a super useful python library - Pygal

/Kozea/pygal 

What is Pygal?

Pygal is a Python library for generating SVG (Scalable Vector Graphics) charts. It is known for its simplicity and speed, which makes it ideal for situations where you need to create charts quickly.Pygal supports a wide range of chart types, such as bar charts, line charts, pie charts, etc., and is easy to extend and customize.

specificities

  • easy-to-use: Pygal is designed to be simple and quick to get started.

  • Customizability: Provides rich customization options to generate personalized charts.

  • High quality output: The resulting SVG charts are clear and beautiful.

Installing Pygal

To get started with Pygal, first make sure that Python is installed on your computer. then, install the Pygal library via the pip command:

pip install pygal

usage example

Example 1: Creating a Bar Chart

Bar charts are a common way to present categorized data. Here are the steps to create a bar chart using Pygal:

import pygal

# Create bar graph objects
bar_chart = ()

# Add data
bar_chart.add('Data 1', [1, 3, 5, 7, 9])
bar_chart.add('Data 2', [2, 4, 6, 8, 10])

# Save the chart
bar_chart.render_to_file('bar_chart.svg')

This code creates a bar chart with two sets of data and saves it as an SVG file.

Example 2: Making a Pie Chart

Pie charts are an effective tool for representing the relationship between parts and the whole. Below is an example of creating a pie chart using Pygal:

# Create pie chart objects
pie_chart = ()

# Add data
pie_chart.add('Python', 50)
pie_chart.add('Java', 30)
pie_chart.add('C++', 20)

# Save the chart
pie_chart.render_to_file('pie_chart.svg')

This code generates a simple pie chart showing the relative share of the three programming languages and saves it as an SVG file.

caveat

Here are a few things to keep in mind when using Pygal:

  • Understanding Chart Types: Pygal supports a wide range of chart types, and choosing the right chart type is critical to presenting your data.

  • data format: The format of the input data may vary depending on the type of chart.

  • Customization Options: Pygal offers a wealth of customization options such as colors, labels, styles, and more.

concluding remarks

Pygal is a powerful tool for Python data visualization with its simplicity and intuitive nature. Whether you're new to data analysis or an experienced developer, Pygal can help you communicate the story behind your data quickly and effectively.

Above is the python Pygal library to generate SVG (Scalable Vector Graphics) charts example of the details, more information about python Pygal to generate SVG charts please pay attention to my other related articles!