Airport Graph#

Download this notebook from GitHub (right-click to download).


import pandas as pd
import geoviews as gv

from geoviews import opts
from bokeh.sampledata.airport_routes import airports, routes

gv.extension('bokeh')

Define data#

# Count the number of connections from each airport
counts = routes.groupby('SourceID')[['Stops']].count().reset_index().rename(columns={'Stops': 'Connections'})
airports_df = pd.merge(airports, counts, left_on='AirportID', right_on='SourceID', how='left')

# Select only US mainland airports & convert from Mercator to Latitudes/Longitudes
airport_points = gv.Points(airports_df, ['Longitude', 'Latitude']).select(Longitude=(-170, -50), Latitude=(0, 50))

# Declare nodes, graph and tiles
nodes = gv.Nodes(airport_points, ['Longitude', 'Latitude', 'AirportID'],
                 ['Name', 'City', 'Connections'])
graph = gv.Graph((routes, nodes), ['SourceID', 'DestinationID'], ['Source', 'Destination'])
tiles = gv.tile_sources.OSM

# Select 50 busiest airports
busiest = list(routes.groupby('SourceID').count().sort_values('Stops').iloc[-50:].index.values)
busiest_airports = graph.select(AirportID=busiest, selection_mode='nodes')

Plot#

(tiles * busiest_airports).opts(
    opts.Graph(edge_selection_line_color='black', edge_hover_line_color='red',
               edge_line_width=1, edge_line_alpha=0.01, edge_nonselection_line_alpha=0.01,
               width=800, height=600))
This web page was generated from a Jupyter notebook and not all interactivity will work on this website. Right click to download and run locally for full Python-backed interactivity.

Download this notebook from GitHub (right-click to download).