Connecting neighbourhoods

I've been working on how best to look at connections between the various Mobi bikeshare stations throughout Vancouver. One thing the quickly became obvious was that some visualizations are much too noisy with all stations, but work nicely when I group stations into neighbourhoods. Luckily the city geometry files of the various neighourhoods available in their open data collection, so I was able to use those to group the stations according to the city's official definitions. See the distributions of stations below.

In [2]:
import mobi
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

import as ccrs
import as shpreader
import shapely
import pyproj

%matplotlib notebook
In [7]:
sdf = mobi.get_stationsdf('/data/mobi/data/')
nhoods = sorted(set(sdf['neighbourhood']))
In [8]:
# Helper function to filter polygons in shapefiles
def shapes_with_atts(shapef,att,vals):
    shapes = list(shpreader.Reader(shapef).geometries())
    records = list(shpreader.Reader(shapef).records())
    shapes = [s for s,r in sorted(zip(shapes,records),key=lambda x:x[1].attributes[att]) if r.attributes[att] in vals]
    return shapes

# Prep colors. We need sdf.color to be integers (matched to hoods) so that matplotlib can use it with a colormap
color_list =
sdf['color'] ={n:c for n,c in zip(nhoods,range(11))})
plot = mobi.GeoPlot()
statlats = sdf.loc[,'coordinates'].map(lambda x: x[1])
statlongs = sdf.loc[,'coordinates'].map(lambda x: x[0])

shapef = '/home/msj/shapes/local_area_boundary.shp'
pshapef = '/home/msj/shapes/park_polygons.shp'
shapes = shapes_with_atts(shapef,'NAME',nhoods) 

# Hack to add stanley park in alphabetical order with other neighbourhoods
shapes = shapes[:-2] + shapes_with_atts(pshapef,'PARK_NAME',['Stanley Park']) + shapes[-2:], ccrs.epsg(26910),facecolor=color_list,edgecolor='k',alpha=0.6,zorder=1)

s =,statlongs,transform=ccrs.PlateCarree(),


cb = plot.f.colorbar(s,fraction=0.35)
cb.set_ticks([(x+0.5)*0.9 for x in range(len(nhoods))])

I've make Stanley Park it's own neighbourhood instead of grouping it in with the West End. There's also only one station each in South Cambie and Riley Park due to a couple of straggler stations south of 16th. The 16th and Heather station is immediately shouth of 16th so could easily be grouped with Fairview but rules are rules.

In [5]:
import holoviews as hv

from holoviews import opts,dim
from mobi_system_data import *