Pygal
Pygal is an open-source Python library for creating dynamic and interactive SVG (Scalar Vector Graphics) charts. It is known for its ease of use, allowing developers to generate high-quality, scalable visualizations with minimal code. Pygal is currently at version 3.1.0 and maintains an active development and release cadence, with recent updates focusing on fixes and minor enhancements.
Warnings
- gotcha When embedding Pygal SVGs directly into HTML, custom fonts defined in styles might not apply correctly. The browser might not fetch external stylesheets linked within the SVG if it's not embedded via an `<embed>` or `<iframe>` tag.
- gotcha Charts rendered in environments like Jupyter Notebooks might display raw SVG/XML metadata instead of the visual chart. This is because the notebook environment might not automatically interpret raw SVG strings as renderable content.
- breaking Pygal 3.0.0 removed the `Iterable` import from `collections` (which was deprecated in Python 3.8 and removed in Python 3.10) and changed internal dependencies like `pytest` attributes and `pkg_resources` usage. Code relying on older Python versions (pre-3.8) or specific `pytest` internals might encounter issues.
- gotcha Rendering PNG output requires the `cairosvg` library. If `cairosvg` is not installed, calls to `render_to_png()` will raise an error.
Install
-
pip install pygal -
pip install "pygal[all]"
Imports
- Bar
import pygal chart = pygal.Bar()
- Style
from pygal.style import Style
Quickstart
import pygal
# Create a Bar chart object
bar_chart = pygal.Bar()
# Add data series
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
bar_chart.add('Prime', [2, 3, 5, 7, 11, 13, 17, 19, 23, 29])
# Render the chart to an SVG file
bar_chart.render_to_file('bar_chart.svg')
# To display in a browser (requires lxml installed)
# bar_chart.render_in_browser()
print("Chart rendered to bar_chart.svg")