{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "2021-06-15-tests.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "LeFKT-FS_HK_"
      },
      "source": [
        "## Develop an interactive visualisation with altair"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RDwrboyB_NaU"
      },
      "source": [
        "import pandas as pd\n",
        "import numpy as np\n",
        "import altair as alt"
      ],
      "execution_count": 16,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 202
        },
        "id": "yurcBAFo_uZK",
        "outputId": "c919d04d-ff59-4ca7-fafa-ff4c1f4705f3"
      },
      "source": [
        "url = \"https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv\"\n",
        "cars = pd.read_csv(url)\n",
        "cars.head()"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>model</th>\n",
              "      <th>mpg</th>\n",
              "      <th>cyl</th>\n",
              "      <th>disp</th>\n",
              "      <th>hp</th>\n",
              "      <th>drat</th>\n",
              "      <th>wt</th>\n",
              "      <th>qsec</th>\n",
              "      <th>vs</th>\n",
              "      <th>am</th>\n",
              "      <th>gear</th>\n",
              "      <th>carb</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>Mazda RX4</td>\n",
              "      <td>21.0</td>\n",
              "      <td>6</td>\n",
              "      <td>160.0</td>\n",
              "      <td>110</td>\n",
              "      <td>3.90</td>\n",
              "      <td>2.620</td>\n",
              "      <td>16.46</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>4</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>Mazda RX4 Wag</td>\n",
              "      <td>21.0</td>\n",
              "      <td>6</td>\n",
              "      <td>160.0</td>\n",
              "      <td>110</td>\n",
              "      <td>3.90</td>\n",
              "      <td>2.875</td>\n",
              "      <td>17.02</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>4</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>Datsun 710</td>\n",
              "      <td>22.8</td>\n",
              "      <td>4</td>\n",
              "      <td>108.0</td>\n",
              "      <td>93</td>\n",
              "      <td>3.85</td>\n",
              "      <td>2.320</td>\n",
              "      <td>18.61</td>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>4</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>Hornet 4 Drive</td>\n",
              "      <td>21.4</td>\n",
              "      <td>6</td>\n",
              "      <td>258.0</td>\n",
              "      <td>110</td>\n",
              "      <td>3.08</td>\n",
              "      <td>3.215</td>\n",
              "      <td>19.44</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>Hornet Sportabout</td>\n",
              "      <td>18.7</td>\n",
              "      <td>8</td>\n",
              "      <td>360.0</td>\n",
              "      <td>175</td>\n",
              "      <td>3.15</td>\n",
              "      <td>3.440</td>\n",
              "      <td>17.02</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "               model   mpg  cyl   disp   hp  ...   qsec  vs  am  gear  carb\n",
              "0          Mazda RX4  21.0    6  160.0  110  ...  16.46   0   1     4     4\n",
              "1      Mazda RX4 Wag  21.0    6  160.0  110  ...  17.02   0   1     4     4\n",
              "2         Datsun 710  22.8    4  108.0   93  ...  18.61   1   1     4     1\n",
              "3     Hornet 4 Drive  21.4    6  258.0  110  ...  19.44   1   0     3     1\n",
              "4  Hornet Sportabout  18.7    8  360.0  175  ...  17.02   0   0     3     2\n",
              "\n",
              "[5 rows x 12 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 2
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 368
        },
        "id": "JJHXj8RQAKEG",
        "outputId": "f6bfcc2e-af79-4e6a-e577-c9632bb37f28"
      },
      "source": [
        "chart = alt.Chart(cars)\n",
        "cars_chart = chart.mark_point().encode(\n",
        "    x = 'wt:Q',\n",
        "    y = 'mpg:Q'\n",
        ")\n",
        "cars_chart"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "alt.Chart(...)"
            ],
            "text/html": [
              "\n",
              "<div id=\"altair-viz-29c6b01527e346089724182068ea229b\"></div>\n",
              "<script type=\"text/javascript\">\n",
              "  (function(spec, embedOpt){\n",
              "    let outputDiv = document.currentScript.previousElementSibling;\n",
              "    if (outputDiv.id !== \"altair-viz-29c6b01527e346089724182068ea229b\") {\n",
              "      outputDiv = document.getElementById(\"altair-viz-29c6b01527e346089724182068ea229b\");\n",
              "    }\n",
              "    const paths = {\n",
              "      \"vega\": \"https://cdn.jsdelivr.net/npm//vega@5?noext\",\n",
              "      \"vega-lib\": \"https://cdn.jsdelivr.net/npm//vega-lib?noext\",\n",
              "      \"vega-lite\": \"https://cdn.jsdelivr.net/npm//vega-lite@4.8.1?noext\",\n",
              "      \"vega-embed\": \"https://cdn.jsdelivr.net/npm//vega-embed@6?noext\",\n",
              "    };\n",
              "\n",
              "    function loadScript(lib) {\n",
              "      return new Promise(function(resolve, reject) {\n",
              "        var s = document.createElement('script');\n",
              "        s.src = paths[lib];\n",
              "        s.async = true;\n",
              "        s.onload = () => resolve(paths[lib]);\n",
              "        s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n",
              "        document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
              "      });\n",
              "    }\n",
              "\n",
              "    function showError(err) {\n",
              "      outputDiv.innerHTML = `<div class=\"error\" style=\"color:red;\">${err}</div>`;\n",
              "      throw err;\n",
              "    }\n",
              "\n",
              "    function displayChart(vegaEmbed) {\n",
              "      vegaEmbed(outputDiv, spec, embedOpt)\n",
              "        .catch(err => showError(`Javascript Error: ${err.message}<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.`));\n",
              "    }\n",
              "\n",
              "    if(typeof define === \"function\" && define.amd) {\n",
              "      requirejs.config({paths});\n",
              "      require([\"vega-embed\"], displayChart, err => showError(`Error loading script: ${err.message}`));\n",
              "    } else if (typeof vegaEmbed === \"function\") {\n",
              "      displayChart(vegaEmbed);\n",
              "    } else {\n",
              "      loadScript(\"vega\")\n",
              "        .then(() => loadScript(\"vega-lite\"))\n",
              "        .then(() => loadScript(\"vega-embed\"))\n",
              "        .catch(showError)\n",
              "        .then(() => displayChart(vegaEmbed));\n",
              "    }\n",
              "  })({\"config\": {\"view\": {\"continuousWidth\": 400, \"continuousHeight\": 300}}, \"data\": {\"name\": \"data-dd9b7841dffa79dd9d0fa604fc4587af\"}, \"mark\": \"point\", \"encoding\": {\"x\": {\"type\": \"quantitative\", \"field\": \"wt\"}, \"y\": {\"type\": \"quantitative\", \"field\": \"mpg\"}}, \"$schema\": \"https://vega.github.io/schema/vega-lite/v4.8.1.json\", \"datasets\": {\"data-dd9b7841dffa79dd9d0fa604fc4587af\": [{\"model\": \"Mazda RX4\", \"mpg\": 21.0, \"cyl\": 6, \"disp\": 160.0, \"hp\": 110, \"drat\": 3.9, \"wt\": 2.62, \"qsec\": 16.46, \"vs\": 0, \"am\": 1, \"gear\": 4, \"carb\": 4}, {\"model\": \"Mazda RX4 Wag\", \"mpg\": 21.0, \"cyl\": 6, \"disp\": 160.0, \"hp\": 110, \"drat\": 3.9, \"wt\": 2.875, \"qsec\": 17.02, \"vs\": 0, \"am\": 1, \"gear\": 4, \"carb\": 4}, {\"model\": \"Datsun 710\", \"mpg\": 22.8, \"cyl\": 4, \"disp\": 108.0, \"hp\": 93, \"drat\": 3.85, \"wt\": 2.32, \"qsec\": 18.61, \"vs\": 1, \"am\": 1, \"gear\": 4, \"carb\": 1}, {\"model\": \"Hornet 4 Drive\", \"mpg\": 21.4, \"cyl\": 6, \"disp\": 258.0, \"hp\": 110, \"drat\": 3.08, \"wt\": 3.215, \"qsec\": 19.44, \"vs\": 1, \"am\": 0, \"gear\": 3, \"carb\": 1}, {\"model\": \"Hornet Sportabout\", \"mpg\": 18.7, \"cyl\": 8, \"disp\": 360.0, \"hp\": 175, \"drat\": 3.15, \"wt\": 3.44, \"qsec\": 17.02, \"vs\": 0, \"am\": 0, \"gear\": 3, \"carb\": 2}, {\"model\": \"Valiant\", \"mpg\": 18.1, \"cyl\": 6, \"disp\": 225.0, \"hp\": 105, \"drat\": 2.76, \"wt\": 3.46, \"qsec\": 20.22, \"vs\": 1, \"am\": 0, \"gear\": 3, \"carb\": 1}, {\"model\": \"Duster 360\", \"mpg\": 14.3, \"cyl\": 8, \"disp\": 360.0, \"hp\": 245, \"drat\": 3.21, \"wt\": 3.57, \"qsec\": 15.84, \"vs\": 0, \"am\": 0, \"gear\": 3, \"carb\": 4}, {\"model\": \"Merc 240D\", \"mpg\": 24.4, \"cyl\": 4, \"disp\": 146.7, \"hp\": 62, \"drat\": 3.69, \"wt\": 3.19, \"qsec\": 20.0, \"vs\": 1, \"am\": 0, \"gear\": 4, \"carb\": 2}, {\"model\": \"Merc 230\", \"mpg\": 22.8, \"cyl\": 4, \"disp\": 140.8, \"hp\": 95, \"drat\": 3.92, \"wt\": 3.15, \"qsec\": 22.9, \"vs\": 1, \"am\": 0, \"gear\": 4, \"carb\": 2}, {\"model\": \"Merc 280\", \"mpg\": 19.2, \"cyl\": 6, \"disp\": 167.6, \"hp\": 123, \"drat\": 3.92, \"wt\": 3.44, \"qsec\": 18.3, \"vs\": 1, \"am\": 0, \"gear\": 4, \"carb\": 4}, {\"model\": \"Merc 280C\", \"mpg\": 17.8, \"cyl\": 6, \"disp\": 167.6, \"hp\": 123, \"drat\": 3.92, \"wt\": 3.44, \"qsec\": 18.9, \"vs\": 1, \"am\": 0, \"gear\": 4, \"carb\": 4}, {\"model\": \"Merc 450SE\", \"mpg\": 16.4, \"cyl\": 8, \"disp\": 275.8, \"hp\": 180, \"drat\": 3.07, \"wt\": 4.07, \"qsec\": 17.4, \"vs\": 0, \"am\": 0, \"gear\": 3, \"carb\": 3}, {\"model\": \"Merc 450SL\", \"mpg\": 17.3, \"cyl\": 8, \"disp\": 275.8, \"hp\": 180, \"drat\": 3.07, \"wt\": 3.73, \"qsec\": 17.6, \"vs\": 0, \"am\": 0, \"gear\": 3, \"carb\": 3}, {\"model\": \"Merc 450SLC\", \"mpg\": 15.2, \"cyl\": 8, \"disp\": 275.8, \"hp\": 180, \"drat\": 3.07, \"wt\": 3.78, \"qsec\": 18.0, \"vs\": 0, \"am\": 0, \"gear\": 3, \"carb\": 3}, {\"model\": \"Cadillac Fleetwood\", \"mpg\": 10.4, \"cyl\": 8, \"disp\": 472.0, \"hp\": 205, \"drat\": 2.93, \"wt\": 5.25, \"qsec\": 17.98, \"vs\": 0, \"am\": 0, \"gear\": 3, \"carb\": 4}, {\"model\": \"Lincoln Continental\", \"mpg\": 10.4, \"cyl\": 8, \"disp\": 460.0, \"hp\": 215, \"drat\": 3.0, \"wt\": 5.4239999999999995, \"qsec\": 17.82, \"vs\": 0, \"am\": 0, \"gear\": 3, \"carb\": 4}, {\"model\": \"Chrysler Imperial\", \"mpg\": 14.7, \"cyl\": 8, \"disp\": 440.0, \"hp\": 230, \"drat\": 3.23, \"wt\": 5.345, \"qsec\": 17.42, \"vs\": 0, \"am\": 0, \"gear\": 3, \"carb\": 4}, {\"model\": \"Fiat 128\", \"mpg\": 32.4, \"cyl\": 4, \"disp\": 78.7, \"hp\": 66, \"drat\": 4.08, \"wt\": 2.2, \"qsec\": 19.47, \"vs\": 1, \"am\": 1, \"gear\": 4, \"carb\": 1}, {\"model\": \"Honda Civic\", \"mpg\": 30.4, \"cyl\": 4, \"disp\": 75.7, \"hp\": 52, \"drat\": 4.93, \"wt\": 1.615, \"qsec\": 18.52, \"vs\": 1, \"am\": 1, \"gear\": 4, \"carb\": 2}, {\"model\": \"Toyota Corolla\", \"mpg\": 33.9, \"cyl\": 4, \"disp\": 71.1, \"hp\": 65, \"drat\": 4.22, \"wt\": 1.835, \"qsec\": 19.9, \"vs\": 1, \"am\": 1, \"gear\": 4, \"carb\": 1}, {\"model\": \"Toyota Corona\", \"mpg\": 21.5, \"cyl\": 4, \"disp\": 120.1, \"hp\": 97, \"drat\": 3.7, \"wt\": 2.465, \"qsec\": 20.01, \"vs\": 1, \"am\": 0, \"gear\": 3, \"carb\": 1}, {\"model\": \"Dodge Challenger\", \"mpg\": 15.5, \"cyl\": 8, \"disp\": 318.0, \"hp\": 150, \"drat\": 2.76, \"wt\": 3.52, \"qsec\": 16.87, \"vs\": 0, \"am\": 0, \"gear\": 3, \"carb\": 2}, {\"model\": \"AMC Javelin\", \"mpg\": 15.2, \"cyl\": 8, \"disp\": 304.0, \"hp\": 150, \"drat\": 3.15, \"wt\": 3.435, \"qsec\": 17.3, \"vs\": 0, \"am\": 0, \"gear\": 3, \"carb\": 2}, {\"model\": \"Camaro Z28\", \"mpg\": 13.3, \"cyl\": 8, \"disp\": 350.0, \"hp\": 245, \"drat\": 3.73, \"wt\": 3.84, \"qsec\": 15.41, \"vs\": 0, \"am\": 0, \"gear\": 3, \"carb\": 4}, {\"model\": \"Pontiac Firebird\", \"mpg\": 19.2, \"cyl\": 8, \"disp\": 400.0, \"hp\": 175, \"drat\": 3.08, \"wt\": 3.845, \"qsec\": 17.05, \"vs\": 0, \"am\": 0, \"gear\": 3, \"carb\": 2}, {\"model\": \"Fiat X1-9\", \"mpg\": 27.3, \"cyl\": 4, \"disp\": 79.0, \"hp\": 66, \"drat\": 4.08, \"wt\": 1.935, \"qsec\": 18.9, \"vs\": 1, \"am\": 1, \"gear\": 4, \"carb\": 1}, {\"model\": \"Porsche 914-2\", \"mpg\": 26.0, \"cyl\": 4, \"disp\": 120.3, \"hp\": 91, \"drat\": 4.43, \"wt\": 2.14, \"qsec\": 16.7, \"vs\": 0, \"am\": 1, \"gear\": 5, \"carb\": 2}, {\"model\": \"Lotus Europa\", \"mpg\": 30.4, \"cyl\": 4, \"disp\": 95.1, \"hp\": 113, \"drat\": 3.77, \"wt\": 1.5130000000000001, \"qsec\": 16.9, \"vs\": 1, \"am\": 1, \"gear\": 5, \"carb\": 2}, {\"model\": \"Ford Pantera L\", \"mpg\": 15.8, \"cyl\": 8, \"disp\": 351.0, \"hp\": 264, \"drat\": 4.22, \"wt\": 3.17, \"qsec\": 14.5, \"vs\": 0, \"am\": 1, \"gear\": 5, \"carb\": 4}, {\"model\": \"Ferrari Dino\", \"mpg\": 19.7, \"cyl\": 6, \"disp\": 145.0, \"hp\": 175, \"drat\": 3.62, \"wt\": 2.77, \"qsec\": 15.5, \"vs\": 0, \"am\": 1, \"gear\": 5, \"carb\": 6}, {\"model\": \"Maserati Bora\", \"mpg\": 15.0, \"cyl\": 8, \"disp\": 301.0, \"hp\": 335, \"drat\": 3.54, \"wt\": 3.57, \"qsec\": 14.6, \"vs\": 0, \"am\": 1, \"gear\": 5, \"carb\": 8}, {\"model\": \"Volvo 142E\", \"mpg\": 21.4, \"cyl\": 4, \"disp\": 121.0, \"hp\": 109, \"drat\": 4.11, \"wt\": 2.78, \"qsec\": 18.6, \"vs\": 1, \"am\": 1, \"gear\": 4, \"carb\": 2}]}}, {\"mode\": \"vega-lite\"});\n",
              "</script>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZxQvRYzTAh47"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}