Spatial Heatmaps

Spatial heatmap #

A spatial heatmap is one in which the density of points geographically is reflected by a smooth color gradient. It is used to give clear indications of how many times a particular observation or piece of data has been collected per unit area.

Earlier in this course, we had plotted data spatially, using a dot density map, where each individual point is plotted on a map. We attempted to make the density of points apparent using small points and transparency. The result is shown below:

This does give an impression of the density, but I think we can do a better job by using the actual spatial heatmap method from Plotly. Once we finish this, it will look like:

Spatial heatmaps in Plotly #

The method we will use is not called a heatmap in Plot, but a density map. Below, I illustrate how to use this.

Preparing the data #

For the heatmap we simply need data with latitude and longitude information. The data that we used before was actually taken from a database that is commonly used and is available within Python. So we will also see how to access data this way. Actually, this time, let us start with the solution and then explain how it works!

import plotly.graph_objects as go
import pandas as pd
from sklearn.datasets import fetch_california_housing

# --- 1. Load Sample Data ---
housing = fetch_california_housing(as_frame=True)
df = housing.frame

# --- 2. Create the Base Density Map Figure ---
# Define the colorbar initially with just the title.
fig = go.Figure(go.Densitymap(
                            lat=df['Latitude'],
                            lon=df['Longitude'],
                            radius=1,
                            colorscale="agsunset",
                            colorbar=dict(
                                # Set tick positions to the min and max of the scale
                                tickvals=[0.02, 0.98],
                                # Set the text labels for those two positions
                                ticktext=["low house density", "high house density"]    
                                )
                            )
                        )

# --- 3. Update the Layout to Configure the Map ---
fig.update_layout(
                title="The highest density of homes are in central and southern California",
                map_style="open-street-map",
                map_center={"lat": 37.3, "lon": -119.5},
                map_zoom=4.5,
                width=600,
                height=800,
                margin=dict(l=5, r=5, t=30, b=5),
            )



# --- 4. Save and Show the Figure ---
fig.write_image("spatialHeatmap.svg")
fig.show("png")

this will produce the following figure:

How it works #

We start by importing libraries as needed. We have two familiar libraries and one new one. The two familiar ones are Plotly and Pandas. Though we typically use the subplots method of Plotly, just as the last time we used spatial data, we will use graph objects. Pandas will be used to hold the data we need. The new library is sklearn, which stands for sci-kit-learn. This library has many functions that pertain to machine learning, but the reason we want to use it , is because it has data sets within it. In particular, it has the ability to pull the California housing data, using the function fetch_california_housing.

The first thing we do is to fetch the housing data, asking for it to be gathered in a way that can be converted to a pandas data frame. We then convert this to a data frame and assign it to the df variable.

Next, we create the figure. Just as with other spatial plots in data, we make a Figure and then add a map, in this case a density map. We need to supply latitude and longitude data, so that we know where houses are. If we simply want to know the density of the houses, this is all we need. It would be possible to also assign some z-value, which would weight the density (for instance, by cost or size, etc.), but for now, let us just keep it as simple density. We also supply a keyword argument radius which basically determine the area over which the density is calculated. If you make this value larger, then the data will spread out more (try it!). Finally, we set up the colorscale and color bar. For the latter, we specify where the tick marks should appear (tickvals) and what they should say (ticktext). If we didn’t use this approach, then there would just be numbers given. But, in my opinion, the numbers are not super important in this case. What is important is just understanding the high and low range. So, I keep it simple.

The final setup of the plot is to provide a title, map style, the center of the map position and the zoom level, the size of the canvas, and the margins.

With this, we can simply write the image and show it. Done!