{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "s-_LXMfxPYNn" }, "source": [ "# Scales, Axes, and Legends\n", "\n", "Visual encoding – mapping data to visual variables such as position, size, shape, or color – is the beating heart of data visualization. The workhorse that actually performs this mapping is the *scale*: a function that takes a data value as input (the scale *domain*) and returns a visual value, such as a pixel position or RGB color, as output (the scale *range*). Of course, a visualization is useless if no one can figure out what it conveys! In addition to graphical marks, a chart needs reference elements, or *guides*, that allow readers to decode the graphic. Guides such as *axes* (which visualize scales with spatial ranges) and *legends* (which visualize scales with color, size, or shape ranges), are the unsung heroes of effective data visualization!\n", "\n", "In this notebook, we will explore the options Altair provides to support customized designs of scale mappings, axes, and legends, using a running example about the effectiveness of antibiotic drugs.\n", "\n", "_This notebook is part of the [data visualization curriculum](https://github.com/uwdata/visualization-curriculum)._" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": {}, "colab_type": "code", "id": "9-iDk57UO3ij" }, "outputs": [], "source": [ "import pandas as pd\n", "import altair as alt" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "2bgy9xz4J2vb" }, "source": [ "## Antibiotics Data" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "cXYJkW_p6zGN" }, "source": [ "After World War II, antibiotics were considered \"wonder drugs\", as they were an easy remedy for what had been intractable ailments. To learn which drug worked most effectively for which bacterial infection, performance of the three most popular antibiotics on 16 bacteria were gathered." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "YRXEnlzG8Cpt" }, "source": [ "We will be using an antibiotics dataset from the [vega-datasets collection](https://github.com/vega/vega-datasets). In the examples below, we will pass the URL directly to Altair:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": {}, "colab_type": "code", "id": "jbtcg3fmJ5_A" }, "outputs": [], "source": [ "antibiotics = 'https://cdn.jsdelivr.net/npm/vega-datasets@1/data/burtin.json'" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "MGHC8Fft8L2E" }, "source": [ "We can first load the data with Pandas to view the dataset in its entirety and get acquainted with the available fields:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 545 }, "colab_type": "code", "id": "WoMzr0HE6ipZ", "outputId": "fa9edadf-8744-4514-d8ba-5401b7f66bed" }, "outputs": [ { "data": { "text/html": [ "
\n", " | Bacteria | \n", "Penicillin | \n", "Streptomycin | \n", "Neomycin | \n", "Gram_Staining | \n", "Genus | \n", "
---|---|---|---|---|---|---|
0 | \n", "Aerobacter aerogenes | \n", "870.000 | \n", "1.00 | \n", "1.600 | \n", "negative | \n", "other | \n", "
1 | \n", "Bacillus anthracis | \n", "0.001 | \n", "0.01 | \n", "0.007 | \n", "positive | \n", "other | \n", "
2 | \n", "Brucella abortus | \n", "1.000 | \n", "2.00 | \n", "0.020 | \n", "negative | \n", "other | \n", "
3 | \n", "Diplococcus pneumoniae | \n", "0.005 | \n", "11.00 | \n", "10.000 | \n", "positive | \n", "other | \n", "
4 | \n", "Escherichia coli | \n", "100.000 | \n", "0.40 | \n", "0.100 | \n", "negative | \n", "other | \n", "
5 | \n", "Klebsiella pneumoniae | \n", "850.000 | \n", "1.20 | \n", "1.000 | \n", "negative | \n", "other | \n", "
6 | \n", "Mycobacterium tuberculosis | \n", "800.000 | \n", "5.00 | \n", "2.000 | \n", "negative | \n", "other | \n", "
7 | \n", "Proteus vulgaris | \n", "3.000 | \n", "0.10 | \n", "0.100 | \n", "negative | \n", "other | \n", "
8 | \n", "Pseudomonas aeruginosa | \n", "850.000 | \n", "2.00 | \n", "0.400 | \n", "negative | \n", "other | \n", "
9 | \n", "Salmonella (Eberthella) typhosa | \n", "1.000 | \n", "0.40 | \n", "0.008 | \n", "negative | \n", "Salmonella | \n", "
10 | \n", "Salmonella schottmuelleri | \n", "10.000 | \n", "0.80 | \n", "0.090 | \n", "negative | \n", "Salmonella | \n", "
11 | \n", "Staphylococcus albus | \n", "0.007 | \n", "0.10 | \n", "0.001 | \n", "positive | \n", "Staphylococcus | \n", "
12 | \n", "Staphylococcus aureus | \n", "0.030 | \n", "0.03 | \n", "0.001 | \n", "positive | \n", "Staphylococcus | \n", "
13 | \n", "Streptococcus fecalis | \n", "1.000 | \n", "1.00 | \n", "0.100 | \n", "positive | \n", "Streptococcus | \n", "
14 | \n", "Streptococcus hemolyticus | \n", "0.001 | \n", "14.00 | \n", "10.000 | \n", "positive | \n", "Streptococcus | \n", "
15 | \n", "Streptococcus viridans | \n", "0.005 | \n", "10.00 | \n", "40.000 | \n", "positive | \n", "Streptococcus | \n", "