observablehq / plot

A concise API for exploratory data visualization implementing a layered grammar of graphics
https://observablehq.com/plot/
ISC License
4.3k stars 175 forks source link

For a log scale, the tickFormat function should only be considered for ticks that should be visible #2077

Closed mbostock closed 3 months ago

mbostock commented 4 months ago

Log scales are a bit unusual in that all the ticks are drawn independent of the ticks option; instead, the ticks option only controls which ticks are labeled.

untitled - 2024-06-09T164644 913

Plot.plot({x: {type: "log", domain: [1, 4200], ticks: 7}})

untitled - 2024-06-09T164647 801

Plot.plot({x: {type: "log", domain: [1, 4200], ticks: 10}})

untitled - 2024-06-09T164649 511

Plot.plot({x: {type: "log", domain: [1, 4200], ticks: 20}})

This means that if you set the tickFormat option with a log scale, it (for the most part) ignores the ticks option. These three specifications produce equivalent output:

untitled - 2024-06-09T164820 342

Plot.plot({x: {type: "log", domain: [1, 4200], ticks: 7, tickFormat: String}})

untitled - 2024-06-09T164820 342

Plot.plot({x: {type: "log", domain: [1, 4200], ticks: 10, tickFormat: String}})

untitled - 2024-06-09T164820 342

Plot.plot({x: {type: "log", domain: [1, 4200], ticks: 20, tickFormat: String}})

It‘d be cleaner if the ticks option filtered the tick labels, composing with the given tickFormat, rather than baking the filtering into the tick format. That way you could override the tick format without affecting the number of displayed ticks.