-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to create histogram with fixed bin width #46
Comments
Regarding the first or last bin not being the same width as the others: that is because given n thresholds, n + 1 bins will be produced. The first bin ( If you prefer, you can consider the effective width of the first and last bin as infinite, since they are bounded only by the input data (or more precisely the histogram’s domain) and not the thresholds. The If you want to force the domain to coincide exactly with the tick interval, you can use d3.range to manually create the ticks as you discuss in option 3. If you also want the tick thresholds to be “human-readable” (per the design of d3.ticks) you can nice your domain before computing the ticks, and then use scale.ticks: var data = [1, 2, 3, 4, 4.7];
var count = 5;
var x = d3.scaleLinear().domain(d3.extent(data)).nice(count);
var histogram = d3.histogram().domain(x.domain()).thresholds(x.ticks(count));
var bins = histogram(data);
console.log("bin widths: " + bins.map(b => b.x1 - b.x0)); You could also use d3.tickStep to nice your histogram’s domain manually, but that’s exactly what linear.nice does. |
Thank you for the detailed answer – I wish you a happy new year 🎉 🍾 🎆 |
Fixes #1082 For background, see the following: * d3/d3-array#46 * https://stackoverflow.com/questions/15880058/d3-js-ticks-function-giving-more-elements-than-needed
Fixes #1082 For background, see the following: * d3/d3-array#46 * https://stackoverflow.com/questions/15880058/d3-js-ticks-function-giving-more-elements-than-needed
* Specify histogram thresholds in fixed-length array Fixes #1082 For background, see the following: * d3/d3-array#46 * https://stackoverflow.com/questions/15880058/d3-js-ticks-function-giving-more-elements-than-needed * Oops, off by one!
I am trying to create a histogram with a specific number of bins which should all have the same width (i.e. the domain should be uniformly divided):
I started by using
x.ticks()
(Option 1):The last bin is narrower than all other bins (to be expected based on how
ticks
works).For
histogram.thresholds([count])
, the docs state that:This is not what I observe (Option 2):
Q: What's the proper invocation of
histogram.thresholds([count])
?I currently use a manual array of thresholds (Option 3):
This works, but seems overly complex for such a simple use case...
The text was updated successfully, but these errors were encountered: