{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Mapboxgl Python Library\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 sys\n", "import os\n", "\n", "from mapboxgl.viz import *\n", "from mapboxgl.utils import *\n", "from mapboxgl.colors import *\n", "\n", "# Must be a public token, starting with `pk`\n", "token = os.getenv('MAPBOX_ACCESS_TOKEN')\n", "\n", "# Load sample gage data (https://cdec.water.ca.gov/cgi-progs/staSearch)\n", "df = pd.read_csv('../examples/cdec.csv')\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", "# Create geojson data object\n", "data = df_to_geojson(df, properties=['CDEC ID', 'CNRFC ID', 'Gage Type', 'Elevation (feet)'],)\n", "\n", "# Assign color stops\n", "category_color_stops = [\n", " ['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", "\n", "# Initialize CircleViz with Categorical Measure Data\n", "viz = CircleViz(data, access_token=token, height='500px')\n", "viz.label_property = 'CDEC ID'\n", "\n", "# Marker color-related attributes\n", "viz.color_property = 'Gage Type'\n", "viz.default_color = 'grey'\n", "viz.color_function_type = 'match'\n", "viz.color_stops = category_color_stops\n", "\n", "# Set center, zoom and render map\n", "viz.center = (-121, 38.5)\n", "viz.zoom = 4.5\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 = [\n", " [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", "\n", "# Select temperature gage records with numeric elevation data\n", "temperature_df = df[df['Gage Type']=='temp']\n", "data = df_to_geojson(temperature_df, properties=['CDEC ID', 'Elevation (feet)'],)\n", "\n", "# Test CircleViz with interval measure data\n", "viz = CircleViz(data, access_token=token, height='400px')\n", "\n", "# Marker color-related attributes\n", "viz.color_property = 'Elevation (feet)'\n", "viz.color_function_type = 'interpolate'\n", "viz.color_stops = sample_color_stops\n", "\n", "# Set center, zoom and render map\n", "viz.center = (-121, 37.5)\n", "viz.zoom = 4.5\n", "viz.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": [ "# create geojson data object\n", "data = df_to_geojson(df, properties=['CDEC ID', 'Elevation (feet)', 'Gage Type'],)\n", "\n", "# Radius stops for linear interpolation\n", "sample_radius_stops = [\n", " [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", "\n", "# Initialize Graduated Circle Visualization \n", "viz2 = GraduatedCircleViz(data, access_token=token, height='400px')\n", "# viz2.label_property = 'CDEC ID'\n", "\n", "# Marker color-related attributes\n", "viz2.color_function_type = 'match'\n", "viz2.color_stops = category_color_stops\n", "viz2.color_property = 'Gage Type'\n", "viz2.default_color = 'grey'\n", "viz2.opacity = 0.5\n", "\n", "# Marker radius-related attributes\n", "viz2.radius_property = 'Elevation (feet)'\n", "viz2.radius_stops = sample_radius_stops\n", "viz2.radius_function_type = 'interpolate'\n", "\n", "# Set center, zoom and render map\n", "viz2.center = (-121, 37.5)\n", "viz2.zoom = 4.5\n", "viz2.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 }