Quick Start#
This example renders a small Vega-Lite bar chart with inline data as chart.png. See Loading Data and Images for configuration options when a chart loads data from a URL or file.
Save this specification:
chart.vl.json
{
"$schema": "https://vega.github.io/schema/vega-lite/v6.json",
"data": {
"values": [
{"category": "A", "value": 28},
{"category": "B", "value": 55},
{"category": "C", "value": 43},
{"category": "D", "value": 91},
{"category": "E", "value": 81}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "category", "type": "nominal"},
"y": {"field": "value", "type": "quantitative"}
}
}
vegalite_to_png() takes a Vega-Lite specification as a dictionary or JSON string and returns PNG bytes:
from pathlib import Path
import vl_convert as vlc
spec = Path("chart.vl.json").read_text(encoding="utf-8")
png = vlc.vegalite_to_png(spec, scale=2)
Path("chart.png").write_bytes(png)
scale=2 doubles the pixel dimensions of the output. See Converting from Vega-Lite for the other output formats.
The specification renders as this chart: