Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional notebook examples for pydeck #3625

Merged
merged 1 commit into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 14 additions & 15 deletions bindings/python/pydeck/examples/02 - Scatterplots.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"\n",
"*(About 5-10 minutes to read)*\n",
"\n",
"Since 1978, China's GDP has nearly doubled every seven years [[1](https://en.wikipedia.org/wiki/Historical_GDP_of_China)]. That sort of exponential growth has led to rapid internal change within the country—as demonstrated in part by the rapid changes within Beijing's urban infrastructure.\n",
"Since 1978, China's GDP has nearly doubled every seven years [[Wikipedia](https://en.wikipedia.org/wiki/Historical_GDP_of_China)]. That sort of exponential growth has led to rapid internal change within the country—as demonstrated in part by the rapid changes within Beijing's urban infrastructure.\n",
"\n",
"Below we'll plot the location of Beijing subway stops over time. Locations for subway stops come from [Wikipedia](https://en.wikipedia.org/wiki/List_of_Beijing_Subway_stations) and [OpenStreetMap](https://wiki.openstreetmap.org/wiki/Beijing_Subway). Do note that this is not intended to be a rigorous study, so some stops may be missing.\n",
"\n",
Expand Down Expand Up @@ -36,8 +36,6 @@
"metadata": {},
"outputs": [],
"source": [
"from ast import literal_eval\n",
"\n",
"import pandas as pd\n",
"from pydeck import (\n",
" data_utils,\n",
Expand All @@ -57,10 +55,7 @@
"source": [
"## Data cleaning\n",
"\n",
"Next, we'll have to engage in some necessary data housekeeping:\n",
"\n",
"1. We have to re-code position to be one field and in a list.\n",
"2. The CSV encodes the `[R, G, B, A]` color values a `str`, and `literal_eval` lets us convert that string a list."
"Next, we'll have to engage in some necessary data housekeeping. The CSV encodes the `[R, G, B, A]` color values a `str`, and `literal_eval` lets us convert that string a list."
]
},
{
Expand All @@ -69,8 +64,8 @@
"metadata": {},
"outputs": [],
"source": [
"from literal_eval import ast\n",
"# We have to re-code position to be one field in a list, so we'll do that here:\n",
"df['position'] = df.apply(lambda x: [x['lng'], x['lat']], axis=1)\n",
"# The CSV encodes the [R, G, B, A] color values listed in it as a string\n",
"df['color'] = df.apply(lambda x: literal_eval(x['color']), axis=1)"
]
Expand All @@ -92,7 +87,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Use pydeck's data_utils module to fit a viewport to the data\n",
"# Use pydeck's data_utils module to fit a viewport to the central 90% of the data\n",
"viewport = data_utils.compute_viewport(points=df[['lng', 'lat']], view_proportion=0.9)\n",
"auto_zoom_map = Deck(layers=None, initial_view_state=viewport)\n",
"auto_zoom_map.show()"
Expand Down Expand Up @@ -121,9 +116,9 @@
"\n",
"\n",
"\n",
"**We can specify the layer type as the first argument, the data as the second, and the layer arguments as keywords.** **[`ScatterplotLayer`](https://github.com/uber/deck.gl/blob/master/docs/layers/scatterplot-layer.md)** is one of a list of layers available in the deck.gl core library. We'll also provide a header to list the year using some built-in Jupyter notebook tools.\n",
"**We can specify the layer type as the first argument, the data as the second, and the layer arguments as keywords.** **[ScatterplotLayer](https://github.com/uber/deck.gl/blob/master/docs/layers/scatterplot-layer.md)** is one of a list of layers available in the deck.gl core library. We'll also provide a header to list the year using some built-in Jupyter notebook tools.\n",
"\n",
"As aside, for a list of other layers, see the [deck.gl documentation](https://github.com/uber/deck.gl/tree/master/docs/layers#deckgl-layer-catalog-overview). Remember that deck.gl is a JavaScript library and not a Python one, so the documentation may differ for some kinds of terminology and functionality (e.g., pydeck doesn't support passing functions as arguments but this is a common occurrence within deck.gl)."
"For a list of other layers, see the [deck.gl documentation](https://github.com/uber/deck.gl/tree/master/docs/layers#deckgl-layer-catalog-overview). Remember that deck.gl is a JavaScript library and not a Python one, so the documentation may differ for some kinds of terminology and functionality (e.g., pydeck doesn't support passing functions as arguments but this is a common occurrence within deck.gl)."
]
},
{
Expand All @@ -145,10 +140,13 @@
" id='scatterplot-layer',\n",
" radius=500,\n",
" get_fill_color='color',\n",
" get_position='position')\n",
" get_position='[lng, lat]')\n",
"r = Deck(layers=[scatterplot], initial_view_state=viewport)\n",
"display_el = ipywidgets.HTML('<h1>' + str(year) + '</h1>')\n",
"\n",
"# Create an HTML header to display the year\n",
"display_el = ipywidgets.HTML('<h1>{}</h1>'.format(year))\n",
"display(display_el)\n",
"# Show the current visualization\n",
"r.show()"
]
},
Expand All @@ -171,7 +169,8 @@
"for y in range(1971, 2020):\n",
" scatterplot.data = df[df['opening_date'] <= str(y)]\n",
" year = y\n",
" display_el.value = '<h1>' + str(year) + '</h1>'\n",
" # Reset the header to display the year\n",
" display_el.value = '<h1>{}</h1>'.format(year)\n",
" r.update()\n",
" time.sleep(0.2)"
]
Expand All @@ -193,7 +192,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.7.4"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Selecting data for use in Python: Uber pickup data\n",
"\n",
"*(About 5-10 minutes to read)*\n",
"\n",
"Often it's easier to use a visual application to draw a shape than define a geometry in code. Here we'll explore how pydeck can be used to select data and pass that selected data back to the Jupyter kernel for use in Pandas.\n",
"\n",
"## Contents\n",
"\n",
"- [Getting the data](#Getting-the-data)\n",
"- [Plotting the data](#Plotting-the-data)\n",
"- [Interaction](#Interaction)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Getting the data\n",
"\n",
"Here we'll use a set of Uber pickup locations made available by a Freedom of Information Act request in 2014."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pydeck import (\n",
" data_utils,\n",
" Deck,\n",
" Layer\n",
")\n",
"\n",
"\n",
"DATA_URL = 'https://raw.githubusercontent.com/uber-common/deck.gl-data/master/examples/screen-grid/uber-pickup-locations.json'\n",
"\n",
"COLOR_RANGE = [\n",
" [255, 255, 178, 25],\n",
" [254, 217, 118, 85],\n",
" [254, 178, 76, 127],\n",
" [253, 141, 60, 170],\n",
" [240, 59, 32, 212],\n",
" [189, 0, 38, 255]\n",
"]\n",
"\n",
"viewport = View(longitude=-73.998, latitude=40.729, zoom=10)\n",
"\n",
"layer = pdk.Layer(\n",
" 'ScreenGridLayer',\n",
" UK_ACCIDENTS_DATA,\n",
" cell_size_pixels=20,\n",
" color_range=COLOR_RANGE,\n",
" get_position='[row[0], row[1]]',\n",
" get_weight='row[2]',\n",
" pickable=True,\n",
" auto_highlight=True)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plotting the data\n",
"\n",
"As we've done in previous examples, we'll call `r.show()` to view the data:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r = pdk.Deck(layers=[layer], viewport=viewport)\n",
"r.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pd.DataFrame(s.selected_data).mean()"
]
}
],
"metadata": {
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}