import pydeck as pdk
import pandas as pd
NASA has collected global light emission data for over 30 years. The data set is a deeply fascinating one and has been used for news stories on the Syrian Civil War [1], North Korea [2], and economic growth [3].
In this notebook, we'll use a deck.gl HeatmapLayer to visualize some of the changes at different points in time.
The data for Chengdu, China, is cleaned and available below. Please note this data is meant for demonstration only.
LIGHTS_URL = 'https://raw.githubusercontent.com/ajduberstein/lights_at_night/master/chengdu_lights_at_night.csv'
df = pd.read_csv(LIGHTS_URL)
df.head()
year | lng | lat | brightness | |
---|---|---|---|---|
0 | 1993 | 104.575 | 31.808 | 4 |
1 | 1993 | 104.583 | 31.808 | 4 |
2 | 1993 | 104.592 | 31.808 | 4 |
3 | 1993 | 104.600 | 31.808 | 4 |
4 | 1993 | 104.675 | 31.808 | 4 |
pydeck does need to know the color for this data in advance of plotting it
df['color'] = df['brightness'].apply(lambda val: [255, val * 4, 255, 255])
df.sample(10)
year | lng | lat | brightness | color | |
---|---|---|---|---|---|
56182 | 2009 | 103.817 | 31.642 | 6 | [255, 24, 255, 255] |
112600 | 2001 | 103.650 | 29.717 | 5 | [255, 20, 255, 255] |
212788 | 2007 | 103.575 | 29.825 | 3 | [255, 12, 255, 255] |
137713 | 2003 | 103.800 | 30.292 | 3 | [255, 12, 255, 255] |
54477 | 1995 | 105.158 | 29.542 | 4 | [255, 16, 255, 255] |
34049 | 1997 | 105.683 | 29.683 | 6 | [255, 24, 255, 255] |
47662 | 1995 | 104.108 | 30.583 | 16 | [255, 64, 255, 255] |
2594 | 1993 | 104.050 | 31.225 | 4 | [255, 16, 255, 255] |
316246 | 1999 | 103.658 | 29.608 | 6 | [255, 24, 255, 255] |
24422 | 1997 | 105.442 | 30.775 | 4 | [255, 16, 255, 255] |
We can plot this data set of light brightness by year, configuring a slider to filter the data as below:
plottable = df[df['year'] == 1993].to_dict(orient='records')
view_state = pdk.ViewState(
latitude=31.0,
longitude=104.5,
zoom=8)
scatterplot = pdk.Layer(
'HeatmapLayer',
data=plottable,
get_position=['lng', 'lat'],
get_weight='brightness',
opacity=0.5,
pickable=False,
get_radius=800)
r = pdk.Deck(
layers=[scatterplot],
initial_view_state=view_state,
views=[pdk.View(type='MapView', controller=None)])
r.show()
import ipywidgets as widgets
from IPython.display import display
slider = widgets.IntSlider(1992, min=1993, max=2013, step=2)
def on_change(v):
results = df[df['year'] == slider.value].to_dict(orient='records')
scatterplot.data = results
r.update()
slider.observe(on_change, names='value')
display(slider)