Skip to content

38 plotly

image image image

Uncomment the following line to install leafmap if needed.

1
# !pip install leafmap
1
import leafmap.plotlymap as leafmap

If you run into an error saying "FigureWidget - 'mapbox._derived' Value Error" (source), uncomment the following line and run it.

1
# leafmap.fix_widget_error()

Create an interactive map using default settings.

1
2
m = leafmap.Map()
m

Change default setting when creating a map.

Can be one of string from "open-street-map", "carto-positron", "carto-darkmatter", "stamen-terrain", "stamen-toner" or "stamen-watercolor" .

1
2
m = leafmap.Map(center=(40, -100), zoom=3, basemap="stamen-terrain", height=500)
m

Set map center and zoom level.

1
2
3
m = leafmap.Map(basemap="stamen-watercolor")
m.set_center(lat=20, lon=0, zoom=2)
m

Print out available basemaps.

1
# leafmap.basemaps.keys()

Add a basemap.

1
2
3
m = leafmap.Map()
m.add_basemap("OpenTopoMap")
m

Add XYZ tile layer.

1
2
3
4
m = leafmap.Map()
tile_url = "https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}"
m.add_tile_layer(tile_url, name="Google Satellite", attribution="Google", opacity=1.0)
m

Add a mapbox tile layer. You will need a mapbox token. The map style can be Can be "basic", "streets", "outdoors", "light", "dark", "satellite", or "satellite-streets".

1
import os
1
# os.environ["MAPBOX_TOKEN"] = "your-mapbox-token"
1
2
3
m = leafmap.Map()
m.add_mapbox_layer(style="streets")
m

Remove the modebar in the upper-right corner.

1
2
m = leafmap.Map(basemap="stamen-toner")
m
1
m.clear()

Add more buttons to the modebar.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
m = leafmap.Map(basemap="carto-positron")
controls = [
    "drawline",
    "drawopenpath",
    "drawclosedpath",
    "drawcircle",
    "drawrect",
    "eraseshape",
]
m.add_controls(controls)
m

Add Cloud Optimized GeoTIFF.

1
2
3
4
m = leafmap.Map()
url = "https://github.com/opengeos/data/releases/download/raster/Libya-2023-07-01.tif"
m.add_cog_layer(url, name="Fire (pre-event)")
m

Add a STAC item via HTTP URL.

1
2
3
4
m = leafmap.Map()
url = "https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json"
m.add_stac_layer(url, bands=["B3", "B2", "B1"], name="False color")
m

Add a STAC item from Microsoft Planetary Computer.

1
2
collection = "landsat-8-c2-l2"
item = "LC08_L2SP_047027_20201204_02_T1"
1
2
3
4
5
6
7
8
m = leafmap.Map()
m.add_stac_layer(
    collection=collection,
    item=item,
    bands=["SR_B7", "SR_B5", "SR_B4"],
    titiler_endpoint="pc",
)
m

Add a heat map.

1
url = "https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv"
1
2
3
4
5
m = leafmap.Map(basemap="stamen-terrain")
m.add_heatmap(
    url, latitude="Latitude", longitude="Longitude", z="Magnitude", name="Earthquake"
)
m

Add a choropleth map.

1
url = "https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/countries.geojson"
1
2
3
m = leafmap.Map(basemap="stamen-terrain")
m.add_choropleth_map(url, name="Pop", z="POP_EST", colorscale="Viridis")
m