Scatter Plots on Maps in JavaScript

How to make D3.js-based scatter plots on maps in JavaScript. Scatter plots on maps highlight geographic areas and can be colored by value.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Try Plotly Studio now.

var data = [{
    type: 'scattergeo',
    mode: 'markers+text',
    text: [
        'Montreal', 'Toronto', 'Vancouver', 'Calgary', 'Edmonton',
        'Ottawa', 'Halifax', 'Victoria', 'Winnepeg', 'Regina'
    ],
    lon: [
        -73.57, -79.24, -123.06, -114.1, -113.28,
        -75.43, -63.57, -123.21, -97.13, -104.6
    ],
    lat: [
        45.5, 43.4, 49.13, 51.1, 53.34, 45.24,
        44.64, 48.25, 49.89, 50.45
    ],
    marker: {
        size: 7,
        color: [
            '#bebada', '#fdb462', '#fb8072', '#d9d9d9', '#bc80bd',
            '#b3de69', '#8dd3c7', '#80b1d3', '#fccde5', '#ffffb3'
        ],
        line: {
            width: 1
        }
    },
    name: 'Canadian cities',
    textposition: [
        'top right', 'top left', 'top center', 'bottom right', 'top right',
        'top left', 'bottom right', 'bottom left', 'top right', 'top right'
    ],
}];

