How to set an *exact* number of of axis ticks in Vega-Lite/Altair?

Viewed 653

The tickCount axis parameter allows to set an approximate number of ticks, as per the docs:

A desired number of ticks, for axes visualizing quantitative scales. The resulting number may be different so that values are "nice" (multiples of 2, 5, 10) and lie within the underlying scale's range.

Here is an example where changing the parameter to 3 only gives 2 axis ticks.

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "A scatterplot showing horsepower and miles per gallons for various cars.",
  "data": {"url": "data/cars.json"},
  "mark": "point",
  "encoding": {
    "x": {"field": "Horsepower", "type": "quantitative", "axis": {"tickCount": 3}},
    "y": {"field": "Miles_per_Gallon", "type": "quantitative"}
  }
}

Open the Chart in the Vega Editor

While I understand that it is nice to have a default that is rounded to a convenient well-behaved value, I think the expectation and desired outcome when starting to modify from this default, is often to be able to set an exact tick number. Is there any way to override the "nice" rounding behavior and set an exact number of ticks?

1 Answers

I saw on vega-lite issue tracker that it is possible to use values to explicitly set the ticks:

"axis": {"values": [0, 80, 160, 240]}

This would solve my pretty much all my use cases, but leaving the question open in case there is a way to achieve this by specifying the exact number of ticks rather than the exact values of the ticks.

Related