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. Try Plotly Studio now.

// from http://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);
</script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"// from http://bl.ocks.org/mbostock/4349187\n// Sample from a normal distribution with mean 0, stddev 1.\n\nfunction normal() {\n var x = 0,\n y = 0,\n rds, c;\n do {\n x = Math.random() * 2 - 1;\n y = Math.random() * 2 - 1;\n rds = x * x + y * y;\n } while (rds == 0 || rds > 1);\n c = Math.sqrt(-2 * Math.log(rds) / rds); // Box-Muller transform\n return x * c; // throw away extra sample y * c\n}\n\nvar N = 2000,\n a = -1,\n b = 1.2;\n\nvar step = (b - a) / (N - 1);\nvar t = new Array(N), x = new Array(N), y = new Array(N);\n\nfor(var i = 0; i < N; i++){\n t[i] = a + step * i;\n x[i] = (Math.pow(t[i], 3)) + (0.3 * normal() );\n y[i] = (Math.pow(t[i], 6)) + (0.3 * normal() );\n}\n\nvar trace1 = {\n x: x,\n y: y,\n mode: 'markers',\n name: 'points',\n marker: {\n color: 'rgb(102,0,0)',\n size: 2,\n opacity: 0.4\n },\n type: 'scatter'\n};\nvar trace2 = {\n x: x,\n y: y,\n name: 'density',\n ncontours: 20,\n colorscale: 'Hot',\n reversescale: true,\n showscale: false,\n type: 'histogram2dcontour'\n};\nvar trace3 = {\n x: x,\n name: 'x density',\n marker: {color: 'rgb(102,0,0)'},\n yaxis: 'y2',\n type: 'histogram'\n};\nvar trace4 = {\n y: y,\n name: 'y density',\n marker: {color: 'rgb(102,0,0)'},\n xaxis: 'x2',\n type: 'histogram'\n};\nvar data = [trace1, trace2, trace3, trace4];\nvar layout = {\n showlegend: false,\n autosize: false,\n width: 600,\n height: 550,\n margin: {t: 50},\n hovermode: 'closest',\n bargap: 0,\n xaxis: {\n domain: [0, 0.85],\n showgrid: false,\n zeroline: false\n },\n yaxis: {\n domain: [0, 0.85],\n showgrid: false,\n zeroline: false\n },\n xaxis2: {\n domain: [0.85, 1],\n showgrid: false,\n zeroline: false\n },\n yaxis2: {\n domain: [0.85, 1],\n showgrid: false,\n zeroline: false\n }\n};\nPlotly.newPlot('myDiv', data, layout);\n"}">
Add slider controls to 2d-density-plot plots with the <a href="/?originalUrl=https%3A%2F%2Fplot.ly%2F%26quot%3Bhttps%3A%2F%2Fgithub.com%2Fplotly%2FpostMessage-API%26quot%3B%2520target%3D%26quot%3B_blank%26quot%3B%26gt%3BpostMessage%2520API%26lt%3B%2Fa%26gt%3B.See%2520the%2520%26lt%3Ba%2520href%3D%26quot%3Bhttps%3A%2F%2Fjsfiddle.net%2Fplotlygraphs%2Fy9sdy76h%2F4%2F%26quot%3B%2520target%3D%26quot%3B_blank%26quot%3B%26gt%3Bcode%2520on%2520JSFiddle%26lt%3B%2Fa%26gt%3B.Watch%2520%26lt%3Ba%2520href%3D%26quot%3Bhttps%3A%2F%2Fraw.githubusercontent.com%2Fplotly%2Fdocumentation%2Fgh-pages%2Fall_static%2Fimages%2Fflight_conflicts.gif%26quot%3B%2520target%3D%26quot%3B_blank%26quot%3B%26gt%3Bthe%25205%2520second%2520video%26lt%3B%2Fa%26gt%3B%2520of%2520how%2520it%2520works.%253C%2Fcode">