An encoding maps a data field to a visual property of chart marks. It answers where or how values appear: position on an axis, color, size, shape, text, or a tooltip. In Altair, a chart combines data, a mark, and encodings; the encoding declares the intended representation rather than manually drawing chart details. section 2.1.3 section 2.1
Choose a channel according to the question the chart should answer. Position channels such as x and y locate values. Mark-property channels control appearance, including color, size, shape, opacity, and stroke. Text and tooltip channels add annotations. Geographic and range-related channels extend the same mapping idea to locations, intervals, and arcs. section 3.2.1 section 3.2.1
Every encoded column needs a data type. Use quantitative for numerical continuous values, nominal for unordered categories, ordinal for ordered categories, temporal for dates or times, and GeoJSON for geographic values. Altair accepts either an explicit type property or shorthand after a colon, such as Q and N. section 3.2.1 section 2.1.3
Map age to a bar chart
Given Name, Age, and City columns, create a bar chart and encode Age on x as quantitative and Name on y as nominal: `alt.Chart(df).mark_bar().encode(x='Age:Q', y='Name:N')`. The mapping places each person by name and represents age through horizontal position. Add City to color when country- or category-level grouping should be visible. section 2.1.3 section 4.4.3
Encodings work with marks but are distinct from them: marks are bars, points, lines, or areas, while encodings assign data attributes to their visible properties. Conditions can vary those assignments by criteria; compound charts and interactivity build on the same chart specification. section 3.2 section 3.4
Table 3.1 The main encodings in Altair
|
Type
|
Channel
|
Description
|
|---|---|---|
|
Position
|
x, y
|
Horizontal and vertical positions
|
longitude, latitude
|
Geographical coordinates
|
|
xError, xError
|
Error values for
x and
y
|
|
x2,y2, longitude2, latitude2
|
Second positions for ranges
|
|
theta, theta2
|
Start and end arc angles
|
|
|
Mark property
|
angle, radius
|
The angle and the radius of the mark
|
color, fill
|
The color and the fill of the mark
|
|
opacity, fillOpacity
|
The opacity and the fill opacity of the mark
|
|
shape, size
|
The shape and the size of the mark
|
|
stroke, strokeDash, strokeOpacity, strokeWidth
|
Stroke properties of the mark
|
|
|
Text and tooltip
|
text
|
The text of the mark
|
tooltip
|
The tooltip of the mark
|
Browse the channel reference to select position, appearance, text, tooltip, geographic, range, and arc encodings for an Altair chart. section 3.2.1
