How Pygal Renders Charts and What Output Formats Are Available
Updated
Pygal renders charts as SVG (Scalable Vector Graphics). You build a chart object, add data, then call render() to get the SVG markup as a string, or render_to_file() to write it to disk. This makes Pygal a good fit when you want resolution-independent, text-based chart output for the web or print rather than a raster image like PNG.
The rendering model
Pygal separates what you chart from how it is serialized:
- Instantiate a chart type — for example
pygal.Bar(). - Add data — pass values to the chart, optionally with a title and labels.
- Render — call a render method to produce the output.
The documentation's own example shows the minimal path:
pygal.Bar()(1, 3, 3, 7)(1, 6, 6, 4).render()
Here the chart is created, two data series are added by calling the chart object, and render() returns the SVG. The chained style works because the chart instance is callable for adding series.
Output formats
| Method | What it returns / does | Typical use |
|---|---|---|
render() |
The chart as an SVG string | Embedding in HTML, templates, or further processing |
render_to_file() |
Writes the SVG to a file path you specify | Saving a standalone .svg file |
Both operate on the same chart object, so switching between "give me the markup" and "write it out" is a one-line change.
Why SVG is the default
SVG is vector-based, so the same chart stays sharp at any size — it scales cleanly in a browser, in a PDF, or in print. Because it is XML text, you can also style it with CSS, inspect it in a text editor, and diff it in version control. That is a different trade-off from raster formats, which are fixed-resolution and larger for equivalent quality.
Other formats
The core documented output is SVG. If you need a raster format such as PNG, that is not part of the basic render() path described in the documentation — treat any additional format as something to confirm against the project's current docs and extensions rather than assuming it is built in.
What to check before you commit
- Do you need vectors? If the chart goes into a web page, a report, or print, SVG is usually the right answer and Pygal gives it to you directly.
- Do you need a bitmap? If a downstream system only accepts PNG/JPEG, verify the conversion path first; you may need to rasterize the SVG yourself.
- Where does the output go? Use
render()when another layer (a template engine, an API response) will handle the string; userender_to_file()when you just want a file on disk.
For installation and first-run setup, see the project's Documentation and Installing pages; for the full set of chart types and options, the API Index is the reference to work from.