Altair Marks & Encodings
π¨ Mark types
alt.Chart(df).mark_point() # scatter points
alt.Chart(df).mark_circle() # filled circles
alt.Chart(df).mark_square() # filled squares
alt.Chart(df).mark_line() # line
alt.Chart(df).mark_line(point=True) # line + visible points
alt.Chart(df).mark_bar() # bar chart
alt.Chart(df).mark_area() # filled area
alt.Chart(df).mark_rect() # heatmap-style cells
alt.Chart(df).mark_arc() # pie / donut
alt.Chart(df).mark_boxplot() # box plot
alt.Chart(df).mark_tick() # tick marks (strip plot style)
alt.Chart(df).mark_text() # text labels as marks
alt.Chart(df).mark_rule() # reference lines
alt.Chart(df).mark_geoshape() # mapsπ― Encoding channels
alt.Chart(df).mark_point().encode(
x="col1",
y="col2",
color="category",
size="value",
shape="group",
opacity="confidence",
tooltip=["col1", "col2", "category"]
)| Channel | Maps a variable to |
|---|---|
x, y | position |
color | color |
size | point/mark size |
shape | marker shape (point marks only) |
opacity | transparency |
tooltip | hover info (list of columns) |
column, row | small multiples β see Altair Composition & Layouts |
order | draw/stacking order |
text | label content (mark_text only) |
π Bar chart
alt.Chart(df).mark_bar().encode(x="category:N", y="value:Q")
alt.Chart(df).mark_bar().encode(x="category:N", y="value:Q", color="group:N") # grouped/stacked automatically
alt.Chart(df).mark_bar().encode(y="category:N", x="value:Q") # horizontal β just swap x/yπ Line chart
alt.Chart(df).mark_line().encode(x="date:T", y="value:Q", color="category:N")π΅ Scatter with multiple channels
alt.Chart(df).mark_circle().encode(
x="horsepower:Q",
y="mpg:Q",
color="origin:N",
size="weight:Q",
tooltip=["name", "horsepower", "mpg"]
)
tooltip=[...]is nearly free interactivity β always worth adding once a chart has more than 2-3 encoded variables, so hidden context is one hover away.
ποΈ Mark-level styling (fixed, not data-driven)
alt.Chart(df).mark_bar(color="steelblue", opacity=0.8)
alt.Chart(df).mark_point(size=100, filled=True)
alt.Chart(df).mark_line(strokeWidth=3, strokeDash=[5,5])Setting
color=insidemark_bar(color=...)is a FIXED color for every mark. Settingcolor=inside.encode(color=...)is DATA-DRIVEN. Easy to mix these up.