Keep in mind that you may require a mapbox account and access token to create these plots.
Feel free to explore the plotly and Mapbox documentation to learn more.”
Plotly.express.density_mapbox()
The function syntax is as shown:
The function parameters:
- Data_frame – defines the data frame containing the column names used in the plot.
- lat – specifies the values used to position the marks according to the latitude on the map.
- lon – defines the values used to position the marks according to the longitude on the map.
- z – defines the values used to position the marks along the z axis in the cartesian plane.
- Title – sets the title for the figure.
- Template – sets the template used for the plot.
- width/height – defines the width and height of the figure in pixels.
Practical Example
The example below shows how to use the density_mapbox() function to plot the US flight path dataset on the map.
df = pd.read_csv ('https://raw.githubusercontent.com/plotly/datasets/master/ 2011_february_us_airport_traffic.csv')
import plotly.express as px
px.set_mapbox_access_token(open('access.mapbox_token').read())
fig = px.density_mapbox(df, lat=df['lat'], lon=df['long'], zoom=0)
fig.show()
Replace the access.mapbox_token with the filename of your MapBox access token.
The code above should use the dataset to plot the flight traffic on a map.
Output:
Closing
This article describes the method of creating a density heatmap using Plotly Express and MapBox API.