{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Mapboxgl Python Library for location data visualizaiton\n", "\n", "https://github.com/mapbox/mapboxgl-jupyter\n", "\n", "### Requirements\n", "\n", "These examples require the installation of the following python modules\n", "\n", "```\n", "pip install mapboxgl\n", "pip install pandas\n", "pip install pysal\n", "```" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pysal.esda.mapclassify as mapclassify\n", "import pandas as pd\n", "import os\n", "from mapboxgl.utils import *\n", "from mapboxgl.viz import *" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Avg Total PaymentsAvg Covered ChargesTotal DischargesAvg Medicare Paymentsadmin1_idProvider Idadmin2_idlonlatdate
08749.02535247.02858.7507678.214USA10110001USA201069-85.36331.2161/1/14 12:00 AM
16812.13116451.09228.9595793.631USA10110005USA201119-88.14332.4531/2/14 12:00 AM
28197.23836942.35745.3607145.960USA10110006USA201077-87.68334.7941/3/14 12:00 AM
34860.82912079.53727.4094047.025USA10110007USA201039-86.25531.2921/4/14 12:00 AM
45898.13716148.75217.8894963.548USA10110008USA201041-86.26531.6941/5/14 12:00 AM
\n", "
" ], "text/plain": [ " Avg Total Payments Avg Covered Charges Total Discharges \\\n", "0 8749.025 35247.028 58.750 \n", "1 6812.131 16451.092 28.959 \n", "2 8197.238 36942.357 45.360 \n", "3 4860.829 12079.537 27.409 \n", "4 5898.137 16148.752 17.889 \n", "\n", " Avg Medicare Payments admin1_id Provider Id admin2_id lon lat \\\n", "0 7678.214 USA101 10001 USA201069 -85.363 31.216 \n", "1 5793.631 USA101 10005 USA201119 -88.143 32.453 \n", "2 7145.960 USA101 10006 USA201077 -87.683 34.794 \n", "3 4047.025 USA101 10007 USA201039 -86.255 31.292 \n", "4 4963.548 USA101 10008 USA201041 -86.265 31.694 \n", "\n", " date \n", "0 1/1/14 12:00 AM \n", "1 1/2/14 12:00 AM \n", "2 1/3/14 12:00 AM \n", "3 1/4/14 12:00 AM \n", "4 1/5/14 12:00 AM " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Load data from sample csv\n", "data_url = 'https://raw.githubusercontent.com/mapbox/mapboxgl-jupyter/master/examples/points.csv'\n", "df = pd.read_csv(data_url).round(3)\n", "df.head(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set your Mapbox access token. \n", "Set a `MAPBOX_ACCESS_TOKEN` environment variable or copy/paste your token \n", "If you do not have a Mapbox access token, sign up for an account at https://www.mapbox.com/ \n", "If you already have an account, you can grab your token at https://www.mapbox.com/account/" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Must be a public token, starting with `pk`\n", "token = os.getenv('MAPBOX_ACCESS_TOKEN')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a visualization from a Pandas dataframe" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'feature_count': 3173, 'filename': 'points1.geojson', 'type': 'file'}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a geojson file export from the current dataframe\n", "df_to_geojson(df, filename='points1.geojson',\n", " properties=['Avg Medicare Payments', 'Avg Covered Charges', 'date'], \n", " lat='lat', lon='lon', precision=3)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Generate data breaks and color stops from colorBrewer\n", "color_breaks = mapclassify.Natural_Breaks(df['Avg Medicare Payments'], k=8, initial=0).bins\n", "color_stops = create_color_stops(color_breaks, colors='YlGnBu')\n", "\n", "# Create the viz from the dataframe\n", "viz = CircleViz('points1.geojson',\n", " access_token=token, \n", " height='400px',\n", " color_property = \"Avg Medicare Payments\",\n", " color_stops = color_stops,\n", " center = (-95, 40),\n", " zoom = 3,\n", " below_layer = 'waterway-label')\n", "\n", "viz.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add labels to the viz" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "viz.label_property = \"Avg Medicare Payments\"\n", "viz.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Change viz data property and color scale" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Generate a new data domain breaks and a new color palette from colorBrewer2\n", "color_breaks = mapclassify.Natural_Breaks(df['Avg Covered Charges'], k=8, initial=0).bins\n", "color_stops = create_color_stops(color_breaks, colors='YlOrRd')\n", "\n", "# Show the viz\n", "viz.color_property='Avg Covered Charges'\n", "viz.color_stops=color_stops\n", "viz.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Change the viz map style" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "viz.style_url='mapbox://styles/mapbox/dark-v9?optimize=true'\n", "viz.show()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Create a graduated cricle viz based on two data properties" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Generate data breaks and color stops from colorBrewer\n", "color_breaks = mapclassify.Natural_Breaks(df['Avg Covered Charges'], k=8, initial=0).bins\n", "color_stops = create_color_stops(color_breaks, colors='Spectral')\n", "\n", "# Generate radius breaks from data domain and circle-radius range\n", "radius_breaks = mapclassify.Natural_Breaks(df[\"Avg Medicare Payments\"], k=8, initial=0).bins\n", "radius_stops = create_radius_stops(radius_breaks, 1, 10)\n", "\n", "# Create the viz\n", "viz2 = GraduatedCircleViz('points1.geojson', \n", " access_token=token,\n", " color_property = \"Avg Covered Charges\",\n", " color_stops = color_stops,\n", " radius_property = \"Avg Medicare Payments\",\n", " radius_stops = radius_stops,\n", " center = (-95, 40),\n", " zoom = 3,\n", " below_layer = 'waterway-label')\n", "\n", "viz2.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a heatmap viz" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#Create a heatmap \n", "heatmap_color_stops = create_color_stops([0.01,0.25,0.5,0.75,1], colors='RdPu')\n", "heatmap_radius_stops = [[0,1], [15, 40]] #increase radius with zoom\n", "\n", "color_breaks = mapclassify.Natural_Breaks(df['Avg Medicare Payments'], k=8, initial=0).bins\n", "color_stops = create_color_stops(color_breaks, colors='Spectral')\n", "\n", "heatmap_weight_stops = create_weight_stops(color_breaks)\n", "\n", "#Create a heatmap \n", "viz3 = HeatmapViz('points1.geojson', \n", " access_token=token,\n", " weight_property = \"Avg Medicare Payments\",\n", " weight_stops = heatmap_weight_stops,\n", " color_stops = heatmap_color_stops,\n", " radius_stops = heatmap_radius_stops,\n", " opacity = 0.9,\n", " center = (-95, 40),\n", " zoom = 3,\n", " below_layer='waterway-label'\n", " )\n", "\n", "viz3.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a clustered circle map" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#Create a clustered circle map\n", "color_stops = create_color_stops([1,10,50,100], colors='BrBG')\n", "\n", "viz4 = ClusteredCircleViz('points1.geojson', \n", " access_token=token,\n", " color_stops = color_stops,\n", " radius_stops = [[1,5], [10, 10], [50, 15], [100, 20]],\n", " cluster_maxzoom = 10,\n", " cluster_radius = 30,\n", " opacity = 0.9,\n", " center = (-95, 40),\n", " zoom = 3\n", " )\n", "\n", "viz4.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Save our viz to an HTML file for distribution\n", "### Note\n", "Viz export contains a reference to the data in this visualization. Serve data from the same directory as the HTML file to vis your visualization." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "with open('viz4.html', 'w') as f:\n", " f.write(viz4.create_html())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run exported HTML example\n", "\n", "Python2: `python -m SimpleHTTPServer 8080`\n", "\n", "Python3: `python3 -m http.server 8080`\n", "\n", "Now navigate your browser to `http://localhost:8080/viz4.html` to see the viz" ] } ], "metadata": { "anaconda-cloud": { "attach-environment": true, "environment": "Root", "summary": "Mapboxgl Python Data Visualization example" }, "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 1 }