As a proof-of-principle, let’s recreate the very first plot we made in the VegaLite tutorial: the simple barchart.
Loading the necessary libraries
First things first: loading the necessary libraries. Make sure that these are installed first (see the previous step).
from vega import Vega
At this point we won’t need panel
and/or param
yet.
Creating the specification
We’ll just put the whole specification in a variable that we call spec
.
spec = {
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "A simple bar chart with embedded data.",
"data": {
"values": [
{"a": "A", "b": 28},
{"a": "B", "b": 55},
{"a": "C", "b": 43},
{"a": "D", "b": 91}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "b", "type": "quantitative"},
"y": {"field": "a", "type": "nominal"}
}
}
Drawing the visualisation
And finally we call the Vega()
function, passing it the spec
as an argument.
Vega(spec)
This will give you the actual plot.