density_plot = function (data, options=({})) {
if (!('kde_bandwidth' in options)) options.kde_bandwidth = 40;
if (!('width' in options)) options.width = width;
if (!('height' in options)) options.height = 200;
if (!('margin' in options)) options.margin = {top: 20, right: 30, bottom: 30, left: 40};
const svg = d3.select(DOM.svg(options.width, options.height));
const y = d3.scaleLinear()
.domain(d3.extent(data, d => d.y)).nice()
.rangeRound([options.height - options.margin.bottom, options.margin.top]);
const x = d3.scaleLinear()
.domain(d3.extent(data, d => d.x)).nice()
.rangeRound([options.margin.left, options.width - options.margin.right]);
const xAxis = g => g.append("g")
.attr("transform", `translate(0,${options.height - options.margin.bottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text")
.select(function() { return this.parentNode.appendChild(this.cloneNode()); })
.attr("y", -3)
.attr("dy", null)
.attr("font-weight", "bold")
.text(data.x));
const yAxis = g => g.append("g")
.attr("transform", `translate(${options.margin.left},0)`)
.call(d3.axisLeft(y).tickSizeOuter(0))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text")
.select(function() { return this.parentNode.appendChild(this.cloneNode()); })
.attr("x", 3)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y));
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
const contours = d3.contourDensity()
.x(d => x(d.x))
.y(d => y(d.y))
.size([options.width, options.height])
.bandwidth(options.kde_bandwidth)
(data);
svg.append("g")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-linejoin", "round")
.selectAll("path")
.data(contours)
.enter().append("path")
.attr("d", d3.geoPath());
svg.append("g")
.attr("stroke", "white")
.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("cx", d => x(d.x))
.attr("cy", d => y(d.y))
.attr("r", 2);
return svg.node();
}