plotly

Plotly.Express.Density_mapbox

“You can create density heatmaps on the MapBox api using the density_mapbox() function in Plotly.

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:

plotly.express.density_mapbox (data_frame=None, lat=None, lon=None, z=None, hover_name=None, hover_data=None, custom_data=None, animation_frame=None, animation_group=None, category_orders=None, labels=None, color_continuous_scale=None, range_color=None, color_continuous_midpoint=None, opacity=None, zoom=8, center=None, mapbox_style=None, radius=None, title=None, template=None, width=None, height=None)

The function parameters:

  1. Data_frame – defines the data frame containing the column names used in the plot.
  2. lat – specifies the values used to position the marks according to the latitude on the map.
  3. lon – defines the values used to position the marks according to the longitude on the map.
  4. z – defines the values used to position the marks along the z axis in the cartesian plane.
  5. Title – sets the title for the figure.
  6. Template – sets the template used for the plot.
  7. 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.

import pandas as pd
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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list