How to easily create a geographical heatmap using SAS and Python

  • Date August 30, 2019
  • Written by Pavel Rogatch
  • Category Python

This example shows how to create a geographical heatmap from a SASHELP.QUAKES dataset using SAS9API and Python.


Geographical heatmaps are great visualisation tools. They show the density of points in or near a particular area. They are especially useful when you have thousands of data points and are mainly interested in their overall distribution. For example, by looking at a heatmap you can easily determine regions with high crime rates, temperatures, earthquake activity, population density, etc.

Sometimes SAS users need to create such maps. One of the ways to create a geographical heatmap is to use a gmaps plugin designed for embedding Google Maps in Jupyter notebooks and visualising data on these maps. Here we demonstrate how to easily do it.


To follow the examples presented here you need to have the following:

  • access to SAS9API proxy,
  • Python 3 installed,
  • Jupyter Notebook installed (,
  • gmaps package for Jupyter (see below),
  • Google Maps API Key (see below),
  • pandas  and sas9api  Python libraries (see below).

Step 1 – Setting up the necessary tools

We need to install gmaps  package for Jupyter Notebook. The easiest way to install this package is to do it with conda  running this command in Anaconda Prompt (if you use Anaconda distribution of the Python language): conda install -c conda-forge gmaps .

(Note: you may also need to run jupyter nbextension enable –py –sys-prefix gmaps  to enable displaying of maps).

To install gmaps  with pip  do the following:

  • Make sure that you have enabled ipywidgets  widgets extensions:

jupyter nbextension enable –py –sys-prefix widgetsnbextension

  • You can then install gmaps  with:

pip install gmaps

  • Then tell Jupyter to load the extension with the following command:

jupyter nbextension enable –py –sys-prefix gmaps  (make sure you get “Validating: ok” after running this command)

(You can find full installation instructions at

After installing gmaps  we need to restart Jupyter Notebook.

Then we need to get Google Maps API Key by following the instructions at

Python sas9api  library can be gotten at We need to download the  file to our local computer. It can be done in different ways:

  • using following command (if you have Git installed): git clone  to download this repository to a designated folder on a local computer;
  • going to, right-clicking on the Raw button in the top right and then choosing “Save target as” to save  file to a local computer.

We need to place  file in the same folder with our Python code or provide a path to it for a successful import.

Now we are ready to start writing a Python code in Jupyter Notebook. We will use data of 10000 earthquakes from the SASHELP.QUAKES dataset to create a heatmap of regions with earthquake activity based on locations and magnitudes of these earthquakes.

Step 2 – Importing the necessary libraries

Let us import sas9api , gmaps  and pandas  libraries:

import sas9api as sas
import gmaps
import pandas as pd

Step 3 – Using a Google Maps API Key

We need to pass our Google Maps API Key to gmaps (you will need to specify your key received from Google):


Step 4 – Specifying SAS9API URL and port

We assign SAS9API server URL and port to a variable (you will need to assign your SAS9API URL and port):

url = "your_SAS9API_url:port"

Step 5 – Retrieving data from a SAS server

We retrieve data from the SAS server using retrieve_data  function from the sas9api  Python library.

We pass the following parameters to this function:

  • url  – SAS server URL defined earlier,
  • library_name  – SAS library name,
  • dataset_name  – SAS dataset name,
  • server_name  – SAS workspace server name,
  • limit  – maximum number of records to retrieve,
  • filter_  – filter to apply to the dataset: here we want to retrieve only records where Type=”earthquake”,
  • only_payload  – a flag set to True to get data as a list of dictionaries containing dataset records.
dat = sas.retrieve_data(url, library_name="sashelp", dataset_name="quakes",
    limit=10000, filter_='{"Type":"earthquake"}', only_payload=True)

For a more convenient way of presenting the data we convert it to a Pandas DataFrame and then look at data.

quakes_df = pd.DataFrame(dat)
Depth Latitude Longitude Magnitude RootMeanSquareTime Type dNearestStation
0 6.2500 35.688000 -121.128670 2.75 0.0700 earthquake 0.02121
1 0.0000 41.900900 -119.622400 4.59 0.0742 earthquake 0.50700
2 0.5329 41.883600 -119.641100 3.19 0.0000 earthquake 0.49300
3 0.5077 41.889700 -119.639900 3.12 0.0119 earthquake 0.49400
4 33.5700 40.878334 -123.270332 2.60 0.0600 earthquake 0.22160


Now it is time to create a map!

Step 6 – Creating a geographical heatmap

Since we are dealing with earthquakes, we create a TERRAIN map (you can try other available types of maps: ROADMAP (the classic Google Maps style), SATELLITE (just satellite tiles with no overlay), HYBRID (satellite base tiles but with features such as roads and cities overlaid)).

fig = gmaps.figure(map_type='TERRAIN')

We can add different layers to this map.

First, we add a heatmap layer specifying the following parameters:

  • locations  – epicentres of earthquakes defined by the Latitude and Longitude columns in our DataFrame;
  • weights  – by default all points have the same weight in creating a heatmap, but we want stronger earthquakes to have more weight, so we use earthquake magnitudes as weights;
  • point_radius  – “radius of influence” of each data point;
  • dissipating  – a flag set to False indicating that the radius of influence of each point remains the same as you zoom in or out.
# Creating a heatmap layer
heatmap_layer = gmaps.heatmap_layer(quakes_df[['Latitude', 'Longitude']],
weights=quakes_df['Magnitude'], point_radius=1, dissipating=False)
# Adding a heatmap layer to the map

Then we add one more layer with markers of epicentres of all earthquakes with magnitudes 5.0 and greater. To display information about a particular earthquake when we hover our mouse over its marker or click on its marker, we specify the hover_text  and info_box_content  parameters as a list of strings with the necessary information.

# Creating a new DataFrame only with earthquakes of magnitude 5.0 and greater
quakes_5plus = quakes_df[quakes_df['Magnitude'] >= 5.0]

# Creating a list of strings with the necessary information to be displayed when we hover a mouse over earthquake markers
info_5plus = [f"EARTHQUAKE:\nLatitude={str(p[1])}\nLongitude={str(p[2])}\nDepth={str(p[0])}\nMagnitude={str(p[3])}" 
    for p in quakes_5plus.values]

# Creating a layer with markers
quakes_layer = gmaps.symbol_layer(quakes_5plus[['Latitude', 'Longitude']], fill_opacity=0, stroke_color="black", scale=3, 
    hover_text=info_5plus, info_box_content=info_5plus)
# Adding a layer with markers to the map

# Showing the map

After ZOOMING IN the map looks like this:

Geo heatmap

The red areas on the map are regions with high earthquake activity while the green areas are regions with low earthquake activity.

We can ZOOM IN and ZOOM OUT, move map, and hover a mouse over the markers to see the detailed information about earthquakes of magnitude 5.0 and greater (see picture below).

Geo heatmap zoomed in


We have created a geographical heatmap showing the regions with earthquake activity. As you could see it was not a difficult process and did not require a lot of code. Now you know how to easily create beautiful heatmaps for analytical or reporting purposes using SAS and Python. There is much more to explore about creating maps:

  • different types of maps;
  • different layers which can be added to maps (e.g., GeoJSON, directions, bicycling, transit and traffic layers);
  • ways to customise a map (width, height, layout, colour gradient and opacity, etc.);
  • ways of drawing markers, lines and polygons;
  • etc.

For the full list of options and different examples please refer to this documentation:

SAS9API not only allows you to create maps and use functionality of Python, but also to use functionality of other programming languages. To find out how to use SAS9API with R language, please refer to