jupyter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Plotly supports two different kinds of maps:
- Mapbox maps are tile-based maps. If your figure is created with a
px.scatter_mapbox
,px.line_mapbox
,px.choropleth_mapbox
orpx.density_mapbox
function or otherwise contains one or more traces of typego.Scattermapbox
,go.Choroplethmapbox
orgo.Densitymapbox
, thelayout.mapbox
object in your figure contains configuration information for the map itself. - Geo maps are outline-based maps. If your figure is created with a
px.scatter_geo
,px.line_geo
orpx.choropleth
function or otherwise contains one or more traces of typego.Scattergeo
orgo.Choropleth
, thelayout.geo
object in your figure contains configuration information for the map itself.
This page documents Mapbox tile-based maps, and the Geo map documentation describes how to configure outline-based maps.
Mapbox tile maps are composed of various layers, of three different types:
layout.mapbox.style
defines is the lowest layers, also known as your "base map"- The various traces in
data
are by default rendered above the base map (although this can be controlled via thebelow
attribute). layout.mapbox.layers
is an array that defines more layers that are by default rendered above the traces indata
(although this can also be controlled via thebelow
attribute).
The word "mapbox" in the trace names and layout.mapbox
refers to the Mapbox GL 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).
If you basemap in layout.mapbox.style
uses maps from the Stadia Maps service (see below for details), you'll need to register for a Stadia Maps account and token.
The accepted values for layout.mapbox.style
are one of:
"white-bg"
yields an empty white canvas which results in no external HTTP requests"open-street-map"
,"carto-positron"
, and"carto-darkmatter"
yield maps composed of raster tiles from various public tile servers which do not require signups or access tokens."basic"
,"streets"
,"outdoors"
,"light"
,"dark"
,"satellite"
, or"satellite-streets"
yield maps composed of vector tiles from the Mapbox service, and do require a Mapbox Access Token or an on-premise Mapbox installation."stamen-terrain"
,"stamen-toner"
or"stamen-watercolor"
yield maps composed of raster tiles from the Stadia Maps service, and require a Stadia Maps account and token.- A Mapbox service style URL, which requires a Mapbox Access Token or an on-premise Mapbox installation.
- A Mapbox Style object as defined at https://docs.mapbox.com/mapbox-gl-js/style-spec/
Here is a simple map rendered with OpenStreetMaps tiles, without needing a Mapbox Access Token:
import pandas as pd
us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")
import plotly.express as px
fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
color_discrete_sequence=["fuchsia"], zoom=3, height=300)
fig.update_layout(mapbox_style="open-street-map")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
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.
import pandas as pd
us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")
import plotly.express as px
fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
color_discrete_sequence=["fuchsia"], zoom=3, height=300)
fig.update_layout(
mapbox_style="white-bg",
mapbox_layers=[
{
"below": 'traces',
"sourcetype": "raster",
"sourceattribution": "United States Geological Survey",
"source": [
"https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
]
}
])
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
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:
import pandas as pd
us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")
import plotly.express as px
fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
color_discrete_sequence=["fuchsia"], zoom=3, height=300)
fig.update_layout(
mapbox_style="white-bg",
mapbox_layers=[
{
"below": 'traces',
"sourcetype": "raster",
"sourceattribution": "United States Geological Survey",
"source": [
"https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
]
},
{
"sourcetype": "raster",
"sourceattribution": "Government of Canada",
"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"],
}
])
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
Here is a map rendered with the "dark"
style from the Mapbox service, which requires an Access Token:
token = open(".mapbox_token").read() # you will need your own token
import pandas as pd
us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")
import plotly.express as px
fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
color_discrete_sequence=["fuchsia"], zoom=3, height=300)
fig.update_layout(mapbox_style="dark", mapbox_accesstoken=token)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
See the example in the plotly and datashader tutorial.
New in 5.11
Set bounds for a map to specify an area outside which a user interacting with the map can't pan or zoom. Here we set a maximum longitude of -180
, a minimum longitude of -50
, a maximum latitude of 90
, and a minimum latitude of 20
.
import plotly.express as px
import pandas as pd
us_cities = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv"
)
fig = px.scatter_mapbox(
us_cities,
lat="lat",
lon="lon",
hover_name="City",
hover_data=["State", "Population"],
color_discrete_sequence=["fuchsia"],
zoom=3,
height=300,
)
fig.update_layout(mapbox_style="open-street-map")
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.update_layout(mapbox_bounds={"west": -180, "east": -50, "south": 20, "north": 90})
fig.show()
See https://plotly.com/python/reference/layout/mapbox/ for more information and options!