var layout = {
    title: {
        text: 'Canadian cities',
        font: {
            family: 'Droid Serif, serif',
            size: 16
        }
    },
    geo: {
        scope: 'north america',
        resolution: 50,
        lonaxis: {
            'range': [-130, -55]
        },
        lataxis: {
            'range': [40, 70]
        },
        showrivers: true,
        rivercolor: '#fff',
        showlakes: true,
        lakecolor: '#fff',
        showland: true,
        landcolor: '#EAEAAE',
        countrycolor: '#d3d3d3',
        countrywidth: 1.5,
        subunitcolor: '#d3d3d3'
    }
};

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":"var data = [{\n type: 'scattergeo',\n mode: 'markers+text',\n text: [\n 'Montreal', 'Toronto', 'Vancouver', 'Calgary', 'Edmonton',\n 'Ottawa', 'Halifax', 'Victoria', 'Winnepeg', 'Regina'\n ],\n lon: [\n -73.57, -79.24, -123.06, -114.1, -113.28,\n -75.43, -63.57, -123.21, -97.13, -104.6\n ],\n lat: [\n 45.5, 43.4, 49.13, 51.1, 53.34, 45.24,\n 44.64, 48.25, 49.89, 50.45\n ],\n marker: {\n size: 7,\n color: [\n '#bebada', '#fdb462', '#fb8072', '#d9d9d9', '#bc80bd',\n '#b3de69', '#8dd3c7', '#80b1d3', '#fccde5', '#ffffb3'\n ],\n line: {\n width: 1\n }\n },\n name: 'Canadian cities',\n textposition: [\n 'top right', 'top left', 'top center', 'bottom right', 'top right',\n 'top left', 'bottom right', 'bottom left', 'top right', 'top right'\n ],\n}];\n\nvar layout = {\n title: {\n text: 'Canadian cities',\n font: {\n family: 'Droid Serif, serif',\n size: 16\n }\n },\n geo: {\n scope: 'north america',\n resolution: 50,\n lonaxis: {\n 'range': [-130, -55]\n },\n lataxis: {\n 'range': [40, 70]\n },\n showrivers: true,\n rivercolor: '#fff',\n showlakes: true,\n lakecolor: '#fff',\n showland: true,\n landcolor: '#EAEAAE',\n countrycolor: '#d3d3d3',\n countrywidth: 1.5,\n subunitcolor: '#d3d3d3'\n }\n};\n\nPlotly.newPlot('myDiv', data, layout);\n\n"}">
d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv', function(err, rows){

    function unpack(rows, key) {
        return rows.map(function(row) { return row[key]; });
    }

    var scl = [[0,'rgb(5, 10, 172)'],[0.35,'rgb(40, 60, 190)'],[0.5,'rgb(70, 100, 245)'], [0.6,'rgb(90, 120, 245)'],[0.7,'rgb(106, 137, 247)'],[1,'rgb(220, 220, 220)']];

    var data = [{
        type:'scattergeo',
        locationmode: 'USA-states',
        lon: unpack(rows, 'long'),
        lat: unpack(rows, 'lat'),
        hoverinfor:  unpack(rows, 'airport'),
        text:  unpack(rows, 'airport'),
        mode: 'markers',
        marker: {
            size: 8,
            opacity: 0.8,
            reversescale: true,
            autocolorscale: false,
            symbol: 'square',
            line: {
                width: 1,
                color: 'rgb(102,102,102)'
            },
            colorscale: scl,
            cmin: 0,
            color: unpack(rows, 'cnt'),
            colorbar: {
                title: {text: 'Incoming Flights February 2011'}
            }
        }
    }];


    var layout = {
        title: {text: 'Most Trafficked US airports'},
        colorbar: true,
        geo: {
            scope: 'usa',
            projection: {
                type: 'albers usa'
            },
            showland: true,
            landcolor: 'rgb(250,250,250)',
            subunitcolor: 'rgb(217,217,217)',
            countrycolor: 'rgb(217,217,217)',
            countrywidth: 0.5,
            subunitwidth: 0.5
        }
    };

    Plotly.newPlot("myDiv", data, layout, {showLink: false});

});
</script>\n\t<script src="/?originalUrl=https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fd3%2F3.5.17%2Fd3.min.js"></script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv', function(err, rows){\n\n function unpack(rows, key) {\n return rows.map(function(row) { return row[key]; });\n }\n\n var scl = [[0,'rgb(5, 10, 172)'],[0.35,'rgb(40, 60, 190)'],[0.5,'rgb(70, 100, 245)'], [0.6,'rgb(90, 120, 245)'],[0.7,'rgb(106, 137, 247)'],[1,'rgb(220, 220, 220)']];\n\n var data = [{\n type:'scattergeo',\n locationmode: 'USA-states',\n lon: unpack(rows, 'long'),\n lat: unpack(rows, 'lat'),\n hoverinfor: unpack(rows, 'airport'),\n text: unpack(rows, 'airport'),\n mode: 'markers',\n marker: {\n size: 8,\n opacity: 0.8,\n reversescale: true,\n autocolorscale: false,\n symbol: 'square',\n line: {\n width: 1,\n color: 'rgb(102,102,102)'\n },\n colorscale: scl,\n cmin: 0,\n color: unpack(rows, 'cnt'),\n colorbar: {\n title: {text: 'Incoming Flights February 2011'}\n }\n }\n }];\n\n\n var layout = {\n title: {text: 'Most Trafficked US airports'},\n colorbar: true,\n geo: {\n scope: 'usa',\n projection: {\n type: 'albers usa'\n },\n showland: true,\n landcolor: 'rgb(250,250,250)',\n subunitcolor: 'rgb(217,217,217)',\n countrycolor: 'rgb(217,217,217)',\n countrywidth: 0.5,\n subunitwidth: 0.5\n }\n };\n\n Plotly.newPlot(\"myDiv\", data, layout, {showLink: false});\n\n});\n"}">
d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/2015_06_30_precipitation.csv', function(err, rows){
      function unpack(rows, key) {
          return rows.map(function(row) { return row[key]; });
      }

 scl = [[0, 'rgb(150,0,90)'],[0.125, 'rgb(0, 0, 200)'],[0.25,'rgb(0, 25, 255)'],[0.375,'rgb(0, 152, 255)'],[0.5,'rgb(44, 255, 150)'],[0.625,'rgb(151, 255, 0)'],[0.75,'rgb(255, 234, 0)'],[0.875,'rgb(255, 111, 0)'],[1,'rgb(255, 0, 0)']];

    var data = [{
        type: 'scattergeo',
        mode: 'markers',
        text: unpack(rows, 'Globvalue'),
        lon: unpack(rows, 'Lon'),
        lat: unpack(rows, 'Lat'),
        marker: {
          color: unpack(rows, 'Globvalue'),
          colorscale: scl,
          cmin: 0,
          cmax: 1.4,
          reversescale: true,
          opacity: 0.2,
          size: 2,
          colorbar:{
            thickness: 10,
            title: {side:
              'right'
            },
            outlinecolor: 'rgba(68,68,68,0)',
            ticks: 'outside',
            ticklen: 3,
            shoticksuffix: 'last',
            ticksuffix: 'inches',
            dtick: 0.1
          }
        },
        name: 'NA Precipitation'
    }];

    var layout = {
      geo:{
        scope: 'north america',
        showland: true,
        landcolor: 'rgb(212,212,212)',
        subunitcolor: 'rgb(255,255,255)',
        countrycolor: 'rgb(255,255,255)',
        showlakes: true,
        lakecolor: 'rgb(255,255,255)',
        showsubunits: true,
        showcountries: true,
        resolution: 50,
        projection: {
          type: 'conic conformal',
          rotation: {
            long: -100
          }
        },
      },
      longaxis: {
        showgrid: true,
        gridwidth: 0.5,
        range: [ -140.0, -55.0 ],
        dtick: 5
      },
      lataxis: {
        showgrid: true,
        gridwidth: 0.5,
        range: [ 20.0, 60.0 ],
        dtick: 5
      },
      title: {text: 'North America Precipitation'},
      width: 600,
      height: 600
    };

    Plotly.newPlot('myDiv', data, layout);
  });
