Skip to content

Instantly share code, notes, and snippets.

@Zsailer
Last active August 29, 2019 19:48
Show Gist options
  • Select an option

  • Save Zsailer/5356d5a8281804f01a6fed493dc22589 to your computer and use it in GitHub Desktop.

Select an option

Save Zsailer/5356d5a8281804f01a6fed493dc22589 to your computer and use it in GitHub Desktop.

Revisions

  1. Zsailer revised this gist Aug 29, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion requirements.txt
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,3 @@
    altair
    vega_datasets
    vega_datasets
    jupyterlab>=1.0.1
  2. Zsailer created this gist Aug 29, 2019.
    109 changes: 109 additions & 0 deletions demo.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,109 @@
    {
    "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
    }
    2 changes: 2 additions & 0 deletions requirements.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    altair
    vega_datasets