{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Altair in `JupyterLite`\n", "\n", "**Altair** is a declarative statistical visualization library for Python.\n", "\n", "Most of the examples below are from: https://altair-viz.github.io/gallery" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import the dependencies:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "trusted": true }, "outputs": [], "source": [ "%pip install -q altair" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simple Bar Chart" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "trusted": true }, "outputs": [], "source": [ "import altair as alt\n", "import pandas as pd\n", "\n", "source = pd.DataFrame({\n", " 'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],\n", " 'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]\n", "})\n", "\n", "alt.Chart(source).mark_bar().encode(\n", " x='a',\n", " y='b'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simple Heatmap" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "trusted": true }, "outputs": [], "source": [ "import altair as alt\n", "import numpy as np\n", "import pandas as pd\n", "\n", "# Compute x^2 + y^2 across a 2D grid\n", "x, y = np.meshgrid(range(-5, 5), range(-5, 5))\n", "z = x ** 2 + y ** 2\n", "\n", "# Convert this grid to columnar data expected by Altair\n", "source = pd.DataFrame({'x': x.ravel(),\n", " 'y': y.ravel(),\n", " 'z': z.ravel()})\n", "\n", "alt.Chart(source).mark_rect().encode(\n", " x='x:O',\n", " y='y:O',\n", " color='z:Q'\n", ")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Install the Vega Dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "trusted": true }, "outputs": [], "source": [ "%pip install -q vega_datasets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interactive Average" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "trusted": true }, "outputs": [], "source": [ "import altair as alt\n", "from vega_datasets import data\n", "\n", "source = data.seattle_weather()\n", "brush = alt.selection(type='interval', encodings=['x'])\n", "\n", "bars = alt.Chart().mark_bar().encode(\n", " x='month(date):O',\n", " y='mean(precipitation):Q',\n", " opacity=alt.condition(brush, alt.OpacityValue(1), alt.OpacityValue(0.7)),\n", ").add_selection(\n", " brush\n", ")\n", "\n", "line = alt.Chart().mark_rule(color='firebrick').encode(\n", " y='mean(precipitation):Q',\n", " size=alt.SizeValue(3)\n", ").transform_filter(\n", " brush\n", ")\n", "\n", "alt.layer(bars, line, data=source)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Locations of US Airports" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "trusted": true }, "outputs": [], "source": [ "import altair as alt\n", "from vega_datasets import data\n", "\n", "airports = data.airports.url\n", "states = alt.topo_feature(data.us_10m.url, feature='states')\n", "\n", "# US states background\n", "background = alt.Chart(states).mark_geoshape(\n", " fill='lightgray',\n", " stroke='white'\n", ").properties(\n", " width=500,\n", " height=300\n", ").project('albersUsa')\n", "\n", "# airport positions on background\n", "points = alt.Chart(airports).transform_aggregate(\n", " latitude='mean(latitude)',\n", " longitude='mean(longitude)',\n", " count='count()',\n", " groupby=['state']\n", ").mark_circle().encode(\n", " longitude='longitude:Q',\n", " latitude='latitude:Q',\n", " size=alt.Size('count:Q', title='Number of Airports'),\n", " color=alt.value('steelblue'),\n", " tooltip=['state:N','count:Q']\n", ").properties(\n", " title='Number of airports in US'\n", ")\n", "\n", "background + points\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python (Pyodide)", "language": "python", "name": "python" }, "language_info": { "codemirror_mode": { "name": "python", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8" } }, "nbformat": 4, "nbformat_minor": 4 }