{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Mapboxgl Python Library for location data visualization\n", "\n", "https://github.com/mapbox/mapboxgl-jupyter\n", "\n", "\n", "# Add a map scale annotation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "| Property | Description | Example |\n", "|:--------- |:-------------|:--------|\n", "| scale | controls visibility of map scale annotation | True |\n", "| scale_position | controls the position of the map scale in the map controls pane | 'bottom-right' |\n", "| scale_background_color | string background color for scale | 'white' |\n", "| scale_border_color | string border color for scale | 'white' |\n", "| scale_text_color | string color for legend text | '#6e6e6e' |\n", "\n", " scale=True,\n", " scale_border_color='#eee',\n", " scale_background_color='red'," ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a visualization from example data file" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "import os\n", "from mapboxgl.utils import *\n", "from mapboxgl.viz import *\n", "\n", "# Set Mapbox Acces Token; Must be a public token, starting with `pk`\n", "token = os.getenv('MAPBOX_ACCESS_TOKEN')\n", "\n", "# 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", "\n", "# 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='YlOrRd')\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_width=0.2,\n", " center=(-95, 40),\n", " zoom=2.5,\n", " scale=True,\n", " scale_unit_system='imperial',\n", " below_layer='waterway-label',\n", " height='300px')\n", "\n", "# Show the viz\n", "viz.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Update scale to match Mapbox Dark-v9 style" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "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", "viz.height = '400px'\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 settings\n", "viz.scale_border_color = 'hsla(0, 0%, 10%, 0.75)'\n", "viz.scale_position = 'top-left'\n", "viz.scale_background_color = '#343332'\n", "viz.scale_text_color = 'hsl(0, 0%, 70%)'\n", "\n", "# Render map\n", "viz.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 }