The idea and the airport's data for this demo was found in the "Towards Data Science" article How to Visualize Data on top of a Map in Python using the Geoviews library by Christos Zeglis.
The paper demonstrates how to make a plot to visualize the passengers volume for the busiest airports in Greece, and the neighbor country, Turkey, for comparison reasons.
The author uses Geoviews and Bokeh libraries to achieve such visualisation. We are going to use the Lets-Plot library solely to do the same job and, on top of it, fill both countries boundaries with semi-transparent colors.
The tasks completed in this tutorial:
import numpy as np
import pandas as pd
from lets_plot import *
LetsPlot.setup_html()
For the purposes of this tutorial, we are going to use "CityLights 2012" map-tiles © NASA Global Imagery Browse Services (GIBS).
LetsPlot.set(maptiles_zxy(
url='https://gibs.earthdata.nasa.gov/wmts/epsg3857/best/VIIRS_CityLights_2012/default//GoogleMapsCompatible_Level8/{z}/{y}/{x}.jpg',
attribution='<a href="https://earthdata.nasa.gov/eosdis/science-system-description/eosdis-components/gibs">© NASA Global Imagery Browse Services (GIBS)</a>',
max_zoom=8
))
The "airports" dataset is already cleaned and only contains data on Greece and Turkey airports.
airports = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/airports.csv")
airports.head(3)
IATA | city | latitude | longitude | passengers | country | |
---|---|---|---|---|---|---|
0 | ATH | Athens | 37.9354 | 23.9437 | 24.13 | GR |
1 | HER | Heraklion | 35.3400 | 25.1753 | 8.00 | GR |
2 | SKG | Thessaloniki | 40.5230 | 22.9767 | 6.67 | GR |
On this step we are using built-in geo-coding capabilities of the Lets-Plot library.
from lets_plot.geo_data import *
countries_gcoder = geocode_countries(['GR', 'TR'])
countries_gcoder.get_geocodes()
The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).
id | country | found name | centroid | position | limit | |
---|---|---|---|---|---|---|
0 | 192307 | GR | Greece | [21.5772458904778, 39.6987419575453] | [20.0085201859474, 37.6486940681934, 26.635576... | [19.3734280765057, 34.8015573620796, 29.645195... |
1 | 174737 | TR | Turkey | [35.4993526199025, 38.95283639431] | [26.0636124014854, 35.807680785656, 44.8176634... | [25.6654492020607, 35.807680785656, 44.8176634... |
(ggplot()
+ geom_livemap(location=[26.65, 38.61],
zoom=6)
+ geom_polygon(aes(fill='country'),
data=countries_gcoder.inc_res().get_boundaries(),
size=0,
alpha=.2)
+ geom_point(aes('longitude', 'latitude', fill='country', size='passengers'),
data=airports,
shape=21,
alpha=.7,
color='white',
tooltips=layer_tooltips()
.format('passengers', '{.1f} m' )
.format('^x', '.2f').format('^y', '.2f')
.line('@|@IATA')
.line('Passengers|@passengers')
.line('City|@city')
.line('Country|@country')
.line('Longitude|^x')
.line('Latitude|^y'))
+ scale_fill_manual(values=['#30a2da', '#fc4f30'])
+ scale_size(range=[10, 40], trans='sqrt')
+ theme(legend_position='none')
+ ggsize(900, 520)
)