CARVIEW |
Select Language
HTTP/2 200
accept-ranges: bytes
access-control-allow-origin: *
age: 0
cache-control: max-age=600
content-encoding: gzip
content-type: text/html; charset=utf-8
date: Thu, 31 Jul 2025 10:14:49 GMT
etag: W/"687e6266-89d4"
expires: Thu, 31 Jul 2025 10:24:49 GMT
last-modified: Mon, 21 Jul 2025 15:53:10 GMT
server: Vercel
strict-transport-security: max-age=63072000
vary: Accept-Encoding
via: 1.1 varnish
x-cache: MISS
x-cache-hits: 0
x-fastly-request-id: 5b775949c71eed014b6fa4d2aa710335af00fbf9
x-github-request-id: 2BBC:FAB07:71F02:87841:688B4218
x-origin-cache: HIT
x-proxy-cache: MISS
x-robots-tag: index
x-served-by: cache-bom-vanm7210058-BOM
x-timer: S1753956889.005846,VS0,VE212
x-vercel-cache: MISS
x-vercel-id: bom1::tvkpc-1753956888995-acfa17e48000
content-length: 8998
2d density plots in JavaScript
2d Density Plots in JavaScript
How to make a D3.js-based 2d density plot in JavaScript. Examples of density plots with kernel density estimations, custom color-scales, and smoothing.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.
// from https://bl.ocks.org/mbostock/4349187
// Sample from a normal distribution with mean 0, stddev 1.
function normal() {
var x = 0,
y = 0,
rds, c;
do {
x = Math.random() * 2 - 1;
y = Math.random() * 2 - 1;
rds = x * x + y * y;
} while (rds == 0 || rds > 1);
c = Math.sqrt(-2 * Math.log(rds) / rds); // Box-Muller transform
return x * c; // throw away extra sample y * c
}
var N = 2000,
a = -1,
b = 1.2;
var step = (b - a) / (N - 1);
var t = new Array(N), x = new Array(N), y = new Array(N);
for(var i = 0; i < N; i++){
t[i] = a + step * i;
x[i] = (Math.pow(t[i], 3)) + (0.3 * normal() );
y[i] = (Math.pow(t[i], 6)) + (0.3 * normal() );
}
var trace1 = {
x: x,
y: y,
mode: 'markers',
name: 'points',
marker: {
color: 'rgb(102,0,0)',
size: 2,
opacity: 0.4
},
type: 'scatter'
};
var trace2 = {
x: x,
y: y,
name: 'density',
ncontours: 20,
colorscale: 'Hot',
reversescale: true,
showscale: false,
type: 'histogram2dcontour'
};
var trace3 = {
x: x,
name: 'x density',
marker: {color: 'rgb(102,0,0)'},
yaxis: 'y2',
type: 'histogram'
};
var trace4 = {
y: y,
name: 'y density',
marker: {color: 'rgb(102,0,0)'},
xaxis: 'x2',
type: 'histogram'
};
var data = [trace1, trace2, trace3, trace4];
var layout = {
showlegend: false,
autosize: false,
width: 600,
height: 550,
margin: {t: 50},
hovermode: 'closest',
bargap: 0,
xaxis: {
domain: [0, 0.85],
showgrid: false,
zeroline: false
},
yaxis: {
domain: [0, 0.85],
showgrid: false,
zeroline: false
},
xaxis2: {
domain: [0.85, 1],
showgrid: false,
zeroline: false
},
yaxis2: {
domain: [0.85, 1],
showgrid: false,
zeroline: false
}
};
Plotly.newPlot('myDiv', data, layout);
Add slider controls to 2d-density-plot plots with the <a href="https://github.com/plotly/postMessage-API" target="_blank">postMessage API</a>.
See the <a href="https://jsfiddle.net/plotlygraphs/y9sdy76h/4/" target="_blank">code on JSFiddle</a>.
Watch <a href="https://raw.githubusercontent.com/plotly/documentation/gh-pages/all_static/images/flight_conflicts.gif" target="_blank">the 5 second video</a> of how it works.
