简体   繁体   中英

How to create a circle packing plot with altair?

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}
        }
      }
    }
  ]
})

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM