Tile Layers#

Navigate to https://docs.mapbox.com/help/how-mapbox-works/access-tokens/ to set up a free Mapbox account and obtain an access token. When needed, the token can be set using the px.set_mapbox_access_token() configuration function.

Public Tile Servers#

There are a few options for base map layers using layout.mapbox.style.

Raster tiles from public tile servers:

  • open-street-map

  • carto-positron

  • carto-darkmatter

  • stamen-terrain

  • stamen-toner

  • stamen-watercolor

import pandas as pd, plotly.express as px # import statement
parks = pd.read_csv("parks.csv") # load data
# create figure
fig = px.scatter_mapbox(parks, lat='Latitude', lon='Longitude', size_max=15, zoom=10,
                        mapbox_style="carto-positron")

fig # show figure

Tiles from a URL#

We could change the value assigned to mapbox_style to change the base map layer style.

In a situation where we are loading a base map layer from a URL, we would set mapbox_style to white-bg to create a blank canvas for the external base map layer style.

us_cities = pd.read_csv("https://raw.githubusercontent.com/kwaldenphd/elements-of-computing-book-data/ch12/us-cities-top-1k.csv") # load data

# create figure
fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"],
                        color_discrete_sequence=["chartreuse"], zoom=3, height=300)

# update figure layout
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}) # update figure margin
fig # show figure

Mapbox Access Token Tiles#

Vector tiles from Mapbox service (require access token):

  • basic

  • streets

  • outdoors

  • light

  • dark

  • satellite

  • satellite-streets

We can also specify the base map layer using a Mapbox service style URL. These styles require an access token.

px.set_mapbox_access_token("YOUR ACCESS TOKEN GOES HERE") # set mapbox access token

# create figure
fig = px.scatter_mapbox(parks, lat='Latitude', lon='Longitude', size_max=15, zoom=10,
                        mapbox_style="dark")

fig # show figure