{ "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", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import os\n", "from mapboxgl.utils import *\n", "from mapboxgl.viz 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", "
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
\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", "\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", "\n", " date \n", "0 1/1/14 12:00 AM \n", "1 1/2/14 12:00 AM " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Load data from sample csv\n", "data_url = 'https://raw.githubusercontent.com/mapbox/mapboxgl-jupyter/master/examples/data/points.csv'\n", "df = pd.read_csv(data_url).round(3)\n", "df.head(2)" ] }, { "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": 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/plain": [ "{'feature_count': 3173,\n", " 'filename': '../data/healthcare_points.geojson',\n", " 'type': 'file'}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a geojson file export from the current dataframe\n", "df_to_geojson(df, filename='../data/healthcare_points.geojson',\n", " properties=['Avg Medicare Payments', 'Avg Covered Charges', 'date'], \n", " lat='lat', lon='lon', precision=3)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Just show a map of the data\n", "viz = CircleViz('../data/healthcare_points.geojson', access_token=token, \n", " radius = 2, center = (-95, 40), zoom = 3)\n", "viz.show()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Generate data breaks using numpy quantiles and color stops from colorBrewer\n", "measure = 'Avg Medicare Payments'\n", "color_breaks = [round(df[measure].quantile(q=x*0.1), 2) for x in range(1,9)]\n", "color_stops = create_color_stops(color_breaks, colors='YlGnBu')\n", "\n", "# Create the viz from the dataframe\n", "viz = CircleViz('../data/healthcare_points.geojson',\n", " access_token=token, \n", " color_property = \"Avg Medicare Payments\",\n", " color_stops = color_stops,\n", " radius = 2.5,\n", " stroke_color = 'black',\n", " stroke_width = 0.2,\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": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "viz.label_property = \"Avg Medicare Payments\"\n", "viz.stroke_width = 0\n", "viz.label_size = 8\n", "viz.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Change viz data property and color scale" ] }, { "cell_type": "code", "execution_count": 8, "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", "measure = 'Avg Covered Charges'\n", "color_breaks = [round(df[measure].quantile(q=x*0.1), 1) for x in range(1,9)]\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": 9, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "viz.style='mapbox://styles/mapbox/dark-v9?optimize=true'\n", "viz.label_color = 'hsl(0, 0%, 70%)'\n", "viz.label_halo_color = 'hsla(0, 0%, 10%, 0.75)'\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": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Generate data breaks and color stops from colorBrewer\n", "measure_color = 'Avg Covered Charges'\n", "color_breaks = [round(df[measure_color].quantile(q=x*0.1), 2) for x in range(2, 10)]\n", "color_stops = create_color_stops(color_breaks, colors='Blues')\n", "\n", "# Generate radius breaks from data domain and circle-radius range\n", "measure_radius = 'Avg Medicare Payments'\n", "radius_breaks = [round(df[measure_radius].quantile(q=x*0.1), 2) for x in range(2,10)]\n", "radius_stops = create_radius_stops(radius_breaks, 0.5, 10)\n", "\n", "# Create the viz\n", "viz2 = GraduatedCircleViz('../data/healthcare_points.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", " stroke_color = 'black',\n", " stroke_width = 0.5,\n", " center = (-95, 40),\n", " zoom = 3,\n", " opacity=0.75,\n", " below_layer = 'waterway-label')\n", "\n", "viz2.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a heatmap viz" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#Create a heatmap \n", "measure = 'Avg Medicare Payments'\n", "heatmap_color_stops = create_color_stops([0.01, 0.25, 0.5, 0.75, 1], colors='RdPu')\n", "heatmap_radius_stops = [[0, 3], [14, 100]] #increase radius with zoom\n", "\n", "color_breaks = [round(df[measure].quantile(q=x*0.1), 2) for x in range(2,10)]\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('../data/healthcare_points.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.8,\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": 12, "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, 25, 50, 75, 100], colors='YlOrBr')\n", "\n", "viz4 = ClusteredCircleViz('../data/healthcare_points.geojson', \n", " access_token=token,\n", " color_stops = color_stops,\n", " stroke_color = 'black',\n", " radius_stops = [[1,5], [10, 10], [50, 15], [100, 20]],\n", " radius_default = 2,\n", " cluster_maxzoom = 10,\n", " cluster_radius = 30,\n", " label_size = 12,\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": 13, "metadata": {}, "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": "Conda Py3", "language": "python", "name": "myenv" }, "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.6.3" } }, "nbformat": 4, "nbformat_minor": 1 }