liveBook · Manning

Mark_line()

As explained in Data Storytelling with Altair and AI

mark_line() is an Altair method that creates line marks for connecting data values in a visualization.

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

How Data Storytelling with Altair and AI covers it

How deep it goes
Demonstrates data preparation, encodings, styling, filtering, and layered end labels rather than line-mark internals.
Examples
Uses tourist arrivals, Portugal air passengers, world population, and monthly pizza-versus-spaghetti orders.
What it assumes
Assumes pandas DataFrames and Altair chart construction while demonstrating melt, filtering, aggregation, and encodings through examples.
What you can do after
Apply it via Altair to build and refine multi-series trend charts with focused visual emphasis and direct labels.
How it connects
Connects to Altair encodings, color channels, mark_text(), transform_filter(), data aggregation, and the DIKW pyramid.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Storytelling with Altair and AI. The section links point to the parts of the book it draws on.