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

