Problem with log scale axis and ticks

2,828 views
Skip to first unread message

Dirk Bergstrom

unread,
Feb 16, 2012, 12:14:26 PM2/16/12
to d3-js
I'm getting very strange results when I plot a log axis for very large
ranges. I ask for ten ticks, and for some domains I get six or seven
equally spaced ticks, for others I get a dozen or so ticks in closely
spaced groups of two, and for some domains I get no ticks at all.
After fighting with this for half an hour I bumped into the ticks()
method on d3.scale, and I thought "hey, I'll use this to build my own
axis". I did some quick experiments and it was clear that method
wouldn't help, and was probably at the root of the axis tick problem.

Here's a demonstration:

--> s1=d3.scale.log().domain([1,1000000000]).range([1,10])
--> s1.ticks(5).length
81
--> s1.ticks(5)
[1, 2, 3, 4, 5, 6, 7, <many values elided> 600000000, 700000000,
800000000, 900000000]

--> s100=d3.scale.log().domain([100,1000000000]).range([1,10])
--> s100.ticks(5).length
63
--> s100.ticks(5)
[100, 200, 300, 400, 500, <etc....> 700000000, 800000000, 900000000]

If I ask for five ticks, and get 81 something is definitely wrong,
either with my understanding or in the code.

Mike Bostock

unread,
Feb 16, 2012, 12:20:20 PM2/16/12
to d3...@googlegroups.com
Log scales don't have a customizable number of ticks:

https://github.com/mbostock/d3/wiki/Quantitative-Scales#wiki-log_ticks

If you were to drop ticks from log scales, you wouldn't be able to see
the log distortion. So, instead you can drop the labels from the ticks
rather than the ticks themselves, using the tickFormat method.

If you use the d3.svg.axis component, the axis.ticks method sets the
arguments that are passed to both the scale.ticks method and the
scale.tickFormat method, so you should get the desired behavior
automatically.

Mike

Jason Davies

unread,
Feb 16, 2012, 12:22:25 PM2/16/12
to d3...@googlegroups.com
On Thu, Feb 16, 2012 at 09:14:26AM -0800, Dirk Bergstrom wrote:
> If I ask for five ticks, and get 81 something is definitely wrong,
> either with my understanding or in the code.

The number of ticks is not configurable for log scales, see:

https://github.com/mbostock/d3/issues/72

However, you can filter the ticks using the scale's tickFormat, which
takes an optional "count" argument to limit the number of labels. This
is explained in the issue above, and also in the API documentation:

https://github.com/mbostock/d3/wiki/Quantitative-Scales#wiki-log_ticks

--
Jason Davies, http://www.jasondavies.com/

Leslie Bondaryk

unread,
Mar 1, 2013, 11:00:35 AM3/1/13
to d3...@googlegroups.com
Note that the example given for this in the doc is unrelated to the log case and uses ticks and not tickFormat and talks about currency (log currency?), and seems not to work for me. Maybe I just have an  old version of d3? In what version was count and the optional format introduced to scale.log().tickFormat()?  Please do update the doc.  I would also be interested to know if someone has a good way of showing the expression 10<sup>exp</sup> on the ticks, rather than the value - I really just want to label the decades.  Would also be good to have a specific way to always end on a full decade.

Thanks for any advice,
Leslie
Reply all
Reply to author
Forward
0 new messages