|
| 1 | +--- |
| 2 | +jupyter: |
| 3 | + jupytext: |
| 4 | + notebook_metadata_filter: all |
| 5 | + text_representation: |
| 6 | + extension: .md |
| 7 | + format_name: markdown |
| 8 | + format_version: '1.1' |
| 9 | + jupytext_version: 1.1.1 |
| 10 | + kernelspec: |
| 11 | + display_name: Python 3 |
| 12 | + language: python |
| 13 | + name: python3 |
| 14 | + language_info: |
| 15 | + codemirror_mode: |
| 16 | + name: ipython |
| 17 | + version: 3 |
| 18 | + file_extension: .py |
| 19 | + mimetype: text/x-python |
| 20 | + name: python |
| 21 | + nbconvert_exporter: python |
| 22 | + pygments_lexer: ipython3 |
| 23 | + version: 3.6.8 |
| 24 | + plotly: |
| 25 | + description: How to make Mapbox maps in Python with various base layers, with |
| 26 | + or without needing a Mapbox Access token. |
| 27 | + display_as: maps |
| 28 | + has_thumbnail: true |
| 29 | + ipynb: ~notebook_demo/261 |
| 30 | + language: python |
| 31 | + layout: user-guide |
| 32 | + name: Mapbox Map Layers |
| 33 | + order: 7 |
| 34 | + page_type: example_index |
| 35 | + permalink: python/mapbox-layers/ |
| 36 | + thumbnail: thumbnail/scatter-mapbox.jpg |
| 37 | + title: Mapbox Map Layers in Python | Plotly |
| 38 | +--- |
| 39 | + |
| 40 | +<!-- #region --> |
| 41 | +#### How Layers Work in Mapbox Maps |
| 42 | + |
| 43 | +If your figure contains one or more traces of type `go.Scattermapbox`, `go.Choroplethmapbox` or `go.Densitymapbox`, the `layout.mapbox` object in your figure contains configuration information for the map itself. The map is composed of various layers, of three different types: |
| 44 | + |
| 45 | + 1. `layout.mapbox.style` defines is the lowest layers, also known as your "base map" |
| 46 | + 2. The various traces in `data` are by default rendered above the base map (although this can be controlled via the `below` attribute). |
| 47 | + 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). |
| 48 | + |
| 49 | +#### Mapbox Access Tokens and When You Need Them |
| 50 | + |
| 51 | +The word "mapbox" in the trace names and `layout.mapbox` refers to the Mapbox.js open-source library, which is integrated into Plotly.py. 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 in `layout.mapbox.access_token` (or, if using Plotly Express, via the `px.set_mapbox_access_token()` configuration function). |
| 52 | + |
| 53 | +> If your `layout.mapbox.style` does not use data from the Mapbox service, you do *not* need to register for a Mapbox account. |
| 54 | +
|
| 55 | +#### Base Maps in `layout.mapbox.style` |
| 56 | + |
| 57 | +The accepted values for `layout.mapbox.style` are one of: |
| 58 | + |
| 59 | +* `"white-bg"` yields an empty white canvas which results in no external HTTP requests |
| 60 | +* `"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 |
| 61 | +* `"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. |
| 62 | +* A Mapbox service style URL, which requires a Mapbox Access Token or an on-premise Mapbox installation. |
| 63 | +* A Mapbox Style object as defined at https://docs.mapbox.com/mapbox-gl-js/style-spec/ |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +#### OpenStreetMap tiles: no token needed |
| 68 | +Here is a simple map rendered with OpenStreetMaps tiles, without needing a Mapbox Access Token: |
| 69 | +<!-- #endregion --> |
| 70 | + |
| 71 | +```python |
| 72 | +import pandas as pd |
| 73 | +us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv") |
| 74 | + |
| 75 | +import plotly.express as px |
| 76 | + |
| 77 | +fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"], |
| 78 | + color_discrete_sequence=["fuchsia"], zoom=3, height=300) |
| 79 | +fig.update_layout(mapbox_style="open-street-map") |
| 80 | +fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) |
| 81 | +fig.show() |
| 82 | +``` |
| 83 | + |
| 84 | +<!-- #region --> |
| 85 | +#### Dark tiles from Mapbox service: free token needed |
| 86 | + |
| 87 | + |
| 88 | +Here is the same map rendered with the `"dark"` style from the Mapbox service, which requires an Access Token: |
| 89 | +<!-- #endregion --> |
| 90 | + |
| 91 | +```python |
| 92 | +token = open(".mapbox_token").read() # you will need your own token |
| 93 | + |
| 94 | +import pandas as pd |
| 95 | +us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv") |
| 96 | + |
| 97 | +import plotly.express as px |
| 98 | + |
| 99 | +fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"], |
| 100 | + color_discrete_sequence=["fuchsia"], zoom=3, height=300) |
| 101 | +fig.update_layout(mapbox_style="dark", mapbox_accesstoken=token) |
| 102 | +fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) |
| 103 | +fig.show() |
| 104 | +``` |
| 105 | + |
| 106 | +<!-- #region --> |
| 107 | +#### Using `layout.mapbox.layers` to Specify a Base Map |
| 108 | + |
| 109 | +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. |
| 110 | + |
| 111 | + |
| 112 | +> If you omit the `below` attribute when using this approach, your data will likely be hidden by fully-opaque raster tiles! |
| 113 | +
|
| 114 | +#### Base Tiles from the USGS: no token needed |
| 115 | + |
| 116 | +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. |
| 117 | +<!-- #endregion --> |
| 118 | + |
| 119 | +```python |
| 120 | +import pandas as pd |
| 121 | +us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv") |
| 122 | + |
| 123 | +import plotly.express as px |
| 124 | + |
| 125 | +fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"], |
| 126 | + color_discrete_sequence=["fuchsia"], zoom=3) |
| 127 | +fig.update_layout( |
| 128 | + mapbox_style="white-bg", |
| 129 | + mapbox_layers=[ |
| 130 | + { |
| 131 | + "below": 'traces', |
| 132 | + "sourcetype": "raster", |
| 133 | + "source": [ |
| 134 | + "https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}" |
| 135 | + ] |
| 136 | + } |
| 137 | + ]) |
| 138 | +fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) |
| 139 | +fig.show() |
| 140 | +``` |
| 141 | + |
| 142 | +<!-- #region --> |
| 143 | +#### Base Tiles from the USGS, radar overlay from Environment Canada: no token needed |
| 144 | + |
| 145 | + |
| 146 | +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: |
| 147 | +<!-- #endregion --> |
| 148 | + |
| 149 | +```python |
| 150 | +import pandas as pd |
| 151 | +us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv") |
| 152 | + |
| 153 | +import plotly.express as px |
| 154 | + |
| 155 | +fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"], |
| 156 | + color_discrete_sequence=["fuchsia"], zoom=3) |
| 157 | +fig.update_layout( |
| 158 | + mapbox_style="white-bg", |
| 159 | + mapbox_layers=[ |
| 160 | + { |
| 161 | + "below": 'traces', |
| 162 | + "sourcetype": "raster", |
| 163 | + "source": [ |
| 164 | + "https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}" |
| 165 | + ] |
| 166 | + }, |
| 167 | + { |
| 168 | + "sourcetype": "raster", |
| 169 | + "source": ["https://geo.weather.gc.ca/geomet/?" |
| 170 | + "SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&BBOX={bbox-epsg-3857}&CRS=EPSG:3857" |
| 171 | + "&WIDTH=1000&HEIGHT=1000&LAYERS=RADAR_1KM_RDBR&TILED=true&FORMAT=image/png"], |
| 172 | + } |
| 173 | + ]) |
| 174 | +fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) |
| 175 | +fig.show() |
| 176 | +``` |
| 177 | + |
| 178 | +#### Reference |
| 179 | +See https://plot.ly/python/reference/#layout-mapbox for more information and options! |
0 commit comments