{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Activity 8 - Interaction and Animation\n", "\n", "We saw in our previous notebook how to create visualisations using matplotlib, seaborn, and pandas - however, in most cases these were static. We did get some interaction through the 3D plots and through using the Plotly library. Here, we are going to pursue this further, and look at how we can incorporate interaction and animation into our tools.\n", "\n", "In particular, interaction techniques are useful for creatng dashboards (visual analytics) interfaces. Animations can be useful for conveying the story, by linking multiple still images together or by showing the analytical process.\n", "\n", "In this example notebook, we demonstrate how to create interactive widgets that can be used to filter data from a dataframe, either based on range selection or by item selection. We also demonstrate how an animated GIF can be constructed by taking frames of a plot to build an animation. We use a live data set based on COVID-19 to illustrate the concepts in this notebook.\n", "\n", "Further details are in the following blog post: https://towardsdatascience.com/interactive-controls-for-jupyter-notebooks-f5c94829aee6" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from datetime import datetime, timedelta\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: ipywidgets in c:\\python39\\lib\\site-packages (7.6.5)\n", "Requirement already satisfied: nbformat>=4.2.0 in c:\\python39\\lib\\site-packages (from ipywidgets) (5.1.3)\n", "Requirement already satisfied: widgetsnbextension~=3.5.0 in c:\\python39\\lib\\site-packages (from ipywidgets) (3.5.2)\n", "Requirement already satisfied: ipykernel>=4.5.1 in c:\\python39\\lib\\site-packages (from ipywidgets) (6.5.0)\n", "Requirement already satisfied: ipython-genutils~=0.2.0 in c:\\python39\\lib\\site-packages (from ipywidgets) (0.2.0)\n", "Requirement already satisfied: ipython>=4.0.0 in c:\\python39\\lib\\site-packages (from ipywidgets) (7.29.0)\n", "Requirement already satisfied: traitlets>=4.3.1 in c:\\python39\\lib\\site-packages (from ipywidgets) (5.1.1)\n", "Requirement already satisfied: jupyterlab-widgets>=1.0.0 in c:\\python39\\lib\\site-packages (from ipywidgets) (1.0.2)\n", "Requirement already satisfied: debugpy<2.0,>=1.0.0 in c:\\python39\\lib\\site-packages (from ipykernel>=4.5.1->ipywidgets) (1.5.1)\n", "Requirement already satisfied: tornado<7.0,>=4.2 in c:\\python39\\lib\\site-packages (from ipykernel>=4.5.1->ipywidgets) (6.1)\n", "Requirement already satisfied: matplotlib-inline<0.2.0,>=0.1.0 in c:\\python39\\lib\\site-packages (from ipykernel>=4.5.1->ipywidgets) (0.1.3)\n", "Requirement already satisfied: jupyter-client<8.0 in c:\\python39\\lib\\site-packages (from ipykernel>=4.5.1->ipywidgets) (7.0.6)\n", "Requirement already satisfied: decorator in c:\\python39\\lib\\site-packages (from ipython>=4.0.0->ipywidgets) (4.4.2)\n", "Requirement already satisfied: pygments in c:\\python39\\lib\\site-packages (from ipython>=4.0.0->ipywidgets) (2.10.0)\n", "Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in c:\\python39\\lib\\site-packages (from ipython>=4.0.0->ipywidgets) (3.0.22)\n", "Requirement already satisfied: setuptools>=18.5 in c:\\python39\\lib\\site-packages (from ipython>=4.0.0->ipywidgets) (57.4.0)\n", "Requirement already satisfied: backcall in c:\\python39\\lib\\site-packages (from ipython>=4.0.0->ipywidgets) (0.2.0)\n", "Requirement already satisfied: colorama in c:\\python39\\lib\\site-packages (from ipython>=4.0.0->ipywidgets) (0.4.4)\n", "Requirement already satisfied: jedi>=0.16 in c:\\python39\\lib\\site-packages (from ipython>=4.0.0->ipywidgets) (0.18.0)\n", "Requirement already satisfied: pickleshare in c:\\python39\\lib\\site-packages (from ipython>=4.0.0->ipywidgets) (0.7.5)\n", "Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in c:\\python39\\lib\\site-packages (from nbformat>=4.2.0->ipywidgets) (3.2.0)\n", "Requirement already satisfied: jupyter-core in c:\\python39\\lib\\site-packages (from nbformat>=4.2.0->ipywidgets) (4.9.1)\n", "Requirement already satisfied: notebook>=4.4.1 in c:\\python39\\lib\\site-packages (from widgetsnbextension~=3.5.0->ipywidgets) (6.4.5)\n", "Requirement already satisfied: parso<0.9.0,>=0.8.0 in c:\\python39\\lib\\site-packages (from jedi>=0.16->ipython>=4.0.0->ipywidgets) (0.8.2)\n", "Requirement already satisfied: six>=1.11.0 in c:\\python39\\lib\\site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets) (1.16.0)\n", "Requirement already satisfied: pyrsistent>=0.14.0 in c:\\python39\\lib\\site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets) (0.18.0)\n", "Requirement already satisfied: attrs>=17.4.0 in c:\\python39\\lib\\site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets) (21.2.0)\n", "Requirement already satisfied: nest-asyncio>=1.5 in c:\\python39\\lib\\site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets) (1.5.1)\n", "Requirement already satisfied: python-dateutil>=2.1 in c:\\python39\\lib\\site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets) (2.8.2)\n", "Requirement already satisfied: entrypoints in c:\\python39\\lib\\site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets) (0.3)\n", "Requirement already satisfied: pyzmq>=13 in c:\\python39\\lib\\site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets) (22.3.0)\n", "Requirement already satisfied: pywin32>=1.0 in c:\\python39\\lib\\site-packages (from jupyter-core->nbformat>=4.2.0->ipywidgets) (302)\n", "Requirement already satisfied: argon2-cffi in c:\\python39\\lib\\site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (21.1.0)\n", "Requirement already satisfied: terminado>=0.8.3 in c:\\python39\\lib\\site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.12.1)\n", "Requirement already satisfied: jinja2 in c:\\python39\\lib\\site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (3.0.3)\n", "Requirement already satisfied: Send2Trash>=1.5.0 in c:\\python39\\lib\\site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.8.0)\n", "Requirement already satisfied: prometheus-client in c:\\python39\\lib\\site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.12.0)\n", "Requirement already satisfied: nbconvert in c:\\python39\\lib\\site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (6.3.0)\n", "Requirement already satisfied: wcwidth in c:\\python39\\lib\\site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=4.0.0->ipywidgets) (0.2.5)\n", "Requirement already satisfied: pywinpty>=1.1.0 in c:\\python39\\lib\\site-packages (from terminado>=0.8.3->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.1.5)\n", "Requirement already satisfied: cffi>=1.0.0 in c:\\python39\\lib\\site-packages (from argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.15.0)\n", "Requirement already satisfied: MarkupSafe>=2.0 in c:\\python39\\lib\\site-packages (from jinja2->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (2.0.1)\n", "Requirement already satisfied: bleach in c:\\python39\\lib\\site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (4.1.0)\n", "Requirement already satisfied: jupyterlab-pygments in c:\\python39\\lib\\site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.1.2)\n", "Requirement already satisfied: defusedxml in c:\\python39\\lib\\site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.7.1)\n", "Requirement already satisfied: testpath in c:\\python39\\lib\\site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.5.0)\n", "Requirement already satisfied: mistune<2,>=0.8.1 in c:\\python39\\lib\\site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.8.4)\n", "Requirement already satisfied: pandocfilters>=1.4.1 in c:\\python39\\lib\\site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.5.0)\n", "Requirement already satisfied: nbclient<0.6.0,>=0.5.0 in c:\\python39\\lib\\site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.5.8)\n", "Requirement already satisfied: pycparser in c:\\python39\\lib\\site-packages (from cffi>=1.0.0->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (2.21)\n", "Requirement already satisfied: packaging in c:\\python39\\lib\\site-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (21.2)\n", "Requirement already satisfied: webencodings in c:\\python39\\lib\\site-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.5.1)\n", "Requirement already satisfied: pyparsing<3,>=2.0.2 in c:\\python39\\lib\\site-packages (from packaging->bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (2.4.7)\n" ] } ], "source": [ "!pip install ipywidgets" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "import ipywidgets as widgets\n", "from ipywidgets import interact, interact_manual" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Area name | \n", "Area code | \n", "Area type | \n", "Specimen date | \n", "Daily lab-confirmed cases | \n", "Cumulative lab-confirmed cases | \n", "Cumulative lab-confirmed cases rate | \n", "
---|---|---|---|---|---|---|---|
0 | \n", "Stoke-on-Trent | \n", "E06000021 | \n", "ltla | \n", "2021-04-26 | \n", "5 | \n", "20083 | \n", "7833.4 | \n", "
1 | \n", "Staffordshire Moorlands | \n", "E07000198 | \n", "ltla | \n", "2021-04-26 | \n", "5 | \n", "5351 | \n", "5436.1 | \n", "
2 | \n", "Hackney and City of London | \n", "E09000012 | \n", "ltla | \n", "2021-04-26 | \n", "5 | \n", "21672 | \n", "7451.5 | \n", "
3 | \n", "Staffordshire Moorlands | \n", "E07000198 | \n", "ltla | \n", "2021-04-25 | \n", "6 | \n", "5346 | \n", "5431.0 | \n", "
4 | \n", "Newark and Sherwood | \n", "E07000175 | \n", "ltla | \n", "2021-04-26 | \n", "4 | \n", "7292 | \n", "5956.5 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
45292 | \n", "Norfolk | \n", "E10000020 | \n", "utla | \n", "2021-01-13 | \n", "621 | \n", "28630 | \n", "3153.9 | \n", "
45293 | \n", "Norfolk | \n", "E10000020 | \n", "utla | \n", "2021-01-12 | \n", "671 | \n", "28009 | \n", "3085.5 | \n", "
45294 | \n", "Norfolk | \n", "E10000020 | \n", "utla | \n", "2021-01-11 | \n", "795 | \n", "27338 | \n", "3011.6 | \n", "
45295 | \n", "Norfolk | \n", "E10000020 | \n", "utla | \n", "2021-01-10 | \n", "504 | \n", "26543 | \n", "2924.0 | \n", "
45296 | \n", "Norfolk | \n", "E10000020 | \n", "utla | \n", "2021-01-09 | \n", "392 | \n", "26039 | \n", "2868.5 | \n", "
45297 rows × 7 columns
\n", "