{ "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": 1, "metadata": {}, "outputs": [], "source": [ "import pysal.esda.mapclassify as mapclassify\n", "import pandas as pd\n", "import sys\n", "import os\n", "sys.path.append(\"..\") # Adds higher directory to python modules path. Use to run this example locallay without a global pip install.\n", "\n", "from mapboxgl.viz import *\n", "from mapboxgl.utils import *\n", "from mapboxgl.colors import *" ] }, { "cell_type": "code", "execution_count": 2, "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", "
Avg Total PaymentsAvg Covered ChargesTotal DischargesAvg Medicare Paymentsadmin1_idProvider Idadmin2_idlonlat
08749.0335247.0358.757678.21USA10110001USA201069-85.3631.22
16812.1316451.0928.965793.63USA10110005USA201119-88.1432.45
28197.2436942.3645.367145.96USA10110006USA201077-87.6834.79
\n", "
" ], "text/plain": [ " Avg Total Payments Avg Covered Charges Total Discharges \\\n", "0 8749.03 35247.03 58.75 \n", "1 6812.13 16451.09 28.96 \n", "2 8197.24 36942.36 45.36 \n", "\n", " Avg Medicare Payments admin1_id Provider Id admin2_id lon lat \n", "0 7678.21 USA101 10001 USA201069 -85.36 31.22 \n", "1 5793.63 USA101 10005 USA201119 -88.14 32.45 \n", "2 7145.96 USA101 10006 USA201077 -87.68 34.79 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('../examples/points.csv').round(2)\n", "df.head(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set your Mapbox access token. \n", "### Set a `MAPBOX_ACCESS_TOKEN` environment variable or copy/paste your token from https://www.mapbox.com/studio/account/tokens/\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "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": 4, "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'].tolist(), 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(df_to_geojson(\n", " df, \n", " properties=['Avg Medicare Payments'],\n", " precision=4),\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": 5, "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 source data and color scale" ] }, { "cell_type": "code", "execution_count": 6, "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'].tolist(), k=8, initial=0).bins\n", "color_stops = create_color_stops(color_breaks, colors='YlOrRd')\n", "\n", "# Show the viz\n", "viz.data = df_to_geojson(df, \n", " properties=['Avg Covered Charges'],\n", " precision=4)\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": 7, "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": 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 Covered Charges'].tolist(), 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\"].tolist(), k=8, initial=0).bins\n", "radius_stops = create_radius_stops(radius_breaks, 1, 10)\n", "\n", "# Create the viz\n", "viz2 = GraduatedCircleViz(df_to_geojson(\n", " df, \n", " properties=['Avg Covered Charges', 'Avg Medicare Payments'],\n", " precision=4), \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": 9, "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'].tolist(), 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(df_to_geojson(\n", " df, \n", " properties=['Avg Medicare Payments'],\n", " precision=4), \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": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#Create a clustered circle map\n", "viz4 = ClusteredCircleViz(df_to_geojson(\n", " df, \n", " properties=['Avg Medicare Payments'],\n", " precision=4), \n", " access_token=token,\n", " color_stops = create_color_stops([1,10,50,100], colors='BrBG'),\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()" ] } ], "metadata": { "anaconda-cloud": { "attach-environment": true, "environment": "Root", "summary": "Mapboxgl Python Data Visualization example" }, "kernelspec": { "display_name": "Python [Root]", "language": "python", "name": "Python [Root]" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }