Converting from Vega#
Use the Vega functions for charts authored in Vega or already compiled to Vega from Vega-Lite. VlConvert parses and evaluates the specification directly, so the Vega-Lite theme and vl_version options do not apply.
Available outputs: SVG, PNG, JPEG, PDF, HTML, a Vega Editor URL, and an evaluated scenegraph.
Save this Vega specification:
chart.vg.json
{
"$schema": "https://vega.github.io/schema/vega/v6.json",
"width": 300,
"height": 180,
"padding": 5,
"data": [
{
"name": "table",
"values": [
{"category": "A", "value": 28},
{"category": "B", "value": 55},
{"category": "C", "value": 43},
{"category": "D", "value": 91},
{"category": "E", "value": 81}
]
}
],
"scales": [
{
"name": "xscale",
"type": "band",
"domain": {"data": "table", "field": "category"},
"range": "width",
"padding": 0.1
},
{
"name": "yscale",
"domain": {"data": "table", "field": "value"},
"nice": true,
"range": "height"
}
],
"axes": [
{"orient": "bottom", "scale": "xscale"},
{"orient": "left", "scale": "yscale"}
],
"marks": [
{
"type": "rect",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"scale": "xscale", "field": "category"},
"width": {"scale": "xscale", "band": 1},
"y": {"scale": "yscale", "field": "value"},
"y2": {"scale": "yscale", "value": 0}
},
"update": {"fill": {"value": "#4c78a8"}}
}
}
]
}
from pathlib import Path
import vl_convert as vlc
vg_spec = Path("chart.vg.json").read_text(encoding="utf-8")
svg = vlc.vega_to_svg(vg_spec)
png = vlc.vega_to_png(vg_spec, scale=2)
Path("chart.svg").write_text(svg, encoding="utf-8")
Path("chart.png").write_bytes(png)
Vega functions also accept the specification as a dictionary. SVG and HTML functions return text. PNG, JPEG, and PDF functions return bytes.
The SVG conversion renders this chart:
Size, locale, and other per-conversion options work the same way as for Vega-Lite. See Image Size and Quality Recommendations, Number and Time Locales, and Conversion Overrides.