30239-notes/03.charts/examples.ipynb

291 lines
42 KiB
Plaintext
Raw Permalink 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",
2024-09-28 19:45:22 +00:00
"execution_count": 53,
2024-09-28 04:10:24 +00:00
"id": "44096965-f500-4ebf-81c5-c1e8df9d4ea1",
"metadata": {},
"outputs": [],
2024-09-28 19:45:22 +00:00
"source": [
"import math\n",
"import polars as pl"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "f652c1e0-ed86-45cd-b299-4ab662f2437d",
"metadata": {},
"outputs": [],
"source": [
"rows = []\n",
"for pt in range(100):\n",
" rows.append({\"percentage\": pt, \"theta\":pt / 100 * 2 * math.pi, \"area\": (pt / 100 * 100**2) / (100**2)})\n",
"area_df = pl.DataFrame(rows)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "813f6626-9ff4-4d9f-ad60-6a2bf508490a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<style>\n",
" #altair-viz-a9400806b87f46f4b7a6bcdd420dc59f.vega-embed {\n",
" width: 100%;\n",
" display: flex;\n",
" }\n",
"\n",
" #altair-viz-a9400806b87f46f4b7a6bcdd420dc59f.vega-embed details,\n",
" #altair-viz-a9400806b87f46f4b7a6bcdd420dc59f.vega-embed details summary {\n",
" position: relative;\n",
" }\n",
"</style>\n",
"<div id=\"altair-viz-a9400806b87f46f4b7a6bcdd420dc59f\"></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-a9400806b87f46f4b7a6bcdd420dc59f\") {\n",
" outputDiv = document.getElementById(\"altair-viz-a9400806b87f46f4b7a6bcdd420dc59f\");\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-fcf14b8ce611854e95dc031312dd6590\"}, \"mark\": {\"type\": \"point\"}, \"encoding\": {\"x\": {\"field\": \"percentage\", \"type\": \"quantitative\"}, \"y\": {\"field\": \"area\", \"type\": \"quantitative\"}}, \"$schema\": \"https://vega.github.io/schema/vega-lite/v5.20.1.json\", \"datasets\": {\"data-fcf14b8ce611854e95dc031312dd6590\": [{\"percentage\": 0, \"theta\": 0.0, \"area\": 0.0}, {\"percentage\": 1, \"theta\": 0.06283185307179587, \"area\": 0.01}, {\"percentage\": 2, \"theta\": 0.12566370614359174, \"area\": 0.02}, {\"percentage\": 3, \"theta\": 0.18849555921538758, \"area\": 0.03}, {\"percentage\": 4, \"theta\": 0.25132741228718347, \"area\": 0.04}, {\"percentage\": 5, \"theta\": 0.3141592653589793, \"area\": 0.05}, {\"percentage\": 6, \"theta\": 0.37699111843077515, \"area\": 0.06}, {\"percentage\": 7, \"theta\": 0.4398229715025711, \"area\": 0.07}, {\"percentage\": 8, \"theta\": 0.5026548245743669, \"area\": 0.08}, {\"percentage\": 9, \"theta\": 0.5654866776461628, \"area\": 0.09}, {\"percentage\": 10, \"theta\": 0.6283185307179586, \"area\": 0.1}, {\"percentage\": 11, \"theta\": 0.6911503837897545, \"area\": 0.11}, {\"percentage\": 12, \"theta\": 0.7539822368615503, \"area\": 0.12}, {\"percentage\": 13, \"theta\": 0.8168140899333463, \"area\": 0.13}, {\"percentage\": 14, \"theta\": 0.8796459430051422, \"area\": 0.14}, {\"percentage\": 15, \"theta\": 0.9424777960769379, \"area\": 0.15}, {\"percentage\": 16, \"theta\": 1.0053096491487339, \"area\": 0.16}, {\"percentage\": 17, \"theta\": 1.0681415022205298, \"area\": 0.17}, {\"percentage\": 18, \"theta\": 1.1309733552923256, \"area\": 0.18}, {\"percentage\": 19, \"theta\": 1.1938052083641213, \"area\": 0.19}, {\"percentage\": 20, \"theta\": 1.2566370614359172, \"area\": 0.2}, {\"percentage\": 21, \"theta\": 1.319468914507713, \"area\": 0.21}, {\"percentage\": 22, \"theta\": 1.382300767579509, \"area\": 0.22}, {\"percentage\": 23, \"theta\": 1.4451326206513049, \"area\": 0.23}, {\"percentage\": 24, \"theta\": 1.5079644737231006, \"area\": 0.24}, {\"percentage\": 25, \"theta\": 1.5707963267948966, \"area\": 0.25}, {\"percentage\": 26, \"theta\": 1.6336281798666925, \"area\": 0.26}, {\"percentage\": 27, \"theta\": 1.6964600329384885, \"area\": 0.27}, {\"percentage\": 28, \"theta\": 1.7592918860102844, \"area\": 0.28}, {\"percentage\": 29, \"theta\": 1.82212373908208, \"area\": 0.29}, {\"percentage\": 30, \"theta\": 1.8849555921538759, \"area\": 0.3}, {\"percentage\": 31, \"theta\": 1.9477874452256718, \"area\": 0.31}, {\"percentage\": 32, \"theta\": 2.0106192982974678, \"area\": 0.32}, {\"percentage\": 33, \"theta\": 2.0734511513692637, \"area\": 0.33}, {\"percentage\": 34, \"theta\": 2.1362830044410597, \"area\": 0.34}, {\"percentage\": 35, \"theta\": 2.199114857512855, \"area\": 0.35}, {\"percentage\": 36, \"theta\": 2.261946710584651, \"area\": 0.36}, {\"percentage\": 37, \"theta\": 2.324778563656447, \"area\": 0.37}, {\"percentage\": 38, \"theta\": 2.3876104167282426, \"area\": 0.38}, {\"percentage\": 39, \"theta\": 2.4504422698000385, \"area\": 0.39}, {\"percentage\": 40, \"theta\": 2.5132741228718345, \"area\": 0.4}, {\"percentage\": 41, \"theta\": 2.57610597594363, \"area\": 0.41}, {\"percentage\": 42, \"theta\": 2.638937829015426, \"area\": 0.42}, {\"percentage\": 43, \"theta\": 2.701769682087222, \"area\": 0.43}, {\"percentage\": 44, \"theta\": 2.764601535159018, \"area\": 0.44}, {\"percentage\": 45, \"theta\": 2.827433388230814, \"area\": 0.45}, {\"percentage\": 46, \"theta\": 2.8902652413026098, \"area\": 0.46}, {\"percentage\": 47, \"theta\": 2.9530970943744053, \"area\": 0.47}, {\"percentage\": 48, \"theta\": 3.015928947446201, \"area\": 0.48}, {\"percentage\": 49, \"theta\": 3.078760800517997, \"area\": 0.49}, {\"percentage\": 50, \"theta\": 3.141592653589793, \"area\": 0.5}, {\"percentage\": 51, \"theta\": 3.204424506661589, \"area\": 0.51}, {\"percentage\": 52, \"theta\": 3.267256359733385, \"area\": 0.52}, {\"percentage\": 53, \"
"</script>"
],
"text/plain": [
"alt.Chart(...)"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"alt.Chart(area_df).mark_point().encode(x=\"percentage\", y=\"area\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "abf756b4-3ccd-4ea3-972b-ac36926a39b3",
"metadata": {},
"outputs": [],
2024-09-28 04:10:24 +00:00
"source": []
},
{
"cell_type": "code",
"execution_count": null,
2024-09-28 19:45:22 +00:00
"id": "b4c75d21-2a82-4326-87b0-8f4b788a3d38",
2024-09-28 04:10:24 +00:00
"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
}