Skip to content

Instantly share code, notes, and snippets.

Forked from jfirebaugh/faithful.js
Last active April 29, 2024 17:15
Show Gist options
  • Save mbostock/4341954 to your computer and use it in GitHub Desktop.
Save mbostock/4341954 to your computer and use it in GitHub Desktop.
Kernel Density Estimation
license: gpl-3.0

Kernel density estimation is a method of estimating the probability distribution of a random variable based on a random sample. In contrast to a histogram, kernel density estimation produces a smooth estimate. The smoothness can be tuned via the kernel’s bandwidth parameter. With the correct choice of bandwidth, important features of the distribution can be seen, while an incorrect choice results in undersmoothing or oversmoothing and obscured features.

This example shows a histogram and a kernel density estimation for times between eruptions of Old Faithful Geyser in Yellowstone National Park, taken from R’s faithful dataset. The data follow a bimodal distribution; short eruptions are followed by a wait time averaging about 55 minutes, and long eruptions by a wait time averaging about 80 minutes. In recent years, wait times have been increasing, possibly due to the effects of earthquakes on the geyser’s geohydrology.

This example is based on a Protovis version by John Firebaugh. See also a two-dimensional density estimation of this dataset using d3-contour.

<!DOCTYPE html>
.axis--y .domain {
display: none;
<svg width="960" height="500"></svg>
<script src=""></script>
var svg ="svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
margin = {top: 20, right: 30, bottom: 30, left: 40};
var x = d3.scaleLinear()
.domain([30, 110])
.range([margin.left, width - margin.right]);
var y = d3.scaleLinear()
.domain([0, 0.1])
.range([height - margin.bottom,]);
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + (height - margin.bottom) + ")")
.attr("x", width - margin.right)
.attr("y", -6)
.attr("fill", "#000")
.attr("text-anchor", "end")
.attr("font-weight", "bold")
.text("Time between eruptions (min.)");
.attr("class", "axis axis--y")
.attr("transform", "translate(" + margin.left + ",0)")
.call(d3.axisLeft(y).ticks(null, "%"));
d3.json("faithful.json", function(error, faithful) {
if (error) throw error;
var n = faithful.length,
bins = d3.histogram().domain(x.domain()).thresholds(40)(faithful),
density = kernelDensityEstimator(kernelEpanechnikov(7), x.ticks(40))(faithful);
svg.insert("g", "*")
.attr("fill", "#bbb")
.attr("x", function(d) { return x(d.x0) + 1; })
.attr("y", function(d) { return y(d.length / n); })
.attr("width", function(d) { return x(d.x1) - x(d.x0) - 1; })
.attr("height", function(d) { return y(0) - y(d.length / n); });
.attr("fill", "none")
.attr("stroke", "#000")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("d", d3.line()
.x(function(d) { return x(d[0]); })
.y(function(d) { return y(d[1]); }));
function kernelDensityEstimator(kernel, X) {
return function(V) {
return {
return [x, d3.mean(V, function(v) { return kernel(x - v); })];
function kernelEpanechnikov(k) {
return function(v) {
return Math.abs(v /= k) <= 1 ? 0.75 * (1 - v * v) / k : 0;
Copy link

xtitter commented Dec 6, 2016

In index.html line 118, the "/ scale" in the true condition should be removed, I believe.

Copy link

I am new to d3,js. I am not able to understand this width = +svg.attr("width") in line 14 and 15 of index.html. I can only think of it as a resizing measure but unable to understand how this works.

Copy link

blindmonkey commented Nov 28, 2018

@Abhilash-Chandran svg is referring to the <svg width="960" height="500"></svg> element and .attr gets the specified attribute on the element, so svg.attr("width") gets the width attribute that was specified on the element (in this case 960). It looks like .attr will either always return a string or just may return a string in some instances, so the + unary operator is used to coerce the value to a number. If you try this yourself in a console, you should see something like the following:

> +'960'

So to summarize, it's getting the width and the height of the HTML element and ensuring they're numeric.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment