Public
Edited
Mar 13, 2023
Insert cell
Insert cell
kde = require('fast-kde')
Insert cell
data = [
{ x: 90015000, y: 20010000, val: 33 },
{ x: 90010000, y: 20015000, val: 15 },
{ x: 90020000, y: 20011000, val: 26 },
{ x: 90015000, y: 20008000, val: 4 },
{ x: 90020000, y: 20012000, val: 28 },
{ x: 90020000, y: 20015000, val: 5 },
{ x: 90020000, y: 20010000, val: 18 }
]
Insert cell
resolution = 1000
Insert cell
center = [90015000, 20010000]
Insert cell
ps = 70
Insert cell
nbX = 600
Insert cell
nbY = 200
Insert cell
extent = [
[center[0] - (nbX / 2) * ps, center[0] + (nbX / 2) * ps],
[center[1] - (nbY / 2) * ps, center[1] + (nbY / 2) * ps]
]
Insert cell
kde_ = kde.density2d(data, {
x: (c) => c.x,
y: (c) => c.y,
weight: (c) => c.val,
bins: [nbX, nbY],
bandwidth: bw,
extent: extent
})
Insert cell
gdata_ = {
const th = 1e-6;
const out = [];

const g = kde_.grid();
for (let i = 0; i < g.length; i++) {
const v = g[i];
//if (v < th) continue;
out.push({ x: extent[0][0], y: extent[1][0], ksmval: v });
}

return out;
}
Insert cell
gdata = {
const th = 1e-6;
const out = [];

const pts = kde_.points("x", "y", "ksmval");

for (let p of pts) {
if (p.ksmval < th) continue;
out.push(p);
}

/*for (let v of g) {
if (v < th) continue;
out.push({ val: v });
}*/
return out;
}
Insert cell
Insert cell
kde_.heatmap()
Insert cell
/*Plot.plot({
r: { type: "sqrt", range: [0, 10] },
marks: [
Plot.dot(kde_, {
x: "x",
y: "y",
r: "z",
fill: "#ccc"
}),
Plot.dot(data, { x: (d) => d.x, y: (d) => d.y, fill: "black" })
],
width: 300,
height: 300
})*/
Insert cell
Type Markdown, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more