{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial: Interactive Maps with Python, Part 2\n", "## Custom Plots on Geographic Maps\n", "\n", "This is part 2 of our 3-part tutorial on interactive maps with python. We are using the NYC bikeshare data as an example. In part 1 we covered how to do basic visualizations with python and folium. Here we will dig a little deeper into how to make custom map overlays.\n", "\n", "In part 1, we already noticed some bike migration: at 9am, some regions have more bike departures and different regions have more bike arrivals. In this tutorial we will see if we can show which *paths* people take. We will put particular emphasis on creating a customized visual appearance. Specifically, we will \n", "1. customize the effect of overlapping paths to show traffic density, and \n", "2. we will add a glow effect to draw attention to high density areas. \n", "This design is loosly inspired by a map created by facebook [https://newsroom.fb.com/news/2017/06/two-billion-people-coming-together-on-facebook/].\n", "\n", "To achieve these effects we will use a raster layer (i.e. and image overlay) and draw our own pixels instead of using the built-in objects in folium to draw the paths (e.g. using PolyLine).\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2018-11-28T20:43:51.493157Z", "start_time": "2018-11-28T20:43:49.755701Z" } }, "outputs": [], "source": [ "from PIL import Image, ImageDraw\n", "import numpy as np\n", "import pandas as pd\n", "import folium\n", "import matplotlib.pyplot as plt\n", "import matplotlib.colors\n", "from matplotlib.colors import LinearSegmentedColormap, rgb_to_hsv, hsv_to_rgb\n", "import scipy.ndimage.filters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As before, we load data from the NYC bike share program" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2018-11-28T20:44:11.802320Z", "start_time": "2018-11-28T20:43:51.495762Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " | Trip Duration | \n", "Start Time | \n", "Stop Time | \n", "Start Station ID | \n", "Start Station Name | \n", "Start Station Latitude | \n", "Start Station Longitude | \n", "End Station ID | \n", "End Station Name | \n", "End Station Latitude | \n", "End Station Longitude | \n", "Bike ID | \n", "User Type | \n", "Birth Year | \n", "Gender | \n", "hour | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "328 | \n", "2016-10-01 00:00:07 | \n", "2016-10-01 00:05:35 | \n", "471 | \n", "Grand St & Havemeyer St | \n", "40.712868 | \n", "-73.956981 | \n", "3077 | \n", "Stagg St & Union Ave | \n", "40.708771 | \n", "-73.950953 | \n", "25254 | \n", "Subscriber | \n", "1992.0 | \n", "1 | \n", "0 | \n", "
1 | \n", "398 | \n", "2016-10-01 00:00:11 | \n", "2016-10-01 00:06:49 | \n", "3147 | \n", "E 85 St & 3 Ave | \n", "40.778012 | \n", "-73.954071 | \n", "3140 | \n", "1 Ave & E 78 St | \n", "40.771404 | \n", "-73.953517 | \n", "17810 | \n", "Subscriber | \n", "1988.0 | \n", "2 | \n", "0 | \n", "
2 | \n", "430 | \n", "2016-10-01 00:00:14 | \n", "2016-10-01 00:07:25 | \n", "345 | \n", "W 13 St & 6 Ave | \n", "40.736494 | \n", "-73.997044 | \n", "470 | \n", "W 20 St & 8 Ave | \n", "40.743453 | \n", "-74.000040 | \n", "20940 | \n", "Subscriber | \n", "1965.0 | \n", "1 | \n", "0 | \n", "
3 | \n", "351 | \n", "2016-10-01 00:00:21 | \n", "2016-10-01 00:06:12 | \n", "3307 | \n", "West End Ave & W 94 St | \n", "40.794165 | \n", "-73.974124 | \n", "3357 | \n", "W 106 St & Amsterdam Ave | \n", "40.800836 | \n", "-73.966449 | \n", "19086 | \n", "Subscriber | \n", "1993.0 | \n", "1 | \n", "0 | \n", "
4 | \n", "2693 | \n", "2016-10-01 00:00:21 | \n", "2016-10-01 00:45:15 | \n", "3428 | \n", "8 Ave & W 16 St | \n", "40.740983 | \n", "-74.001702 | \n", "3323 | \n", "W 106 St & Central Park West | \n", "40.798186 | \n", "-73.960591 | \n", "26502 | \n", "Subscriber | \n", "1991.0 | \n", "1 | \n", "0 | \n", "