Search code examples
vega-lite

trim unused outer parts of the axis in vega-lite v3


How can I "trim" the axis and get rid of that padding on both sides beyond [-101, 225] specifically in Vega-Lite v3 ?


Solution

  • You can use scale.domain along with scale.nice=false. This works both in Vega-Lite v3 and v4. For example:

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v3.4.0.json",
      "data": {
        "values": [
          {"label": "A", "value": -101},
          {"label": "B", "value": -50},
          {"label": "C", "value": 10},
          {"label": "D", "value": 116},
          {"label": "E", "value": 225}
        ]
      },
      "mark": "bar",
      "encoding": {
        "color": {"field": "label", "type": "nominal"},
        "x": {
          "field": "value",
          "scale": {"domain": [-101, 225], "nice": false},
          "type": "quantitative"
        },
        "y": {"field": "label", "type": "nominal"}
      }
    }
    

    enter image description here