diff --git a/examples/notebooks/dataframe-accessor-example.ipynb b/examples/notebooks/dataframe-accessor-example.ipynb new file mode 100644 index 0000000..252dca8 --- /dev/null +++ b/examples/notebooks/dataframe-accessor-example.ipynb @@ -0,0 +1,140 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Mapboxgl Python Library for location data visualization\n", + "\n", + "https://github.com/mapbox/mapboxgl-jupyter\n", + "\n", + "## Maps using Pandas DataFrame accessors\n", + "requires Pandas > 0.23" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import os\n", + "from mapboxgl.utils import *\n", + "from mapboxgl.viz import *\n", + "\n", + "# Set Mapbox public token, starting with `pk`\n", + "token = os.getenv('MAPBOX_ACCESS_TOKEN')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Define Accessor with `mapbox` namespace" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "@pd.api.extensions.register_dataframe_accessor('mapbox')\n", + "class GeoAccessor(object):\n", + " \"\"\"\n", + " https://pandas.pydata.org/pandas-docs/stable/extending.html\n", + " \"\"\"\n", + " \n", + " def __init__(self, pandas_obj):\n", + " self._obj = pandas_obj\n", + "\n", + " @property\n", + " def center(self):\n", + " # return the geographic center point of this DataFrame\n", + " lat = self._obj.lat\n", + " lon = self._obj.lon\n", + " return (float(lon.mean()), float(lat.mean()))\n", + "\n", + " def draw_map(self, color_property, color_stops):\n", + " \n", + " data = df_to_geojson(df, \n", + " properties=['Avg Medicare Payments', \n", + " 'Avg Covered Charges', \n", + " 'date'], \n", + " lat='lat', \n", + " lon='lon', \n", + " precision=3)\n", + " \n", + " viz = CircleViz(data, \n", + " access_token=token, \n", + " color_property=color_property,\n", + " color_stops=color_stops,\n", + " radius=2, \n", + " center=(-95, 40), \n", + " zoom=3)\n", + " \n", + " viz.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Create a visualization from a Pandas dataframe" + ] + }, + { + "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", + "\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='YlGnBu')\n", + "\n", + "# Make map with dataframe accessor\n", + "df.mapbox.draw_map(measure, color_stops)" + ] + }, + { + "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 +}