</script>\n\t<script src="/?originalUrl=https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fd3%2F3.5.17%2Fd3.min.js"></script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/2015_06_30_precipitation.csv', function(err, rows){\n function unpack(rows, key) {\n return rows.map(function(row) { return row[key]; });\n }\n\n scl = [[0, 'rgb(150,0,90)'],[0.125, 'rgb(0, 0, 200)'],[0.25,'rgb(0, 25, 255)'],[0.375,'rgb(0, 152, 255)'],[0.5,'rgb(44, 255, 150)'],[0.625,'rgb(151, 255, 0)'],[0.75,'rgb(255, 234, 0)'],[0.875,'rgb(255, 111, 0)'],[1,'rgb(255, 0, 0)']];\n\n var data = [{\n type: 'scattergeo',\n mode: 'markers',\n text: unpack(rows, 'Globvalue'),\n lon: unpack(rows, 'Lon'),\n lat: unpack(rows, 'Lat'),\n marker: {\n color: unpack(rows, 'Globvalue'),\n colorscale: scl,\n cmin: 0,\n cmax: 1.4,\n reversescale: true,\n opacity: 0.2,\n size: 2,\n colorbar:{\n thickness: 10,\n title: {side:\n 'right'\n },\n outlinecolor: 'rgba(68,68,68,0)',\n ticks: 'outside',\n ticklen: 3,\n shoticksuffix: 'last',\n ticksuffix: 'inches',\n dtick: 0.1\n }\n },\n name: 'NA Precipitation'\n }];\n\n var layout = {\n geo:{\n scope: 'north america',\n showland: true,\n landcolor: 'rgb(212,212,212)',\n subunitcolor: 'rgb(255,255,255)',\n countrycolor: 'rgb(255,255,255)',\n showlakes: true,\n lakecolor: 'rgb(255,255,255)',\n showsubunits: true,\n showcountries: true,\n resolution: 50,\n projection: {\n type: 'conic conformal',\n rotation: {\n long: -100\n }\n },\n },\n longaxis: {\n showgrid: true,\n gridwidth: 0.5,\n range: [ -140.0, -55.0 ],\n dtick: 5\n },\n lataxis: {\n showgrid: true,\n gridwidth: 0.5,\n range: [ 20.0, 60.0 ],\n dtick: 5\n },\n title: {text: 'North America Precipitation'},\n width: 600,\n height: 600\n };\n\n Plotly.newPlot('myDiv', data, layout);\n });\n"}">