30239-notes/examples/highlighting-example.ipynb

395 lines
437 KiB
Plaintext
Raw Normal View History

2024-10-27 23:13:04 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "aad6179d-be3f-4e61-a80b-3428f17e12b3",
"metadata": {},
"source": [
"## Example: Highlighting Subset of Data\n",
"\n",
"Let's say you have a lot of something: countries or counties maybe, represented by lines or points, and you want to show a lot of them for context, but there are 1-5 that are very important.\n",
"\n",
"This is an example of how you might desaturate all but the key items, and ensure that the data that you care about stands out.\n",
"\n",
"This can turn a pile of spaghetti into a clear indication of a point."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "07eb67c8-41d0-4c84-892d-822d90504cad",
"metadata": {},
"outputs": [],
"source": [
"import altair as alt\n",
"import polars as pl\n",
"import random"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "eac46c04-ab06-44ea-82af-6471c71f486b",
"metadata": {},
"outputs": [],
"source": [
"countries_initial = {\n",
" \"AA\": 20, \"AB\": 25, \"AC\": 30, \"AD\": 27, \"AE\": 21,\n",
" \"BA\": 20, \"BB\": 25, \"BC\": 30, \"BD\": 27, \"BE\": 21,\n",
" \"CA\": 20, \"CB\": 25, \"CC\": 30, \"CD\": 27, \"CE\": 21,\n",
" \"DA\": 20, \"DB\": 25, \"DC\": 30, \"DD\": 27, \"CE\": 21,\n",
" \"EA\": 20, \"EB\": 25, \"EC\": 30, \"ED\": 27, \"CE\": 21,\n",
" }\n",
"def generate_fake_data():\n",
" countries = {\n",
" \"AA\": 20, \"AB\": 25, \"AC\": 30, \"AD\": 27, \"AE\": 21,\n",
" \"BA\": 20, \"BB\": 25, \"BC\": 30, \"BD\": 27, \"BE\": 21,\n",
" \"CA\": 20, \"CB\": 25, \"CC\": 30, \"CD\": 27, \"CE\": 21,\n",
" \"DA\": 20, \"DB\": 25, \"DC\": 30, \"DD\": 27, \"CE\": 21,\n",
" \"EA\": 20, \"EB\": 25, \"EC\": 30, \"ED\": 27, \"CE\": 21,\n",
" }\n",
" # 100 records in our time series\n",
" for t in range(100):\n",
" # update all 25\n",
" for country in countries:\n",
" countries[country] += random.random() - 0.3\n",
" yield {\"country\": country, \"val\": countries[country], \"t\": t} \n",
"\n",
"# just need some fake time-series like data\n",
"# scatterplots can also benefit a lot from these ideas\n",
"data = pl.DataFrame(generate_fake_data())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fd90289f-46bb-4102-bb02-bf5732dee01c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 45,
"id": "17e6f86a-bd9f-44a7-af5f-917237c3393b",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<style>\n",
" #altair-viz-0bf2236c80754a1e8911e265343b907f.vega-embed {\n",
" width: 100%;\n",
" display: flex;\n",
" }\n",
"\n",
" #altair-viz-0bf2236c80754a1e8911e265343b907f.vega-embed details,\n",
" #altair-viz-0bf2236c80754a1e8911e265343b907f.vega-embed details summary {\n",
" position: relative;\n",
" }\n",
"</style>\n",
"<div id=\"altair-viz-0bf2236c80754a1e8911e265343b907f\"></div>\n",
"<script type=\"text/javascript\">\n",
" var VEGA_DEBUG = (typeof VEGA_DEBUG == \"undefined\") ? {} : VEGA_DEBUG;\n",
" (function(spec, embedOpt){\n",
" let outputDiv = document.currentScript.previousElementSibling;\n",
" if (outputDiv.id !== \"altair-viz-0bf2236c80754a1e8911e265343b907f\") {\n",
" outputDiv = document.getElementById(\"altair-viz-0bf2236c80754a1e8911e265343b907f\");\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@5.20.1?noext\",\n",
" \"vega-embed\": \"https://cdn.jsdelivr.net/npm/vega-embed@6?noext\",\n",
" };\n",
"\n",
" function maybeLoadScript(lib, version) {\n",
" var key = `${lib.replace(\"-\", \"\")}_version`;\n",
" return (VEGA_DEBUG[key] == version) ?\n",
" Promise.resolve(paths[lib]) :\n",
" new Promise(function(resolve, reject) {\n",
" var s = document.createElement('script');\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" s.async = true;\n",
" s.onload = () => {\n",
" VEGA_DEBUG[key] = version;\n",
" return resolve(paths[lib]);\n",
" };\n",
" s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n",
" s.src = paths[lib];\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 {\n",
" maybeLoadScript(\"vega\", \"5\")\n",
" .then(() => maybeLoadScript(\"vega-lite\", \"5.20.1\"))\n",
" .then(() => maybeLoadScript(\"vega-embed\", \"6\"))\n",
" .catch(showError)\n",
" .then(() => displayChart(vegaEmbed));\n",
" }\n",
" })({\"config\": {\"view\": {\"continuousWidth\": 300, \"continuousHeight\": 300}}, \"data\": {\"name\": \"data-c9bb8c97fb5e80e6964753840972258a\"}, \"mark\": {\"type\": \"line\"}, \"encoding\": {\"color\": {\"field\": \"country\", \"type\": \"nominal\"}, \"x\": {\"field\": \"t\", \"type\": \"quantitative\"}, \"y\": {\"field\": \"val\", \"type\": \"quantitative\"}}, \"$schema\": \"https://vega.github.io/schema/vega-lite/v5.20.1.json\", \"datasets\": {\"data-c9bb8c97fb5e80e6964753840972258a\": [{\"country\": \"AA\", \"val\": 20.690405202053906, \"t\": 0}, {\"country\": \"AB\", \"val\": 25.627486098647374, \"t\": 0}, {\"country\": \"AC\", \"val\": 29.803984673411694, \"t\": 0}, {\"country\": \"AD\", \"val\": 26.806044568343054, \"t\": 0}, {\"country\": \"AE\", \"val\": 21.65437480231554, \"t\": 0}, {\"country\": \"BA\", \"val\": 19.82254802157332, \"t\": 0}, {\"country\": \"BB\", \"val\": 24.755561439368503, \"t\": 0}, {\"country\": \"BC\", \"val\": 29.779613386761508, \"t\": 0}, {\"country\": \"BD\", \"val\": 27.125654519671674, \"t\": 0}, {\"country\": \"BE\", \"val\": 21.082171596350676, \"t\": 0}, {\"country\": \"CA\", \"val\": 20.18290256011536, \"t\": 0}, {\"country\": \"CB\", \"val\": 25.16475598155596, \"t\": 0}, {\"country\": \"CC\", \"val\": 29.71481094502052, \"t\": 0}, {\"country\": \"CD\", \"val\": 27.29916915735259, \"t\": 0}, {\"country\": \"CE\", \"val\": 21.355857567341904, \"t\": 0}, {\"country\": \"DA\", \"val\": 20.3094331746071, \"t\": 0}, {\"country\": \"DB\", \"val\": 25.01681416413383, \"t\": 0}, {\"country\": \"DC\", \"val\": 30.131491561347673, \"t\": 0}, {\"country\": \"DD\", \"val\": 27.607889963520954, \"t\": 0}, {\"country\": \"EA\", \"val\": 19.774303589086518, \"t\": 0}, {\"country\": \"EB\", \"val\": 25.330115328630598, \"t\": 0}, {\"country\": \"EC\", \"val\": 29.8771142165084, \"t\": 0}, {\"country\": \"ED\", \"val\": 26.77191867642152, \"t\": 0}, {\"country\": \"AA\", \"val\": 20.495627325522573, \"t\": 1}, {\"country\": \"AB\", \"val\": 26.003957253497624, \"t\": 1}, {\"country\": \"AC\", \"val\": 29.75194884139284, \"t\": 1}, {\"country\": \"AD\", \"val\": 27.309842011711044, \"t\": 1}, {\"country\": \"AE\", \"val\": 21.477336976624958, \"t\": 1}, {\"country\": \"BA\", \"val\": 20.331868799852465, \"t\": 1}, {\"country\": \"BB\", \"val\": 24.633904719669342, \"t\": 1}, {\"country\": \"BC\", \"val\": 29.74968113621723, \"t\": 1}, {\"country\": \"BD\", \"val\": 26.86191197658304, \"t\": 1}, {\"country\": \"BE\", \"val\": 21.347014669128868, \"t\": 1}, {\"country\": \"CA\", \"val\": 20.85581138854999, \"t\": 1}, {\"country\": \"CB\", \"val\": 25.64558158050691, \"t\": 1}, {\"country\": \"CC\", \"val\": 29.919570097694987, \"t\": 1}, {\"country\": \"CD\", \"val\": 27.381483164123864, \"t\": 1}, {\"country\": \"CE\", \"val\": 21.38550784594748, \"t\": 1}, {\"country\": \"DA\", \"val\": 20.06642581872245, \"t\": 1}, {\"country\": \"DB\", \"val\": 25.086742362841676, \"t\": 1}, {\"country\": \"DC\", \"val\": 29.887538578377104, \"t\": 1}, {\"country\": \"DD\", \"val\": 27.711025453048087, \"t\": 1}, {\"country\": \"EA\", \"val\": 20.19304591281626, \"t\": 1}, {\"country\": \"EB\", \"val\": 25.926802040442094, \"t\": 1}, {\"country\": \"EC\", \"val\": 29.71487037943407, \"t\": 1}, {\"country\": \"ED\", \"val\": 27.10130642907181, \"t\": 1}, {\"country\": \"AA\", \"val\": 20.29806964699776, \"t\": 2}, {\"country\": \"AB\", \"val\": 26.13399981046762, \"t\": 2}, {\"country\": \"AC\", \"val\": 29.981937614792052, \"t\": 2}, {\"country\": \"AD\", \"val\": 27.29431855338583, \"t\": 2}, {\"country\": \"AE\", \"val\": 21.202771010819575, \"t\": 2}, {\"country\": \"BA\", \"val\": 20.816444885025025, \"t\": 2}, {\"country\": \"BB\", \"val\": 24.989174186590976, \"t\": 2}, {\"country\": \"BC\", \"val\": 30.428661910793284, \"t\": 2}, {\"country\": \"BD\", \"val\": 27.116717585776872, \"t\": 2}, {\"country\": \"BE\", \"val\": 21.71129604282831, \"t\": 2}, {\"country\": \"CA\", \"val\": 21.366807528470606, \"t\": 2}, {\"country\": \"CB\", \"val\": 25.503015347574618, \"t\": 2}, {\"country\": \"C
"</script>"
],
"text/plain": [
"alt.Chart(...)"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Spaghetti\n",
"alt.Chart(data).mark_line().encode(color=\"country\", y=\"val\", x=\"t\")"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "c5fc9ebc-0286-4d87-aeaa-c589127871bb",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<style>\n",
" #altair-viz-928865c018314977bb16c755db7538c5.vega-embed {\n",
" width: 100%;\n",
" display: flex;\n",
" }\n",
"\n",
" #altair-viz-928865c018314977bb16c755db7538c5.vega-embed details,\n",
" #altair-viz-928865c018314977bb16c755db7538c5.vega-embed details summary {\n",
" position: relative;\n",
" }\n",
"</style>\n",
"<div id=\"altair-viz-928865c018314977bb16c755db7538c5\"></div>\n",
"<script type=\"text/javascript\">\n",
" var VEGA_DEBUG = (typeof VEGA_DEBUG == \"undefined\") ? {} : VEGA_DEBUG;\n",
" (function(spec, embedOpt){\n",
" let outputDiv = document.currentScript.previousElementSibling;\n",
" if (outputDiv.id !== \"altair-viz-928865c018314977bb16c755db7538c5\") {\n",
" outputDiv = document.getElementById(\"altair-viz-928865c018314977bb16c755db7538c5\");\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@5.20.1?noext\",\n",
" \"vega-embed\": \"https://cdn.jsdelivr.net/npm/vega-embed@6?noext\",\n",
" };\n",
"\n",
" function maybeLoadScript(lib, version) {\n",
" var key = `${lib.replace(\"-\", \"\")}_version`;\n",
" return (VEGA_DEBUG[key] == version) ?\n",
" Promise.resolve(paths[lib]) :\n",
" new Promise(function(resolve, reject) {\n",
" var s = document.createElement('script');\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" s.async = true;\n",
" s.onload = () => {\n",
" VEGA_DEBUG[key] = version;\n",
" return resolve(paths[lib]);\n",
" };\n",
" s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n",
" s.src = paths[lib];\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 {\n",
" maybeLoadScript(\"vega\", \"5\")\n",
" .then(() => maybeLoadScript(\"vega-lite\", \"5.20.1\"))\n",
" .then(() => maybeLoadScript(\"vega-embed\", \"6\"))\n",
" .catch(showError)\n",
" .then(() => displayChart(vegaEmbed));\n",
" }\n",
" })({\"config\": {\"view\": {\"continuousWidth\": 300, \"continuousHeight\": 300}}, \"data\": {\"name\": \"data-c9bb8c97fb5e80e6964753840972258a\"}, \"mark\": {\"type\": \"line\"}, \"encoding\": {\"color\": {\"condition\": {\"test\": {\"field\": \"country\", \"oneOf\": [\"EC\", \"ED\"]}, \"value\": \"orange\"}, \"value\": \"lightgrey\"}, \"detail\": {\"field\": \"country\", \"type\": \"nominal\"}, \"x\": {\"field\": \"t\", \"type\": \"quantitative\"}, \"y\": {\"field\": \"val\", \"type\": \"quantitative\"}}, \"$schema\": \"https://vega.github.io/schema/vega-lite/v5.20.1.json\", \"datasets\": {\"data-c9bb8c97fb5e80e6964753840972258a\": [{\"country\": \"AA\", \"val\": 20.690405202053906, \"t\": 0}, {\"country\": \"AB\", \"val\": 25.627486098647374, \"t\": 0}, {\"country\": \"AC\", \"val\": 29.803984673411694, \"t\": 0}, {\"country\": \"AD\", \"val\": 26.806044568343054, \"t\": 0}, {\"country\": \"AE\", \"val\": 21.65437480231554, \"t\": 0}, {\"country\": \"BA\", \"val\": 19.82254802157332, \"t\": 0}, {\"country\": \"BB\", \"val\": 24.755561439368503, \"t\": 0}, {\"country\": \"BC\", \"val\": 29.779613386761508, \"t\": 0}, {\"country\": \"BD\", \"val\": 27.125654519671674, \"t\": 0}, {\"country\": \"BE\", \"val\": 21.082171596350676, \"t\": 0}, {\"country\": \"CA\", \"val\": 20.18290256011536, \"t\": 0}, {\"country\": \"CB\", \"val\": 25.16475598155596, \"t\": 0}, {\"country\": \"CC\", \"val\": 29.71481094502052, \"t\": 0}, {\"country\": \"CD\", \"val\": 27.29916915735259, \"t\": 0}, {\"country\": \"CE\", \"val\": 21.355857567341904, \"t\": 0}, {\"country\": \"DA\", \"val\": 20.3094331746071, \"t\": 0}, {\"country\": \"DB\", \"val\": 25.01681416413383, \"t\": 0}, {\"country\": \"DC\", \"val\": 30.131491561347673, \"t\": 0}, {\"country\": \"DD\", \"val\": 27.607889963520954, \"t\": 0}, {\"country\": \"EA\", \"val\": 19.774303589086518, \"t\": 0}, {\"country\": \"EB\", \"val\": 25.330115328630598, \"t\": 0}, {\"country\": \"EC\", \"val\": 29.8771142165084, \"t\": 0}, {\"country\": \"ED\", \"val\": 26.77191867642152, \"t\": 0}, {\"country\": \"AA\", \"val\": 20.495627325522573, \"t\": 1}, {\"country\": \"AB\", \"val\": 26.003957253497624, \"t\": 1}, {\"country\": \"AC\", \"val\": 29.75194884139284, \"t\": 1}, {\"country\": \"AD\", \"val\": 27.309842011711044, \"t\": 1}, {\"country\": \"AE\", \"val\": 21.477336976624958, \"t\": 1}, {\"country\": \"BA\", \"val\": 20.331868799852465, \"t\": 1}, {\"country\": \"BB\", \"val\": 24.633904719669342, \"t\": 1}, {\"country\": \"BC\", \"val\": 29.74968113621723, \"t\": 1}, {\"country\": \"BD\", \"val\": 26.86191197658304, \"t\": 1}, {\"country\": \"BE\", \"val\": 21.347014669128868, \"t\": 1}, {\"country\": \"CA\", \"val\": 20.85581138854999, \"t\": 1}, {\"country\": \"CB\", \"val\": 25.64558158050691, \"t\": 1}, {\"country\": \"CC\", \"val\": 29.919570097694987, \"t\": 1}, {\"country\": \"CD\", \"val\": 27.381483164123864, \"t\": 1}, {\"country\": \"CE\", \"val\": 21.38550784594748, \"t\": 1}, {\"country\": \"DA\", \"val\": 20.06642581872245, \"t\": 1}, {\"country\": \"DB\", \"val\": 25.086742362841676, \"t\": 1}, {\"country\": \"DC\", \"val\": 29.887538578377104, \"t\": 1}, {\"country\": \"DD\", \"val\": 27.711025453048087, \"t\": 1}, {\"country\": \"EA\", \"val\": 20.19304591281626, \"t\": 1}, {\"country\": \"EB\", \"val\": 25.926802040442094, \"t\": 1}, {\"country\": \"EC\", \"val\": 29.71487037943407, \"t\": 1}, {\"country\": \"ED\", \"val\": 27.10130642907181, \"t\": 1}, {\"country\": \"AA\", \"val\": 20.29806964699776, \"t\": 2}, {\"country\": \"AB\", \"val\": 26.13399981046762, \"t\": 2}, {\"country\": \"AC\", \"val\": 29.981937614792052, \"t\": 2}, {\"country\": \"AD\", \"val\": 27.29431855338583, \"t\": 2}, {\"country\": \"AE\", \"val\": 21.202771010819575, \"t\": 2}, {\"country\": \"BA\", \"val\": 20.816444885025025, \"t\": 2}, {\"country\": \"BB\", \"val\": 24.989174186590976, \"t\": 2}, {\"country\": \"BC\", \"val\": 30.428661910793284, \"t\": 2}, {\"country\": \"BD\", \"val\": 27.116717585776872, \"t\": 2}, {\"country\": \"BE\", \"val\": 21.71129604282831, \"t\":
"</script>"
],
"text/plain": [
"alt.Chart(...)"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Example: highlighting a subset of lines\n",
"\n",
"# If you're seeing overlaps, reorder data so your highlights are last\n",
"highlights = [\"EC\", \"ED\"] \n",
"\n",
"alt.Chart(data).mark_line().encode( \n",
" y=\"val\",\n",
" x=\"t\",\n",
" color=alt.condition(\n",
" alt.FieldOneOfPredicate(field='country', oneOf=highlights),\n",
" alt.value('orange'), # Color for the highlighted countries\n",
" alt.value('lightgrey') # Color for the rest\n",
" ),\n",
" # this is needed to preserve grouping when using condition\n",
" detail=\"country\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "31ba7622-d1da-4f52-bf44-26e3e1ced0d5",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<style>\n",
" #altair-viz-835d5c9c7f0849f5aec23c3053ce85a2.vega-embed {\n",
" width: 100%;\n",
" display: flex;\n",
" }\n",
"\n",
" #altair-viz-835d5c9c7f0849f5aec23c3053ce85a2.vega-embed details,\n",
" #altair-viz-835d5c9c7f0849f5aec23c3053ce85a2.vega-embed details summary {\n",
" position: relative;\n",
" }\n",
"</style>\n",
"<div id=\"altair-viz-835d5c9c7f0849f5aec23c3053ce85a2\"></div>\n",
"<script type=\"text/javascript\">\n",
" var VEGA_DEBUG = (typeof VEGA_DEBUG == \"undefined\") ? {} : VEGA_DEBUG;\n",
" (function(spec, embedOpt){\n",
" let outputDiv = document.currentScript.previousElementSibling;\n",
" if (outputDiv.id !== \"altair-viz-835d5c9c7f0849f5aec23c3053ce85a2\") {\n",
" outputDiv = document.getElementById(\"altair-viz-835d5c9c7f0849f5aec23c3053ce85a2\");\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@5.20.1?noext\",\n",
" \"vega-embed\": \"https://cdn.jsdelivr.net/npm/vega-embed@6?noext\",\n",
" };\n",
"\n",
" function maybeLoadScript(lib, version) {\n",
" var key = `${lib.replace(\"-\", \"\")}_version`;\n",
" return (VEGA_DEBUG[key] == version) ?\n",
" Promise.resolve(paths[lib]) :\n",
" new Promise(function(resolve, reject) {\n",
" var s = document.createElement('script');\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" s.async = true;\n",
" s.onload = () => {\n",
" VEGA_DEBUG[key] = version;\n",
" return resolve(paths[lib]);\n",
" };\n",
" s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n",
" s.src = paths[lib];\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 {\n",
" maybeLoadScript(\"vega\", \"5\")\n",
" .then(() => maybeLoadScript(\"vega-lite\", \"5.20.1\"))\n",
" .then(() => maybeLoadScript(\"vega-embed\", \"6\"))\n",
" .catch(showError)\n",
" .then(() => displayChart(vegaEmbed));\n",
" }\n",
" })({\"config\": {\"view\": {\"continuousWidth\": 300, \"continuousHeight\": 300}}, \"data\": {\"name\": \"data-c9bb8c97fb5e80e6964753840972258a\"}, \"mark\": {\"type\": \"line\"}, \"encoding\": {\"color\": {\"field\": \"country\", \"scale\": {\"domain\": [\"AA\", \"AB\", \"AC\", \"AD\", \"AE\", \"BA\", \"BB\", \"BC\", \"BD\", \"BE\", \"CA\", \"CB\", \"CC\", \"CD\", \"CE\", \"DA\", \"DB\", \"DC\", \"DD\", \"EA\", \"EB\", \"EC\", \"ED\"], \"range\": [\"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"#ddd\", \"blue\", \"red\"]}, \"type\": \"nominal\"}, \"x\": {\"field\": \"t\", \"type\": \"quantitative\"}, \"y\": {\"field\": \"val\", \"type\": \"quantitative\"}}, \"$schema\": \"https://vega.github.io/schema/vega-lite/v5.20.1.json\", \"datasets\": {\"data-c9bb8c97fb5e80e6964753840972258a\": [{\"country\": \"AA\", \"val\": 20.690405202053906, \"t\": 0}, {\"country\": \"AB\", \"val\": 25.627486098647374, \"t\": 0}, {\"country\": \"AC\", \"val\": 29.803984673411694, \"t\": 0}, {\"country\": \"AD\", \"val\": 26.806044568343054, \"t\": 0}, {\"country\": \"AE\", \"val\": 21.65437480231554, \"t\": 0}, {\"country\": \"BA\", \"val\": 19.82254802157332, \"t\": 0}, {\"country\": \"BB\", \"val\": 24.755561439368503, \"t\": 0}, {\"country\": \"BC\", \"val\": 29.779613386761508, \"t\": 0}, {\"country\": \"BD\", \"val\": 27.125654519671674, \"t\": 0}, {\"country\": \"BE\", \"val\": 21.082171596350676, \"t\": 0}, {\"country\": \"CA\", \"val\": 20.18290256011536, \"t\": 0}, {\"country\": \"CB\", \"val\": 25.16475598155596, \"t\": 0}, {\"country\": \"CC\", \"val\": 29.71481094502052, \"t\": 0}, {\"country\": \"CD\", \"val\": 27.29916915735259, \"t\": 0}, {\"country\": \"CE\", \"val\": 21.355857567341904, \"t\": 0}, {\"country\": \"DA\", \"val\": 20.3094331746071, \"t\": 0}, {\"country\": \"DB\", \"val\": 25.01681416413383, \"t\": 0}, {\"country\": \"DC\", \"val\": 30.131491561347673, \"t\": 0}, {\"country\": \"DD\", \"val\": 27.607889963520954, \"t\": 0}, {\"country\": \"EA\", \"val\": 19.774303589086518, \"t\": 0}, {\"country\": \"EB\", \"val\": 25.330115328630598, \"t\": 0}, {\"country\": \"EC\", \"val\": 29.8771142165084, \"t\": 0}, {\"country\": \"ED\", \"val\": 26.77191867642152, \"t\": 0}, {\"country\": \"AA\", \"val\": 20.495627325522573, \"t\": 1}, {\"country\": \"AB\", \"val\": 26.003957253497624, \"t\": 1}, {\"country\": \"AC\", \"val\": 29.75194884139284, \"t\": 1}, {\"country\": \"AD\", \"val\": 27.309842011711044, \"t\": 1}, {\"country\": \"AE\", \"val\": 21.477336976624958, \"t\": 1}, {\"country\": \"BA\", \"val\": 20.331868799852465, \"t\": 1}, {\"country\": \"BB\", \"val\": 24.633904719669342, \"t\": 1}, {\"country\": \"BC\", \"val\": 29.74968113621723, \"t\": 1}, {\"country\": \"BD\", \"val\": 26.86191197658304, \"t\": 1}, {\"country\": \"BE\", \"val\": 21.347014669128868, \"t\": 1}, {\"country\": \"CA\", \"val\": 20.85581138854999, \"t\": 1}, {\"country\": \"CB\", \"val\": 25.64558158050691, \"t\": 1}, {\"country\": \"CC\", \"val\": 29.919570097694987, \"t\": 1}, {\"country\": \"CD\", \"val\": 27.381483164123864, \"t\": 1}, {\"country\": \"CE\", \"val\": 21.38550784594748, \"t\": 1}, {\"country\": \"DA\", \"val\": 20.06642581872245, \"t\": 1}, {\"country\": \"DB\", \"val\": 25.086742362841676, \"t\": 1}, {\"country\": \"DC\", \"val\": 29.887538578377104, \"t\": 1}, {\"country\": \"DD\", \"val\": 27.711025453048087, \"t\": 1}, {\"country\": \"EA\", \"val\": 20.19304591281626, \"t\": 1}, {\"country\": \"EB\", \"val\": 25.926802040442094, \"t\": 1}, {\"country\": \"EC\", \"val\": 29.71487037943407, \"t\": 1}, {\"country\": \"ED\", \"val\": 27.10130642907181, \"t\": 1}, {\"country\": \"AA\", \"val\": 20.29806964699776, \"t\": 2}, {\"country\": \"AB\", \"val\": 26.13399981046762, \"t\": 2}, {\"country\": \"AC\", \"val\": 29.981937614792052, \"t\": 2}, {\"country\": \"AD\", \"val\": 27.29431855338583, \"t\": 2}, {\"country\": \"AE\", \"val\": 21.202771010819575, \"t\": 2
"</script>"
],
"text/plain": [
"alt.Chart(...)"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# alternately, using a mapping\n",
"color_mapping = {\"EC\": \"blue\", \"ED\": \"red\", \"default\": \"#ddd\"}\n",
"alt.Chart(data).mark_line().encode( \n",
" y=\"val\",\n",
" x=\"t\",\n",
" color=alt.Color(\"country:N\", scale=alt.Scale(\n",
" domain=list(countries_initial.keys()),\n",
" range=[color_mapping.get(c, color_mapping['default']) for c in countries_initial] \n",
" )),\n",
" #detail=\"country\",\n",
") "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b547cf94-0d51-4f82-bb04-ccb7984b002c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}