liveBook · Manning

Scale

As explained in Data Storytelling with Altair and AI

A visualization scale maps data values to visual values such as positions, colors, or sizes.

A visualization scale maps input data values to visual values such as colors, positions, or sizes. In Altair, a color scale can define both its input domain and output range: values from 0 to 100 might map from blue to red. section 8.2.1

Use scales to make encoded differences visible and intentional. A color channel can select a palette through scheme or specify a custom range with alt.Scale; a categorical field such as Country can use a default scheme such as set2. section 8.2.1 section 2.3.1

Scaling a color encoding

The following encoding assigns category values to colors and lets Altair select the palette: color=alt.Color('Category:N') To choose a specific scheme, configure the scale explicitly: color=alt.Color('Country:N', scale=alt.Scale(scheme='set2')) For a quantitative color field, specify the mapping limits and outputs, such as domain=[0, 100] and range=['blue', 'red']. section 8.2.1 section 2.3.1

Listing 8.1 Setting colors in Altair
import pandas as pd
import altair as alt

df = pd.DataFrame({
    'Category' : ['A', 'A', 'B', 'B', 'C', 'C', 'C'],
    'Cost' : [12,45,64,23,45,78,72],
    'Sales' : [34,56,45,34,44,23,23],
    'Product' : ['P1','P2','P3','P4','P5','P6','P7']
})

chart = alt.Chart(df).mark_bar().encode(
        x=alt.X('Product:N'),
        y=alt.Y('Cost:Q'),
        color=alt.Color('Category:N')
    )

chart.save('chart.html')

A runnable Altair example maps product categories to bar colors while encoding cost on the y-axis. section 8.2.1

How Data Storytelling with Altair and AI covers it

How deep it goes
Defines domains, ranges, and schemes with examples, but stops short of explaining scale internals.
Examples
Uses category-colored bars, quantitative blue-to-red mappings, and country lines colored with the set2 scheme.
What it assumes
Assumes familiarity with Altair channels and introduces scale configuration through color-channel examples.
What you can do after
Apply it via Altair to configure color palettes, domains, and ranges for encoded chart values.
How it connects
Connects color encoding with quantitative variables, categorical variables, domains, ranges, and color schemes.

Also explained in

Compare how 2 books explain scale →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Storytelling with Altair and AI. The section links point to the parts of the book it draws on.