diff --git a/altair-python/README.md b/altair-python/README.md new file mode 100644 index 0000000000..55f1953171 --- /dev/null +++ b/altair-python/README.md @@ -0,0 +1,3 @@ +# Altair: Declarative Charts With Python + +This folder provides the code examples for the Real Python tutorial [Altair: Declarative Charts With Python](https://realpython.com/altair-python/). diff --git a/altair-python/altair-python.ipynb b/altair-python/altair-python.ipynb new file mode 100644 index 0000000000..476655994e --- /dev/null +++ b/altair-python/altair-python.ipynb @@ -0,0 +1,732 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "7e0a276e-132b-4984-8c6a-9560eca313ac", + "metadata": {}, + "source": [ + "## Step Counts as a Bar Chart" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "9047ab05-f289-4fca-af14-9c1f75bc953e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "
\n", + "" + ], + "text/plain": [ + "alt.Chart(...)" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import altair as alt\n", + "import pandas as pd\n", + "\n", + "steps = pd.DataFrame(\n", + " {\n", + " \"Day\": [\"1-Mon\", \"2-Tue\", \"3-Wed\", \"4-Thu\", \"5-Fri\", \"6-Sat\", \"7-Sun\"],\n", + " \"Steps\": [6200, 8400, 7100, 9800, 5500, 9870, 3769],\n", + " }\n", + ")\n", + "\n", + "weekly_steps = (\n", + " alt.Chart(steps)\n", + " .mark_bar()\n", + " .encode(\n", + " x=\"Day\",\n", + " y=\"Steps\",\n", + " )\n", + ")\n", + "weekly_steps" + ] + }, + { + "cell_type": "markdown", + "id": "2fd3412b-83ef-40d8-94bc-16422c97c59b", + "metadata": {}, + "source": [ + "## The `movies` Dataset" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "8446e30e-7fd0-406e-89ff-b5fec360830f", + "metadata": {}, + "outputs": [], + "source": [ + "from altair.datasets import data\n", + "\n", + "movies = data.movies()\n", + "movies = movies.dropna(\n", + " subset=[\n", + " \"Production Budget\",\n", + " \"Worldwide Gross\",\n", + " \"IMDB Rating\",\n", + " \"Major Genre\",\n", + " \"MPAA Rating\",\n", + " ]\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "39c7c21e-9536-48bc-8bc9-c3da708c2eec", + "metadata": {}, + "source": [ + "## Basic Scatter Plot" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "4e25ea99-ec0f-4797-bc6c-46ab3cb36b22", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "
\n", + "" + ], + "text/plain": [ + "alt.Chart(...)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "scatter = (\n", + " alt.Chart(movies)\n", + " .mark_point()\n", + " .encode(\n", + " x=\"Production Budget:Q\",\n", + " y=\"Worldwide Gross:Q\",\n", + " )\n", + ")\n", + "scatter" + ] + }, + { + "cell_type": "markdown", + "id": "62d51fc2-c1e2-4398-a0bb-e4ab3a656832", + "metadata": {}, + "source": [ + "## Scatter Plot With Color and Size Encoding Channels" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "6a6f25c8-7514-42ba-ba40-b2fd3c263e35", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "
\n", + "" + ], + "text/plain": [ + "alt.Chart(...)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "scatter = (\n", + " alt.Chart(movies)\n", + " .mark_point()\n", + " .encode(\n", + " x=\"Production Budget:Q\",\n", + " y=\"Worldwide Gross:Q\",\n", + " color=\"Major Genre:N\",\n", + " size=\"IMDB Rating:Q\",\n", + " tooltip=[\"Title:N\", \"IMDB Rating:Q\"],\n", + " )\n", + ")\n", + "scatter" + ] + }, + { + "cell_type": "markdown", + "id": "9ff6c75f-6ef6-4c9e-a058-f7fdaa98864f", + "metadata": {}, + "source": [ + "## Faceted Scatter Plot Split by MPAA Rating" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "4d895cbb-463d-495f-aacf-cba27d2878dd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "
\n", + "" + ], + "text/plain": [ + "alt.Chart(...)" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "scatter = (\n", + " alt.Chart(movies)\n", + " .mark_point()\n", + " .encode(\n", + " x=\"Production Budget:Q\",\n", + " y=\"Worldwide Gross:Q\",\n", + " color=\"Major Genre:N\",\n", + " size=\"IMDB Rating:Q\",\n", + " tooltip=[\"Title:N\", \"IMDB Rating:Q\"],\n", + " column=\"MPAA Rating:O\",\n", + " )\n", + ")\n", + "scatter" + ] + }, + { + "cell_type": "markdown", + "id": "3d7de8c7-f8f6-4673-9bc2-321f216c486a", + "metadata": {}, + "source": [ + "## Basic Scatter Plot With Brush" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "260eb204-8fd3-4a68-877d-4c5471004da9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "
\n", + "" + ], + "text/plain": [ + "alt.Chart(...)" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "brush = alt.selection_interval()\n", + "\n", + "scatter = (\n", + " alt.Chart(movies)\n", + " .mark_point()\n", + " .encode(\n", + " x=\"Production Budget:Q\",\n", + " y=\"Worldwide Gross:Q\",\n", + " color=(\n", + " alt.when(brush)\n", + " .then(\"Major Genre:N\")\n", + " .otherwise(alt.value(\"lightgray\"))\n", + " ),\n", + " )\n", + " .add_params(brush)\n", + ")\n", + "\n", + "scatter" + ] + }, + { + "cell_type": "markdown", + "id": "c98813a3-48c5-4f55-b0c8-78cb0e53573f", + "metadata": {}, + "source": [ + "## Scatter Plot Connexted With Bars Plot" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "eb3aef15-7e39-47ea-936a-3c02c80462ad", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "
\n", + "" + ], + "text/plain": [ + "alt.VConcatChart(...)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bars = (\n", + " alt.Chart(movies)\n", + " .mark_bar()\n", + " .encode(\n", + " x=\"mean(IMDB Rating):Q\",\n", + " y=\"Major Genre:N\",\n", + " )\n", + " .transform_filter(brush)\n", + ")\n", + "\n", + "scatter & bars" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6984104a-ff47-475c-a1b3-e84d5f81a6dd", + "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.13.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/altair-python/scatter_basic.py b/altair-python/scatter_basic.py new file mode 100644 index 0000000000..0f798cbed4 --- /dev/null +++ b/altair-python/scatter_basic.py @@ -0,0 +1,20 @@ +import altair as alt +from altair.datasets import data + +movies = data.movies() +movies = movies.dropna( + subset=[ + "Production Budget", + "Worldwide Gross", + ] +) + +scatter = ( + alt.Chart(movies) + .mark_point() + .encode( + x="Production Budget:Q", + y="Worldwide Gross:Q", + ) +) +scatter diff --git a/altair-python/scatter_connected.py b/altair-python/scatter_connected.py new file mode 100644 index 0000000000..a43cb069c6 --- /dev/null +++ b/altair-python/scatter_connected.py @@ -0,0 +1,43 @@ +import altair as alt +from altair.datasets import data + +movies = data.movies() +movies = movies.dropna( + subset=[ + "Production Budget", + "Worldwide Gross", + "IMDB Rating", + "Major Genre", + ] +) + +brush = alt.selection_interval() + +scatter = ( + alt.Chart(movies) + .mark_point() + .encode( + x="Production Budget:Q", + y="Worldwide Gross:Q", + color=( + alt.when(brush) + .then("Major Genre:N") + .otherwise(alt.value("lightgray")), + ), + ) + .add_params(brush) +) + +scatter + +bars = ( + alt.Chart(movies) + .mark_bar() + .encode( + x="mean(IMDB Rating):Q", + y="Major Genre:N", + ) + .transform_filter(brush) +) + +scatter & bars diff --git a/altair-python/scatter_encoding_channels.py b/altair-python/scatter_encoding_channels.py new file mode 100644 index 0000000000..f815e4278e --- /dev/null +++ b/altair-python/scatter_encoding_channels.py @@ -0,0 +1,25 @@ +import altair as alt +from altair.datasets import data + +movies = data.movies() +movies = movies.dropna( + subset=[ + "Production Budget", + "Worldwide Gross", + "IMDB Rating", + "Major Genre", + ] +) + +scatter = ( + alt.Chart(movies) + .mark_point() + .encode( + x="Production Budget:Q", + y="Worldwide Gross:Q", + color="Major Genre:N", + size="IMDB Rating:Q", + tooltip=["Title:N", "IMDB Rating:Q"], + ) +) +scatter diff --git a/altair-python/scatter_faceted.py b/altair-python/scatter_faceted.py new file mode 100644 index 0000000000..dc4621b47e --- /dev/null +++ b/altair-python/scatter_faceted.py @@ -0,0 +1,27 @@ +import altair as alt +from altair.datasets import data + +movies = data.movies() +movies = movies.dropna( + subset=[ + "Production Budget", + "Worldwide Gross", + "IMDB Rating", + "Major Genre", + "MPAA Rating", + ] +) + +scatter = ( + alt.Chart(movies) + .mark_point() + .encode( + x="Production Budget:Q", + y="Worldwide Gross:Q", + color="Major Genre:N", + size="IMDB Rating:Q", + tooltip=["Title:N", "IMDB Rating:Q"], + column="MPAA Rating:O", + ) +) +scatter diff --git a/altair-python/steps.py b/altair-python/steps.py new file mode 100644 index 0000000000..102f2f49e3 --- /dev/null +++ b/altair-python/steps.py @@ -0,0 +1,19 @@ +import altair as alt +import pandas as pd + +steps = pd.DataFrame( + { + "Day": ["1-Mon", "2-Tue", "3-Wed", "4-Thu", "5-Fri", "6-Sat", "7-Sun"], + "Steps": [6200, 8400, 7100, 9800, 5500, 9870, 3769], + } +) + +weekly_steps = ( + alt.Chart(steps) + .mark_bar() + .encode( + x="Day", + y="Steps", + ) +) +weekly_steps