{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Mapboxgl Python Library for location data visualization\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": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "import pandas as pd\n", "import os\n", "from mapboxgl.utils import *\n", "from mapboxgl.viz import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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": null, "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": null, "metadata": {}, "outputs": [], "source": [ "# Create a geojson Feature Collection from the current dataframe\n", "# optionally create a geojson file export by including the argument \n", "# filename='../data/healthcare_points.geojson',\n", "geodata = df_to_geojson(df, \n", " properties=['Avg Medicare Payments', 'Avg Covered Charges', 'date'], \n", " lat='lat', \n", " lon='lon', \n", " precision=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Just show a map of the data\n", "viz = CircleViz(geodata, \n", " access_token=token, \n", " radius=2, \n", " center=(-95, 40), \n", " zoom=3)\n", "viz.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add data-driven styles and a legend to the viz" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "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(geodata,\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", "viz.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add a scale bar to the viz" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "viz.scale = True\n", "viz.scale_unit_system = 'imperial'\n", "viz.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add labels to the viz" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "viz.label_property = \"Avg Medicare Payments\"\n", "viz.stroke_width = 0\n", "viz.label_size = 8\n", "viz.legend_text_numeric_precision = 2\n", "viz.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Change viz data property and color scale" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "# Map settings\n", "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", "\n", "# Legend settings\n", "viz.legend_gradient = False\n", "viz.legend_fill = '#343332'\n", "viz.legend_header_fill = '#343332'\n", "viz.legend_text_color = 'hsl(0, 0%, 70%)'\n", "viz.legend_key_borders_on = False\n", "viz.legend_title_halo_color = 'hsla(0, 0%, 10%, 0.75)'\n", "\n", "# Scale bar settings\n", "viz.scale = True\n", "viz.scale_border_color = 'hsla(0, 0%, 10%, 0.75)'\n", "viz.scale_position = 'bottom-left'\n", "viz.scale_background_color = '#343332'\n", "viz.scale_text_color = 'hsl(0, 0%, 70%)'\n", "\n", "# Render map\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": null, "metadata": {}, "outputs": [], "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(geodata, \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", "viz2.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a heatmap viz" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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 the heatmap \n", "viz3 = HeatmapViz(geodata, \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", "viz3.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a clustered circle map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create a clustered circle map\n", "color_stops = create_color_stops([1, 10, 25, 50, 75, 100], colors='YlOrBr')\n", "\n", "viz4 = ClusteredCircleViz(geodata, \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", "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": null, "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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Download the map canvas to a PNG file\n", "### Note\n", "Screen captures of the map and legend are separate; additional image processing is required to export a map with visible legend and other Mapbox control elements. Click either the `Map` or `Legend` links to download the corresponding PNG file." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "viz4.add_snapshot_links = True\n", "viz4.show()" ] } ], "metadata": { "anaconda-cloud": { "attach-environment": true, "environment": "Root", "summary": "Mapboxgl Python Data Visualization example" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.1" } }, "nbformat": 4, "nbformat_minor": 1 }