{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Mapboxgl Python Library for location data visualization\n", "\n", "https://github.com/mapbox/mapboxgl-jupyter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example with Match-type Color Assignment (Categorical Data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "import pandas as pd\n", "import os\n", "\n", "from mapboxgl.viz import *\n", "from mapboxgl.utils import *" ] }, { "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": "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/cdec.csv'\n", "df = pd.read_csv(data_url)\n", "\n", "# Convert Elevation series to float\n", "df['Elevation (feet)'] = df['Elevation (feet)'].astype(float)\n", "\n", "# Clean up by dropping null rows\n", "df = df.dropna(axis=1, how='all')\n", "\n", "df.head(2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create geojson data object\n", "cdec_data = df_to_geojson(df.fillna(''), \n", " properties=['CDEC ID', 'CNRFC ID', 'Gage Type', 'Elevation (feet)'], \n", " precision=4) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Assign color stops\n", "category_color_stops = [['reservoir', 'rgb(211,47,47)'], \n", " ['river', 'rgb(81,45,168)'], \n", " ['snow', 'rgb(2,136,209)'], \n", " ['precip', 'rgb(139,195,74)'], \n", " ['temp', 'rgb(255,160,0)']]\n", "\n", "# Initialize CircleViz with Categorical Measure Data\n", "viz = CircleViz(cdec_data, \n", " access_token=token,\n", " height='500px',\n", " label_property='CDEC ID',\n", " color_property='Gage Type',\n", " color_default='grey',\n", " color_function_type='match',\n", " color_stops=category_color_stops,\n", " radius=2,\n", " center=(-121, 38.5),\n", " zoom=4.5)\n", "\n", "# Render map\n", "viz.show() " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Standard linear interpolation behavior" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Numeric color stops from ColorBrewer\n", "sample_color_stops = [[0.0, 'rgb(255,255,204)'],\n", " [100.0, 'rgb(255,237,160)'],\n", " [250.0, 'rgb(254,217,118)'],\n", " [500.0, 'rgb(254,178,76)'],\n", " [1000.0, 'rgb(253,141,60)'],\n", " [2000.0, 'rgb(252,78,42)'],\n", " [4000.0, 'rgb(227,26,28)'],\n", " [6000.0, 'rgb(189,0,38)'],\n", " [10000.0,'rgb(128,0,38)']]\n", "\n", "# Select temperature gage records with numeric elevation data\n", "temperature_df = df[df['Gage Type']=='temp']\n", "temperature_data = df_to_geojson(temperature_df, \n", " properties=['CDEC ID', 'Elevation (feet)'])\n", "\n", "# Test CircleViz with interval measure data\n", "viz2 = CircleViz(temperature_data, \n", " access_token=token, \n", " height='400px',\n", " color_property='Elevation (feet)', \n", " color_function_type='interpolate', \n", " color_stops=sample_color_stops, \n", " radius=2, \n", " center=(-121, 37.5), \n", " zoom=4.5, \n", " below_layer='waterway-label',\n", " legend_key_shape='contiguous-bar',\n", " legend_key_borders_on=False)\n", "\n", "# Render map\n", "viz2.show() " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Combination of match-type and interpolate-type color and radius assignment" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Radius stops for linear interpolation\n", "sample_radius_stops = [[0.0, 1.0],\n", " [100.0, 2.0],\n", " [500.0, 3.0],\n", " [1000.0, 4.0],\n", " [5000.0, 5.0],\n", " [10000.0, 6.0]]\n", "\n", "# Initialize Graduated Circle Visualization \n", "viz3 = GraduatedCircleViz(cdec_data, \n", " access_token=token, \n", " height='400px',\n", " color_function_type='match', \n", " color_stops=category_color_stops, \n", " color_property='Gage Type', \n", " color_default='grey', \n", " opacity=0.5, \n", " radius_property='Elevation (feet)', \n", " radius_stops=sample_radius_stops, \n", " radius_function_type='interpolate', \n", " radius_default=2, \n", " center=(-121, 37.5), \n", " zoom=4.5, \n", " below_layer='waterway-label')\n", "\n", "# Render map\n", "viz3.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Use match function for both color and radius" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Radius stops for linear interpolation\n", "category_radius_stops = [['reservoir', 3], \n", " ['river', 5], \n", " ['snow', 8], \n", " ['precip', 11], \n", " ['temp', 14]]\n", "\n", "# Initialize Graduated Circle Visualization \n", "viz4 = GraduatedCircleViz(cdec_data, \n", " access_token=token, \n", " height='400px',\n", " color_function_type='match',\n", " color_stops=category_color_stops,\n", " color_property='Gage Type',\n", " color_default='grey',\n", " opacity=0.5,\n", " radius_property='Elevation (feet)',\n", " radius_stops=category_radius_stops,\n", " radius_function_type='match',\n", " radius_default=2,\n", " center=(-121, 37.5),\n", " zoom=4.5,\n", " below_layer='waterway-label')\n", "\n", "# Render map\n", "viz4.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }