{ "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": 8, "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 *" ] }, { "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": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Must be a public token, starting with `pk`\n", "token = os.getenv('MAPBOX_ACCESS_TOKEN')" ] }, { "cell_type": "code", "execution_count": 10, "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", " \n", " \n", " \n", " \n", " \n", " \n", "
STATIONNAMECDEC IDUSGS IDUSACE IDCNRFC IDlatlonElevation (feet)RIVER_BASINCOUNTYOPERATORGage Type
0EL RIO (RIO MESA HIGH SCHOOL)ELHNaNNaNNaN34.2523-119.1430.0VENTURA RVENTURAVentura Countytemp
1GOLDEN GATEGGTNaNNaNNaN37.8100-122.4740.0SF BAYSAN FRANCISCONational Weather Servicetemp
\n", "
" ], "text/plain": [ " STATIONNAME CDEC ID USGS ID USACE ID CNRFC ID lat \\\n", "0 EL RIO (RIO MESA HIGH SCHOOL) ELH NaN NaN NaN 34.2523 \n", "1 GOLDEN GATE GGT NaN NaN NaN 37.8100 \n", "\n", " lon Elevation (feet) RIVER_BASIN COUNTY \\\n", "0 -119.143 0.0 VENTURA R VENTURA \n", "1 -122.474 0.0 SF BAY SAN FRANCISCO \n", "\n", " OPERATOR Gage Type \n", "0 Ventura County temp \n", "1 National Weather Service temp " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Load data from sample csv\n", "data_url = 'https://raw.githubusercontent.com/mapbox/mapboxgl-jupyter/master/examples/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": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'feature_count': 2353, 'filename': 'cdec.geojson', 'type': 'file'}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create geojson data object\n", "df_to_geojson(\n", " df.fillna(''), \n", " filename=\"cdec.geojson\",\n", " properties=['CDEC ID', 'CNRFC ID', 'Gage Type', 'Elevation (feet)'],\n", " precision=4\n", ") " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# 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('cdec.geojson', 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": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "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": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "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 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 1 }