Show Sidebar Hide Sidebar
Plotly.js

Mapbox Map Layers in JavaScript

How to make a mapbox maps in JavaScript with various base layers with or without needing a Mapbox access token.

If your figure contains one or more traces of type Scattermapbox, Choroplethmapbox or Densitymapbox, the layout object in your figure contains configuration information for the map itself. The map is composed of various layers, of three different types.

  1. layout.mapbox.style defines the lowest layers, also known as your "base map"
  2. The various traces in data are by default rendered above the base map (although this can be controlled via the below attribute).
  3. layout.mapbox.layers is an array that defines more layers that are by default rendered above the traces in data (although this can also be controlled via the below attribute).

The word "mapbox" in the trace names and layout.mapbox refers to the Mapbox.js open-source library. If your basemap in layout.mapbox.style uses data from the Mapbox service, then you will need to register for a free account at https://mapbox.com/ and obtain a Mapbox Access token. This token should be provided either in mapboxAccessToken in setPlotConfig function, or as a variable that would be passed as an argument of newPlot. If your layout.mapbox.style does not use data from the Mapbox service, you do not need to register for a Mapbox account.

Base Maps in layout.mapbox.style
The accepted values for layout.mapbox.style are one of the following tiles.
  1. "white-bg" yields an empty white canvas which results in no external HTTP requests
  2. "open-street-map", "carto-positron", "carto-darkmatter", "stamen-terrain", "stamen-toner" or "stamen-watercolor" yeild maps composed of raster tiles from various public tile servers which do not require signups or access tokens
  3. "basic", "streets", "outdoors", "light", "dark", "satellite", or "satellite-streets" yeild maps composed of vector tiles from the Mapbox service, and do require a Mapbox Access Token or an on-premise Mapbox installation.
  4. A Mapbox service style URL, which requires a Mapbox Access Token or an on-premise Mapbox installation.
  5. A Mapbox Style object as defined at https://docs.mapbox.com/mapbox-gl-js/style-spec/

Here is a simple map rendered with "open-street-map" tiles, without needing a Mapbox Access Token.

Plotly.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];
			});
		}

		var data = [
			{
				type: "scattermapbox",
				text: unpack(rows, "Globvalue"),
				lon: unpack(rows, "Lon"),
				lat: unpack(rows, "Lat"),
				marker: { color: "fuchsia", size: 4 }
			}
		];

		var layout = {
			dragmode: "zoom",
			mapbox: { style: "open-street-map", center: { lat: 38, lon: -90 }, zoom: 3 },
			margin: { r: 0, t: 0, b: 0, l: 0 }
		};

		Plotly.newPlot("gd", data, layout);
	}
);

If you have access to your own private tile servers, or wish to use a tile server not included in the list above, the recommended approach is to set layout.mapbox.style to "white-bg" and to use layout.mapbox.layers with below to specify a custom base map. If you omit the below attribute when using this approach, your data will likely be hidden by fully-opaque raster tiles!

Here is an example of a map which uses a public USGS imagery map, specified in layout.mapbox.layers, and which is rendered below the data layer.

Plotly.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];
			});
		}

var data = [
	{
		type: "scattermapbox",
		text: unpack(rows, "Globvalue"),
		lon: unpack(rows, "Lon"),
		lat: unpack(rows, "Lat"),
		marker: { color: "fuchsia", size: 4 }
	}
];

var layout = {
	dragmode: "zoom",
	mapbox: {
		style: "white-bg",
		layers: [
			{
				sourcetype: "raster",
				source: ["https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"],
				below: "traces"
			}
		],
		center: { lat: 38, lon: -90 },
		zoom: 3
	},
	margin: { r: 0, t: 0, b: 0, l: 0 }
};

Plotly.newPlot("gd", data, layout);
	}
);

Here is the same example, with in addition, a WMS layer from Environment Canada which displays near-real-time radar imagery in partly-transparent raster tiles, rendered above the go.Scattermapbox trace, as is the default.

Plotly.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]; });
		}

var data = [{
        type: 'scattermapbox', text: unpack(rows, 'Globvalue'),
        lon: unpack(rows, 'Lon'), lat: unpack(rows, 'Lat'),
        marker: {color: 'fuchsia', size: 4}
    }];

var layout = {
	dragmode: 'zoom',
	mapbox: {
		style: 'white-bg',
		layers: [
			{
            "below": 'traces',
            "sourcetype": "raster",
            "source": [
                "https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
            ]
        },
			{
             sourcetype: "raster",
			 source: ["https://geo.weather.gc.ca/geomet/?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&BBOX={bbox-epsg-3857}&CRS=EPSG:3857&WIDTH=1000&HEIGHT=1000&LAYERS=RADAR_1KM_RDBR&TILED=true&FORMAT=image/png"]}],
		below: 'traces',
		center: {lat: 38, lon: -90}, zoom: 4},
	margin: {r: 0, t: 0, b: 0, l: 0},
	showlegend: false};

Plotly.newPlot('gd', data, layout);
  });

Here is a map rendered with the "dark" style from the Mapbox service, which requires an Access Token.

var url = "https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson";

Plotly.d3.json(url, (err, raw) => {
  var lon = raw.features.map(f => f.geometry.coordinates[0]);
  var lat = raw.features.map(f => f.geometry.coordinates[1]);
  var z = raw.features.map(f => f.properties.mag);

  var data = [
    { type: "scattermapbox", lon: lon, lat: lat, z: z, hoverinfo: "y" }
  ];

  var layout = {
    mapbox: { style: "dark", zoom: 2, center: { lon: -150, lat: 60 } },
    margin: { t: 0, b: 0 }
  };

  var config = {
    mapboxAccessToken:
      "pk.eyJ1IjoiZXRwaW5hcmQiLCJhIjoiY2luMHIzdHE0MGFxNXVubTRxczZ2YmUxaCJ9.hwWZful0U2CQxit4ItNsiQ"
  };

  Plotly.newPlot("graph", data, layout, config);
});

plotly.js runs on all SVG-compatible browsers