Skip to content

Instantly share code, notes, and snippets.

@Zsailer
Last active August 29, 2019 19:48
Show Gist options
  • Save Zsailer/5356d5a8281804f01a6fed493dc22589 to your computer and use it in GitHub Desktop.
Save Zsailer/5356d5a8281804f01a6fed493dc22589 to your computer and use it in GitHub Desktop.
altair demo
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Seattle Weather Interactive\n",
"\n",
"This chart provides an interactive exploration of Seattle weather over the course of the year. It includes a one-axis brush selection to easily see the distribution of weather types in a particular date range."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import altair as alt\n",
"from vega_datasets import data\n",
"\n",
"source = data.seattle_weather()\n",
"source"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scale = alt.Scale(domain=['sun', 'fog', 'drizzle', 'rain', 'snow'],\n",
" range=['#e7ba52', '#a7a7a7', '#aec7e8', '#1f77b4', '#9467bd'])\n",
"color = alt.Color('weather:N', scale=scale)\n",
"\n",
"# We create two selections:\n",
"# - a brush that is active on the top panel\n",
"# - a multi-click that is active on the bottom panel\n",
"brush = alt.selection_interval(encodings=['x'])\n",
"click = alt.selection_multi(encodings=['color'])\n",
"\n",
"# Top panel is scatter plot of temperature vs time\n",
"points = alt.Chart().mark_point().encode(\n",
" alt.X('monthdate(date):T', title='Date'),\n",
" alt.Y('temp_max:Q',\n",
" title='Maximum Daily Temperature (C)',\n",
" scale=alt.Scale(domain=[-5, 40])\n",
" ),\n",
" color=alt.condition(brush, color, alt.value('lightgray')),\n",
" size=alt.Size('precipitation:Q', scale=alt.Scale(range=[5, 200]))\n",
").properties(\n",
" width=550,\n",
" height=300\n",
").add_selection(\n",
" brush\n",
").transform_filter(\n",
" click\n",
")\n",
"\n",
"# Bottom panel is a bar chart of weather type\n",
"bars = alt.Chart().mark_bar().encode(\n",
" x='count()',\n",
" y='weather:N',\n",
" color=alt.condition(click, color, alt.value('lightgray')),\n",
").transform_filter(\n",
" brush\n",
").properties(\n",
" width=550,\n",
").add_selection(\n",
" click\n",
")\n",
"\n",
"alt.vconcat(\n",
" points,\n",
" bars,\n",
" data=source,\n",
" title=\"Seattle Weather: 2012-2015\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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": 4
}
altair
vega_datasets
jupyterlab>=1.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment