30239-notes/03.charts/examples.ipynb

178 lines
31 KiB
Plaintext
Raw Normal View History

2024-09-28 04:10:24 +00:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 43,
"id": "4a015e87-cf20-47b0-b271-a9dc1c38f2b3",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<style>\n",
" #altair-viz-6e4fea272d6f4342b396f5d5f1715ba6.vega-embed {\n",
" width: 100%;\n",
" display: flex;\n",
" }\n",
"\n",
" #altair-viz-6e4fea272d6f4342b396f5d5f1715ba6.vega-embed details,\n",
" #altair-viz-6e4fea272d6f4342b396f5d5f1715ba6.vega-embed details summary {\n",
" position: relative;\n",
" }\n",
"</style>\n",
"<div id=\"altair-viz-6e4fea272d6f4342b396f5d5f1715ba6\"></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-6e4fea272d6f4342b396f5d5f1715ba6\") {\n",
" outputDiv = document.getElementById(\"altair-viz-6e4fea272d6f4342b396f5d5f1715ba6\");\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}}, \"layer\": [{\"data\": {\"name\": \"data-01eed797016d7a820140a15818a43ac7\"}, \"mark\": {\"type\": \"line\"}, \"encoding\": {\"color\": {\"field\": \"symbol\", \"type\": \"nominal\"}, \"x\": {\"field\": \"date\", \"type\": \"temporal\"}, \"y\": {\"field\": \"price\", \"type\": \"quantitative\"}}}, {\"data\": {\"name\": \"empty\"}, \"mark\": {\"type\": \"text\", \"color\": \"black\", \"dx\": -45, \"dy\": -130}, \"encoding\": {\"text\": {\"datum\": \"iPhone Released\"}, \"x\": {\"datum\": {\"month\": \"June\", \"year\": 2007}}}}, {\"data\": {\"name\": \"empty\"}, \"mark\": {\"type\": \"rule\", \"color\": \"black\", \"strokeWidth\": 1}, \"encoding\": {\"x\": {\"datum\": {\"month\": \"June\", \"year\": 2007}}}}, {\"data\": {\"name\": \"empty\"}, \"mark\": {\"type\": \"text\", \"color\": \"black\", \"dx\": 45, \"dy\": 60}, \"encoding\": {\"text\": {\"datum\": \"2008 Financial Crisis\"}, \"x\": {\"datum\": {\"month\": \"November\", \"year\": 2008}}}}, {\"data\": {\"name\": \"empty\"}, \"mark\": {\"type\": \"rect\", \"color\": \"#cccccc33\"}, \"encoding\": {\"text\": {\"datum\": \"2008 Financial Crisis\"}, \"x\": {\"datum\": {\"month\": \"January\", \"year\": 2008}}, \"x2\": {\"datum\": {\"month\": \"January\", \"year\": 2009}}}}], \"$schema\": \"https://vega.github.io/schema/vega-lite/v5.20.1.json\", \"datasets\": {\"data-01eed797016d7a820140a15818a43ac7\": [{\"symbol\": \"MSFT\", \"date\": \"2005-02-01T00:00:00\", \"price\": 23.15}, {\"symbol\": \"MSFT\", \"date\": \"2005-03-01T00:00:00\", \"price\": 22.24}, {\"symbol\": \"MSFT\", \"date\": \"2005-04-01T00:00:00\", \"price\": 23.28}, {\"symbol\": \"MSFT\", \"date\": \"2005-05-01T00:00:00\", \"price\": 23.82}, {\"symbol\": \"MSFT\", \"date\": \"2005-06-01T00:00:00\", \"price\": 22.93}, {\"symbol\": \"MSFT\", \"date\": \"2005-07-01T00:00:00\", \"price\": 23.64}, {\"symbol\": \"MSFT\", \"date\": \"2005-08-01T00:00:00\", \"price\": 25.35}, {\"symbol\": \"MSFT\", \"date\": \"2005-09-01T00:00:00\", \"price\": 23.83}, {\"symbol\": \"MSFT\", \"date\": \"2005-10-01T00:00:00\", \"price\": 23.8}, {\"symbol\": \"MSFT\", \"date\": \"2005-11-01T00:00:00\", \"price\": 25.71}, {\"symbol\": \"MSFT\", \"date\": \"2005-12-01T00:00:00\", \"price\": 24.29}, {\"symbol\": \"MSFT\", \"date\": \"2006-01-01T00:00:00\", \"price\": 26.14}, {\"symbol\": \"MSFT\", \"date\": \"2006-02-01T00:00:00\", \"price\": 25.04}, {\"symbol\": \"MSFT\", \"date\": \"2006-03-01T00:00:00\", \"price\": 25.36}, {\"symbol\": \"MSFT\", \"date\": \"2006-04-01T00:00:00\", \"price\": 22.5}, {\"symbol\": \"MSFT\", \"date\": \"2006-05-01T00:00:00\", \"price\": 21.19}, {\"symbol\": \"MSFT\", \"date\": \"2006-06-01T00:00:00\", \"price\": 21.8}, {\"symbol\": \"MSFT\", \"date\": \"2006-07-01T00:00:00\", \"price\": 22.51}, {\"symbol\": \"MSFT\", \"date\": \"2006-08-01T00:00:00\", \"price\": 24.13}, {\"symbol\": \"MSFT\", \"date\": \"2006-09-01T00:00:00\", \"price\": 25.68}, {\"symbol\": \"MSFT\", \"date\": \"2006-10-01T00:00:00\", \"price\": 26.96}, {\"symbol\": \"MSFT\", \"date\": \"2006-11-01T00:00:00\", \"price\": 27.66}, {\"symbol\": \"MSFT\", \"date\": \"2006-12-01T00:00:00\", \"price\": 28.13}, {\"symbol\": \"MSFT\", \"date\": \"2007-01-01T00:00:00\", \"price\": 29.07}, {\"symbol\": \"MSFT\", \"date\": \"2007-02-01T00:00:00\", \"price\": 26.63}, {\"symbol\": \"MSFT\", \"date\": \"2007-03-01T00:00:00\", \"price\": 26.35}, {\"symbol\": \"MSFT\", \"date\": \"2007-04-01T00:00:00\", \"price\": 28.3}, {\"symbol\": \"MSFT\", \"date\": \"2007-05-01T00:00:00\", \"price\": 29.11}, {\"symbol\": \"MSFT\", \"date\": \"2007-06-01T00:00:00\", \"price\": 27.95}, {\"symbol\": \"MSFT\", \"date\": \"2007-07-01T00:00:00\", \"price\": 27.5}, {\"symbol\": \"MSFT\", \"date\": \"2007-08-01T00:00:00\", \"price\": 27.34}, {\"symbol\": \"MSFT\", \"date\": \"2007-09-01T00:00:00\", \"price\": 28.04}, {\"symbol\": \"MSFT\", \"date\": \"2007-10-01T00:00:00\", \"price\": 35.03}, {\"symbol\": \"MSFT\", \"date\": \"2007-11-01T00:00:00\", \"price\": 32.09}, {\"symbol\": \
"</script>"
],
"text/plain": [
"alt.LayerChart(...)"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import altair as alt\n",
"from vega_datasets import data\n",
"\n",
"source = data.stocks()\n",
"source = source[source[\"date\"] > \"2005-01-01\"]\n",
"\n",
"lines = (\n",
" alt.Chart(source)\n",
" .mark_line()\n",
" .encode(x=\"date\", y=\"price\", color=\"symbol\")\n",
")\n",
"\n",
"xrule = (\n",
" alt.Chart()\n",
" .mark_rule(color=\"black\", strokeWidth=1)\n",
" .encode(x=alt.datum(alt.DateTime(year=2007, month=\"June\")))\n",
")\n",
"\n",
"label = (\n",
" alt.Chart()\n",
" .mark_text(color=\"black\", dx=-45, dy=-130)\n",
" .encode(\n",
" x=alt.datum(alt.DateTime(year=2007, month=\"June\")),\n",
" text=alt.datum(\"iPhone Released\"),\n",
" )\n",
")\n",
"\n",
"xrule2 = (\n",
" alt.Chart()\n",
" .mark_text(color=\"black\", dx=45, dy=60)\n",
" .encode(x=alt.datum(alt.DateTime(year=2008, month=\"November\")),\n",
" text=alt.datum(\"2008 Financial Crisis\"),\n",
" )\n",
"\n",
")\n",
"\n",
"label2 = (\n",
" alt.Chart()\n",
" .mark_rect(color=\"#cccccc33\")\n",
" .encode(\n",
" x=alt.datum(alt.DateTime(year=2008, month=\"January\")),\n",
" x2=alt.datum(alt.DateTime(year=2009, month=\"January\")),\n",
" text=alt.datum(\"2008 Financial Crisis\"),\n",
" )\n",
")\n",
"\n",
"\n",
"lines + label + xrule + xrule2 + label2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "44096965-f500-4ebf-81c5-c1e8df9d4ea1",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f652c1e0-ed86-45cd-b299-4ab662f2437d",
"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
}