Public account: You and the cabin by: Peter Editor: Peter

Hello, I’m Peter

Today, I will introduce a fantastic spatial (geographic) data visualization artifact: Keplergl. This artifact that Xiaobian recently stumbled upon is completely open source for Uber and is the default tool for spatial data visualization within Uber.

Keplergl, the Python-oriented interface package for Jupyter Notebook, allows you to write Python code in jupyter Notebook to pass in data in a variety of formats, and use its built-in spatial data visualization capabilities in the interactive window embedded in the Notebook. There are three main learning sites:

Kepler. gl/

2, Jupyter Notebook Instruction manual address: github.com/keplergl/ke…

3. Case address: github.com/keplergl/ke…

The installation

Keplergl installation is very simple. If the error, please baidu or Google to solve, xiaobian is a one-time installation success ❤️

pip install keplergl
Copy the code

Jing is colourful figure

Here comes a wave of amazing graphics:

Introduction to case

import pandas as pd
import geopandas as gpd

from keplergl import KeplerGl

# create object
kep1 = KeplerGl(height=600)
Activate the object and load it into the Jupyter Notebook
kep1
Copy the code

You can see that after running the basic code in Jupyter directly generated built-in graphics, graphics itself is also dynamic; The dark background is also my favorite:

Add data

By default, Keplergl can add three forms of data:

  • csv
  • GeoJSON
  • DataFrame

CSV format

There is a CSV data in the local directory: China. CSV, which records the longitude and latitude of each province in China:

with open("china.csv"."r") as f:
    csv_data = f.read()
    
# add_data Adds data
kep1.add_data(data=csv_data, name="csv_kep")
kep1
Copy the code

DataFrame format

china = pd.read_csv("china.csv")
kep1.add_data(data=china, name="dataframe_kep")
kep1
Copy the code

GeoJson format

url = 'http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_040_00_500k.json'
country_gdf = gpd.read_file(url)  # geopandas Reads JSON files

kep1.add_data(data=country_gdf, name="state")
kep1
Copy the code

Custom graph

Keplergl customization method: key buttons. Once inside, you can customize the operation

Save and reuse configuration

The configuration of the instantiated KEP can be saved and reused in later instance objects:

1. Save:

Save to a file
with open('config1.py'.'w') as f:
    f.write('config={}'.format(kep1.config))
    
# Run: Magic command %run
%run config1.py
Copy the code

2, reuse,

kep2 = KeplerGl(height=400,
                data={"layer1":df},
                config=kep1.config  # kep1 configuration
               )
kep2
Copy the code

Save image

1, simplified version, mainly file name

kep1.save_to_html(file_name="first_kep.html")
Copy the code

2, full version: file name, configuration, data, readability

# 4 arguments
kep1.save_to_html(file_name="first_kep.html",
                  data={'data_1':china},
                  config=config,
                  read_only=True
                 )
Copy the code

The online operation

The operation shown above is done in shinotebook, which we can also do online directly: kepler.gl/demo