How to Create Eye-Catching Maps With Python and Kepler.gl
By: Thiago Carvalho
Tags: Mapping, Python, Data Science, Programming, Data Visualization

How to Create Eye-Catching Maps With Python and Kepler.gl

Use this intuitive tool to simplify mapping

In this article, we’ll explore Kepler.gl, an open-source solution for geospatial data visualization and exploration. Kepler was developed by Uber to make it easier for users of all levels to design meaningful maps that also look good. The tool can handle large amounts of data and has a friendly, intuitive interface that allows users to build effective maps in an instant.

Available for all to use since 2018, it’s about time we get a closer look at how the tool fits into the data visualization landscape. In this article, we’ll cover the basics of importing data to Kepler using Python’s Pandas and GeoPandas, how to design your visualization, and export the map to an HTML file.

Vancouver Number of Graffitis by Block

Getting Started

The dataset for this example is NOOA’s Global Significant Earthquakes dataset. [Kaggle]

Import statements.

Pandas

I’m interested in looking at the intensity of the earthquakes and if they generated a tsunami, but those aren’t the only values we’ll need. We also need some Geolocation.

# read csv

df = pd.read_csv('data/Worldwide-Earthquake-database.csv')

In this dataset, our geolocation is stored in two fields, Latitude and Longitude.

Those will be essential for Kepler to draw our data, so we need to make sure all those values are clean and usable.

# lat and lon to numeric, errors converted to nan

df['LONGITUDE'] = pd.to_numeric(df.LONGITUDE, errors='coerce')

df['LATITUDE'] = pd.to_numeric(df.LATITUDE, errors='coerce') # drop rows with missing lat, lon, and intensity

df.dropna(subset=['LONGITUDE', 'LATITUDE', 'INTENSITY'], inplace=True) # convert tsunami flag from string to int

df['FLAG_TSUNAMI'] = [1 if i=='Yes' else 0 for i in df.FLAG_TSUNAMI.values]

After loading the data to Pandas, we can use .numeric to make sure they’re numbers, then use .dropna to remove the empty values.

You can also convert the TSUNAMI_FLAG from yes and no to 1 and 0.

Cleaning and preparation are up to your needs, you may have different requirements or use other tools for that, but once your data is in a Pandas data frame you can map.

Kepler.gl

Kepler is straightforward. It gives you a world map and tools to build the visualization; it expects the data, and the configurations of the map.

Let’s start by defining a map. (I’m using Kepler for Jupyter)

kepler_map = keplergl.KeplerGl(height=400)

kepler_map

Default map without data.

Then we add the data frame to it.

kepler_map.add_data(data=df, name="earthquakes")

Map marking the earthquakes.

And the map is updated. Quite easy!

You can load your data to Kepler with Pandas and Geopandas, which support a more comprehensive array of extensions, or directly from a GeoJSON and CSV files.

Design

On the top left of the map, there’s an arrow that opens the settings menu.

Settings Menu

On the menu we have:

Layers — Defines how the variables are encoded to the map

— Defines how the variables are encoded to the map Filters — For selecting smaller sets of data

— For selecting smaller sets of data Interactions — Defines interactions such as Tooltips, search boxes, and others

— Defines interactions such as Tooltips, search boxes, and others Basemap — Defines the style of the world map and other elements like labels, roads, styles

Layers

You can select an existing layer or create a new one, then click the ellipsis besides Basic. That’ll open a selection of different encodings for your map, try selecting Hexbin for the next example.