mark_line() creates line marks in an Altair chart. It is useful when values have an order—such as dates, years, or months—and connecting them makes a trend visible. A line chart still depends on its encodings: x and y identify position, while color can separate multiple series. Start with a raw view to inspect the data, then refine the data and visual emphasis until the chart communicates one useful point. section 2.3.1 section 6.3.3
Plot a time series before drawing conclusions
A tourist-arrivals dataset is reshaped so Date identifies each observation, Country identifies the series, and Tourist Arrivals supplies the value. Applying mark_line() and encoding Date on x, Tourist Arrivals on y, and Country with color produces one trend line per country. The initial chart reveals an overall increase, but overlapping lines make detailed comparison difficult. Missing boundary years also matter: restrict the range to years with usable data, aggregate arrivals by year and country, then redraw the lines. Converting each series to percentage increase from 1994 makes Portugal’s relative growth clearer; a wider stroke then highlights Portugal without removing the comparison lines. section 2.3.1
Use mark_line() as part of a deliberate chart-building sequence:
- Prepare the data at the intended level of comparison. Melt wide data when needed, filter incomplete periods, and aggregate values before plotting. section 2.3.1 section 3.3
- Encode the ordered field on x and the measured field on y. Add color for categories only when multiple series need comparison. section 2.3.1 section 3.3
- Build a reusable base chart when lines and text share the same encodings, then apply mark_line() to that base. section 6.3.3
- Reduce clutter when the message requires focus. Highlight one series with stroke width, suppress an unnecessary legend, or label lines at their final values. section 2.3.1 section 6.3.3
Label lines directly instead of relying on a legend
For monthly pizza and spaghetti orders, define a base chart with Month on x, Number of Orders on y, and Meal Type as color. Apply mark_line() to draw the two series. Reuse the same base with mark_text(), filter to December, and place each meal-type label beside its line end. Removing the legend and grid keeps attention on the two labeled trends. section 6.3.3
A raw line chart can be the beginning rather than the final result. Plotting every country’s population from 1960 to 2021 with a different color exposes why selection matters: too many countries, too many colors, and no focus on North America create a confusing chart. Treat that confusion as evidence to narrow the question and refine the view, not as a reason to make the line mark carry every message alone. section 3.3
Listing 6.13 How to build the line chart and the labels
chart = base.mark_line()
text = base.mark_text(
fontSize=14,
baseline='middle',
align='left',
dx=10
).encode(
text=alt.Text('Meal Type:N'),
).transform_filter(
alt.datum['Month'] == 'December'
)
chart = chart + text #1
chart = chart.configure_view(
strokeWidth=0
).configure_axis(
grid=False
)
chart.save('line-chart.html')
Inspect a compact pattern for sharing encodings between line and text marks, filtering labels to the final month, and combining the layers. section 6.3.3
