Mapbox density heatmap with Plotly in Python

Data visualization is one of the highlights of data analysis as it involves a graphical representation of data. We can enhance our ability to interpret data findings greatly.

Mapbox density heatmaps are interesting plots in Plotly that combine density and heatmaps. In this Answer, we will learn these and visualize the world map with density heatmaps, so don't miss out!

Data visualization examples
Data visualization examples

Plotly

Plotly is a data visualization library that allows us to create rich and dynamic visualizations in Python, R, and JavaScript. Some interesting features are:

  1. Zooming

  2. Tooltips

  3. Rotating

  4. Downloading plots

Plotly logo
Plotly logo

Let's break down the keywords before continuing with Mapbox density heatmaps.

Density

In data visualization, density indicates how closely data points are packed together in a specific region.

Heatmaps

Heatmaps are visual representations that use colors to display the intensity of data points.

Sample heatmap
Sample heatmap

Mapbox

Mapbox is a platform for creating different interactive maps and allows us to integrate location-based data and design customized maps accordingly.

Mapbox density heatmaps

Very simply put, we can use Mapbox's capabilities to visualize the concentration of data points over areas through color intensity.

Density heatmaps provide a great way to represent the distribution of data points across a geographic area. By using color gradients to signify density levels, we can easily identify regions with higher concentrations of data.

Code walkthrough

Importing and preprocessing the data

Our first step is to get the data we need for plotting on the map. For our answer, we'll plot the earthquake data and use pandas to preprocess the dataset for us efficiently.

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

Creating the heatmap

To make our density heatmap, we will utilize Plotly's Densitymapbox trace type. This trace type allows us to specify latitude, longitude, and density values to easily generate an interactive density heatmap in a few lines!

import plotly.graph_objects as go
fig = go.Figure(go.Densitymapbox(lat = quakes.Latitude, lon = quakes.Longitude, z = quakes.Magnitude,
radius = 10))

Customizing our map visualization

We can customize the visualization by adjusting the map style, centering the map, and refining the map layout.

fig.update_layout(mapbox_style = "stamen-terrain", mapbox_center_lon = 180)
fig.update_layout( margin = { "r": 0, "t": 0, "l": 0, "b": 0} )

Displaying the final plot

Finally, we can display the Mapbox density heatmap by calling the show() method on our Plotly figure.

fig.show()

Executable code

The following code is completely executable and renders the interactive Mapbox plot for us and even saves it in an HTML file called output.html. You can experiment with it and click "Run" once done.

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 = "stamen-terrain", mapbox_center_lon = 180)
fig.update_layout( margin = { "r": 0, "t": 0, "l": 0, "b": 0} )

fig.show()
fig.write_html("output.html", auto_open=True)

Demonstration

This is how the plot shows the varying density of earthquakes occurring at different places on the map.

Code output
Code output

We can zoom in at any point within the map.

Zoomed in output
Zoomed in output

Let's witness the interactive aspect of the plot as well.

Use cases of Mapbox density heatmaps

Mapbox density heatmaps have quite a few real-life applications in various domains where the density of a variable, such as population.

Use cases

Explanations

Urban planning

Analyzing population density for city planning

Business intelligence

Optimizing store locations for brands

Disaster response

Areas affected during different emergencies

End notes

Congratulations, we've succeeded in creating an interactive visualization to reveal geographical data patterns in a highly clear and zoomable map plot! We have understood data distribution across space by combining Plotly's features and Mapbox's rich mapping capabilities.

Test your Mapbox density heatmap knowledge!

Question

What Plotly function do we call in order to create a Mapbox density heatmap figure?

Show Answer

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved