{ "cells": [ { "cell_type": "markdown", "id": "b30269d6-9c9d-4a1f-a5e2-95c94a4877d6", "metadata": {}, "source": [ "# Introduction to Altair\n", "\n", "## Before we start: Jupyter Notebooks\n", "\n", "- Made up of \"cells\" which can be Python, markdown, or with extensions, any language.\n", "- Cells execute individually, and can execute out of order. Always re-order cells to be logical and make use of comments and/or markdown cells to make your notebook a narrative.\n", "- Tip: Before submitting a notebook, restart the kernel and run all cells in order. (See Kernel menu for options.) Ensure that you didn't inadvertently break things.\n", "- Not particularly Git friendly as the contents of the notebook are stored in JSON. (Still keep them in Git! even if suboptimal better than losing work!)\n", "- An alternative is `marimo` notebooks, which fix a few of the above issues. Still new & there are some rough edges still so I'll be sticking with Jupyter.\n", "\n", "`uv run jupyter lab` or `uv run jupyter notebook` will start the local notebook server (Lab is a newer UI, Notebook is the traditional one). You may opt to use VS Code, but I personallyh find their interface for notebooks more confusing than helpful.\n", "\n", "### Style Addendum (for all assignments in this course)\n", "\n", "- Notebook style imports allowed/preferred.\n", "- Limited use of global variables permitted with useful names and comments.\n", "- Notebooks must execute sequentially!" ] }, { "cell_type": "markdown", "id": "MJUe", "metadata": {}, "source": [ "## Altair expects \"tidy\" data\n", "\n", "Altair expects our data to be [tidy](http://vita.had.co.nz/papers/tidy-data.html).\n", "\n", "- Each variable is a column.\n", "- Each observation is a row.\n", "- Each type of observational unit is a table.\n", "\n", "You may use `pandas` or `polars` DataFrames. \n", "Class examples will focus on pre-cleaned data, and I will use polars in examples, though rarely will what I show differ significantly in pandas." ] }, { "cell_type": "code", "execution_count": 18, "id": "Hbol", "metadata": {}, "outputs": [], "source": [ "# best to follow convention, \"notebook\" style imports are allowed/preferred\n", "import marimo as mo\n", "import altair as alt\n", "import polars as pl\n", "from pathlib import Path" ] }, { "cell_type": "code", "execution_count": 22, "id": "vblA", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
state | session_start_year | num_bills |
---|---|---|
str | i64 | i64 |
"IL" | 2017 | 13616 |
"IL" | 2019 | 12760 |
"IL" | 2021 | 12847 |
"IL" | 2023 | 11951 |
"MI" | 2017 | 4818 |
… | … | … |
"MI" | 2023 | 3424 |
"WI" | 2017 | 1820 |
"WI" | 2019 | 2264 |
"WI" | 2021 | 2618 |
"WI" | 2023 | 2656 |