{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "adf7aa23-daf2-4cfe-9910-2b00576b2ff7", "metadata": {}, "outputs": [], "source": [ "import polars as pl\n", "import altair as alt" ] }, { "cell_type": "markdown", "id": "0726aa55-4e9f-4f0f-a522-dc231c3f2c41", "metadata": {}, "source": [ "These are examples used in class, there's no additional content here, but you can see how the examples were created." ] }, { "cell_type": "code", "execution_count": 118, "id": "05a4d76e-7ade-431a-a773-d438c6ac9c79", "metadata": {}, "outputs": [], "source": [ "random_df = pl.DataFrame({\n", " \"a\": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],\n", " \"b\": [\"a\", \"b\", \"q\", \"c\", \"a\", \"c\", \"n\", \"c\", \"p\", \"b\", \"c\", \"n\", \"q\", \"r\", \"a\", \"b\", \"c\", \"b\", \"b\", \"b\"],\n", " \"c\": [500, 2, 3, 4, 10, 20, 100, 490, 400, 140, 200, 180, 380, 350, 180, 135, 400, 210, 230, 300]\n", "})" ] }, { "cell_type": "code", "execution_count": 122, "id": "902298fe-3a6c-446e-a060-f95071acedf1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alt.Chart(random_df).mark_point().encode(\n", " alt.X(\"a\"),\n", " alt.Y(\"c\"),\n", " alt.Color(\"b\", legend=None),\n", " alt.Size(\"c\", legend=None),\n", " alt.Shape(\"a:N\", legend=None),\n", " alt.Fill(\"b\", legend=None),\n", " alt.Opacity(\"b\", legend=None),\n", ") " ] }, { "cell_type": "code", "execution_count": 120, "id": "87228f27-baad-4acb-80af-1ca44c21e8fe", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "code", "execution_count": 123, "id": "5515ea60-70c1-4fd7-bef9-b5451c2ea00c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alt.Chart(random_df).mark_line().encode(\n", " x=\"a\",\n", " y=\"c\",\n", ")" ] }, { "cell_type": "code", "execution_count": 143, "id": "ba3df4b2-e64e-4574-b104-068ebe8d76c2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stevens = {\"brightness\": 0.4, \"depth perception\": 0.63, \"area\": 0.7, \"2D position\": 1, \n", " \"color saturation\": 1.7, \n", " # \"electric shock\": 3.5\n", " }\n", "\n", "rows = []\n", "for x in [0,0.2,0.4,0.6, 0.8, 1.0, 1.25, 1.5, 1.75, 2, 3]:\n", " for cat, exponent in stevens.items():\n", " rows.append({\"category\": cat, \"delta-stimulus\": x, \"response\": x**exponent})\n", "\n", "stevens_df = pl.DataFrame(rows)\n", "\n", "alt.Chart(stevens_df).mark_line().encode(\n", " alt.Color(\"category\", legend=None),\n", " alt.X(\"delta-stimulus\"),\n", " alt.Y(\"response\"),\n", ") + alt.Chart(stevens_df).mark_text(align=\"left\", dx=2).encode(\n", " alt.Color(\"category\"),\n", " alt.Text(\"category\"),\n", " alt.X(\"delta-stimulus\", aggregate=\"max\"),\n", " alt.Y(\"response\", aggregate=\"max\"),\n", ").properties()" ] }, { "cell_type": "code", "execution_count": 138, "id": "95dc101c-1dda-413a-87fb-b700fe5f43e3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9ad736e1-0e19-46d1-8e93-2b1409ffffc1", "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 }