Vega itself provides circle packing via the pack transform. You can find an example plot here.
I cannot find the pack transformation in the altair documentation. Is it somehow otherwise exposed or can I easily add it? I suppose, I can always modify the JSON Vega spec but that might be a bit brittle.
Altair is built on top of Vega-Lite, not directly on Vega. Therefore, the available transforms in Altair are those specified in Vega-Lite. Having that said, you could run Vega specs directly in Altair like this:
from altair.vega import Vega
Vega({
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "An example of a circle packing layout for hierarchical data.",
"width": 600,
"height": 600,
"padding": 5,
"autosize": "none",
"data": [
{
"name": "tree",
"url": "https://raw.githubusercontent.com/vega/vega/main/docs/data/flare.json",
"transform": [
{
"type": "stratify",
"key": "id",
"parentKey": "parent"
},
{
"type": "pack",
"field": "size",
"sort": {"field": "value"},
"size": [{"signal": "width"}, {"signal": "height"}]
}
]
}
],
"scales": [
{
"name": "color",
"type": "ordinal",
"domain": {"data": "tree", "field": "depth"},
"range": {"scheme": "category20"}
}
],
"marks": [
{
"type": "symbol",
"from": {"data": "tree"},
"encode": {
"enter": {
"shape": {"value": "circle"},
"fill": {"scale": "color", "field": "depth"},
"tooltip": {"signal": "datum.name + (datum.size ? ', ' + datum.size + ' bytes' : '')"}
},
"update": {
"x": {"field": "x"},
"y": {"field": "y"},
"size": {"signal": "4 * datum.r * datum.r"},
"stroke": {"value": "white"},
"strokeWidth": {"value": 0.5}
},
"hover": {
"stroke": {"value": "red"},
"strokeWidth": {"value": 2}
}
}
}
]
})
Note that the Vega wrapper in Altair might be deprecated in a future version of the package since it is now possible to use IPython to display Vega and Vega-Lite specs:
import json
from IPython.display import display
vg_spec = r"""{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "An example of a circle packing layout for hierarchical data.",
"width": 600,
"height": 600,
"padding": 5,
"autosize": "none",
"data": [
{
"name": "tree",
"url": "https://raw.githubusercontent.com/vega/vega/main/docs/data/flare.json",
"transform": [
{
"type": "stratify",
"key": "id",
"parentKey": "parent"
},
{
"type": "pack",
"field": "size",
"sort": {"field": "value"},
"size": [{"signal": "width"}, {"signal": "height"}]
}
]
}
],
"scales": [
{
"name": "color",
"type": "ordinal",
"domain": {"data": "tree", "field": "depth"},
"range": {"scheme": "category20"}
}
],
"marks": [
{
"type": "symbol",
"from": {"data": "tree"},
"encode": {
"enter": {
"shape": {"value": "circle"},
"fill": {"scale": "color", "field": "depth"},
"tooltip": {"signal": "datum.name + (datum.size ? ', ' + datum.size + ' bytes' : '')"}
},
"update": {
"x": {"field": "x"},
"y": {"field": "y"},
"size": {"signal": "4 * datum.r * datum.r"},
"stroke": {"value": "white"},
"strokeWidth": {"value": 0.5}
},
"hover": {
"stroke": {"value": "red"},
"strokeWidth": {"value": 2}
}
}
}
]
}
"""
display({"application/vnd.vega.v5+json": json.loads(vg_spec)}, raw=True)