Channels#
Altair provides a number of encoding channels that can be useful in different circumstances. The following sections summarize them:
Position#
Channel |
Altair Class |
Description |
Example |
---|---|---|---|
x |
The x-axis value |
||
y |
The y-axis value |
||
x2 |
Second x value for ranges |
||
y2 |
Second y value for ranges |
||
longitude |
Longitude for geo charts |
||
latitude |
Latitude for geo charts |
||
longitude2 |
Second longitude value for ranges |
||
latitude2 |
Second latitude value for ranges |
||
xError |
The x-axis error value |
N/A |
|
yError |
The y-axis error value |
N/A |
|
xError2 |
The second x-axis error value |
N/A |
|
yError2 |
The second y-axis error value |
N/A |
|
xOffset |
Offset to the x position |
||
yOffset |
Offset to the y position |
||
theta |
The start arc angle |
||
theta2 |
The end arc angle (radian) |
Mark Property#
Channel |
Altair Class |
Description |
Example |
---|---|---|---|
angle |
The angle of the mark |
||
color |
The color of the mark |
||
fill |
The fill for the mark |
||
fillopacity |
The opacity of the mark’s fill |
N/A |
|
opacity |
The opacity of the mark |
||
radius |
The radius or the mark |
||
shape |
The shape of the mark |
||
size |
The size of the mark |
||
stroke |
The stroke of the mark |
N/A |
|
strokeDash |
The stroke dash style |
||
strokeOpacity |
The opacity of the line |
N/A |
|
strokeWidth |
The width of the line |
N/A |
Text and Tooltip#
Channel |
Altair Class |
Description |
Example |
---|---|---|---|
text |
Text to use for the mark |
||
tooltip |
The tooltip value |
Hyperlink#
Channel |
Altair Class |
Description |
Example |
---|---|---|---|
href |
Hyperlink for points |
Detail#
Grouping data is an important operation in data visualization. For line and area marks,
mapping an unaggregated data field to any
non-position channel will group the lines and stacked areas by that field.
For aggregated plots, all unaggregated fields encoded are used as grouping fields
in the aggregation (similar to fields in GROUP BY
in SQL).
The detail
channel specifies an additional grouping field (or fields) for grouping
data without mapping the field(s) to any visual properties.
Channel |
Altair Class |
Description |
Example |
---|---|---|---|
detail |
Additional property to group by |
For example here is a line chart showing stock prices of 5 tech companies over time.
We map the symbol
variable to detail
to use them to group lines.
import altair as alt
from vega_datasets import data
source = data.stocks()
alt.Chart(source).mark_line().encode(
x="date:T",
y="price:Q",
detail="symbol:N"
)
Order#
The order
option and Order
channel can sort how marks are drawn on the chart.
For stacked marks, this controls the order of components of the stack. Here, the elements of each bar are sorted alphabetically by the name of the nominal data in the color channel.
import altair as alt
from vega_datasets import data
barley = data.barley()
alt.Chart(barley).mark_bar().encode(
x='variety:N',
y='sum(yield):Q',
color='site:N',
order=alt.Order("site").sort("ascending")
)
The order can be reversed by changing the sort option to descending.
import altair as alt
from vega_datasets import data
barley = data.barley()
alt.Chart(barley).mark_bar().encode(
x='variety:N',
y='sum(yield):Q',
color='site:N',
order=alt.Order("site").sort("descending")
)
The same approach works for other mark types, like stacked areas charts.
import altair as alt
from vega_datasets import data
barley = data.barley()
alt.Chart(barley).mark_area().encode(
x='variety:N',
y='sum(yield):Q',
color='site:N',
order=alt.Order("site").sort("ascending")
)
Note that unlike the sort
parameter to positional encoding channels,
the Order
channel cannot take a list of values to sort by
and is not automatically sorted when an ordered pandas categorical column is passed.
If we want to sort stacked segments in a custom order, we can follow the approach in this issue comment, although there might be edge cases where this is not fully supported. This workaround also makes the order of the segments align with the order that the colors shows up in a legend that uses custom sorting for the color domain.
For line marks, the Order
channel encodes the order in which data points are connected. This can be useful for creating a scatter plot that draws lines between the dots using a different field than the x and y axes.
import altair as alt
from vega_datasets import data
driving = data.driving()
alt.Chart(driving).mark_line(point=True).encode(
alt.X('miles').scale(zero=False),
alt.Y('gas').scale(zero=False),
order='year'
)
Facet#
For more information, see Faceted Charts.
Channel |
Altair Class |
Description |
Example |
---|---|---|---|
column |
The column of a faceted plot |
||
row |
The row of a faceted plot |
||
facet |
The row and/or column of a general faceted plot |