Skip to content

Latest commit

 

History

History
119 lines (92 loc) · 5 KB

mapbox-density-heatmaps.md

File metadata and controls

119 lines (92 loc) · 5 KB
jupyter
jupytext kernelspec language_info plotly
notebook_metadata_filter text_representation
all
extension format_name format_version jupytext_version
.md
markdown
1.3
1.14.6
display_name language name
Python 3 (ipykernel)
python
python3
codemirror_mode file_extension mimetype name nbconvert_exporter pygments_lexer version
name version
ipython
3
.py
text/x-python
python
python
ipython3
3.10.11
description display_as language layout name order page_type permalink thumbnail
How to make a Mapbox Density Heatmap in Python with Plotly.
maps
python
base
Mapbox Density Heatmap
5
u-guide
python/mapbox-density-heatmaps/
thumbnail/mapbox-density.png

Mapbox Access Token

To plot on Mapbox maps with Plotly, you may need a Mapbox account and token or a Stadia Maps account and token, depending on base map (mapbox_style) you use. On this page, we show how to use the "open-street-map" base map, which doesn't require a token, and a "stamen" base map, which requires a Stadia Maps token. See our Mapbox Map Layers documentation for more examples.

OpenStreetMap base map (no token needed): density mapbox with plotly.express

Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures.

With px.density_mapbox, each row of the DataFrame is represented as a point smoothed with a given radius of influence.

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')

import plotly.express as px
fig = px.density_mapbox(df, lat='Latitude', lon='Longitude', z='Magnitude', radius=10,
                        center=dict(lat=0, lon=180), zoom=0,
                        mapbox_style="open-street-map")
fig.show()

Stamen Terrain base map (Stadia Maps token needed): density mapbox with plotly.express

Some base maps require a token. To use "stamen" base maps, you'll need a Stadia Maps token, which you can provide to the mapbox_accesstoken parameter on fig.update_layout. Here, we have the token saved in a file called .mapbox_token, load it in to the variable token, and then pass it to mapbox_accesstoken.

import plotly.express as px
import pandas as pd

token = open(".mapbox_token").read() # you will need your own token

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')

fig = px.density_mapbox(df, lat='Latitude', lon='Longitude', z='Magnitude', radius=10,
                        center=dict(lat=0, lon=180), zoom=0,
                        mapbox_style="stamen-terrain")
fig.update_layout(mapbox_accesstoken=token)
fig.show()

OpenStreetMap base map (no token needed): density mapbox with plotly.graph_objects

If Plotly Express does not provide a good starting point, it is also possible to use the more generic go.Densitymapbox class from plotly.graph_objects.

import pandas as pd
quakes = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')

import plotly.graph_objects as go
fig = go.Figure(go.Densitymapbox(lat=quakes.Latitude, lon=quakes.Longitude, z=quakes.Magnitude,
                                 radius=10))
fig.update_layout(mapbox_style="open-street-map", mapbox_center_lon=180)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

Stamen Terrain base map (Stadia Maps token needed): density mapbox with plotly.graph_objects

Some base maps require a token. To use "stamen" base maps, you'll need a Stadia Maps token, which you can provide to the mapbox_accesstoken parameter on fig.update_layout. Here, we have the token saved in a file called .mapbox_token, load it in to the variable token, and then pass it to mapbox_accesstoken.

import plotly.graph_objects as go
import pandas as pd

token = open(".mapbox_token").read() # you will need your own token

quakes = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')

fig = go.Figure(go.Densitymapbox(lat=quakes.Latitude, lon=quakes.Longitude, z=quakes.Magnitude,
                                 radius=10))
fig.update_layout(mapbox_style="stamen-terrain", mapbox_center_lon=180, mapbox_accesstoken=token)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

Reference

See function reference for px.(density_mapbox) or https://plotly.com/python/reference/densitymapbox/ for more information about mapbox and their attribute options.