Altair’s mark_rule() adds rule marks—horizontal or vertical reference lines—that give charts a clear comparison point or visual guide. section 4.5.2 section 6.3.2
Use mark_rule() to make chart comparisons easier:
- Create a horizontal baseline, such as a red line at 50% improvement, to show which values exceed a target. section 4.5.2
- Create vertical guide lines from numeric x-values, then style them with color, stroke width, and opacity. section 6.3.2
- Layer the rule chart with other chart elements using the + operator. section 4.5.2 section 6.3.2
Horizontal baseline
A 50% baseline can be built from a small DataFrame and encoded on y: line = alt.Chart(pd.DataFrame({'y': [50]})).mark_rule(color='red').encode(y='y'); chart = chart + line. The resulting chart makes the sports performing above the baseline immediately visible. section 4.5.2
Listing 4.17 The generated code to create a baseline
line = alt.Chart(pd.DataFrame({'y': [50]})).mark_rule(color='red').encode(y='y')
chart = chart + line
Inspect the complete generated code for a red horizontal rule at y=50 and its layered combination with the original chart. section 4.5.2
