Quiz: Altair

Answer all 20 questions. Answers and explanations are hidden below each question.


Part I: Multiple Choice (10 questions)

Q1. Which researcher introduced the Grammar of Graphics in 1999?

A) Edward Tufte B) Leland Wilkinson C) Hadley Wickham D) William Cleveland

Answer **B.** Leland Wilkinson's 1999 book *The Grammar of Graphics* introduced the framework. Wickham popularized it through ggplot2; Cleveland and Tufte are separate visualization authorities with their own contributions.

Q2. What is Vega-Lite?

A) A JavaScript library for DOM manipulation B) A JSON-based visualization grammar rendered by a JavaScript library C) A Python plotting library built on matplotlib D) A database query language

Answer **B.** Vega-Lite is a JSON specification language for statistical graphics. The Vega-Lite JavaScript library reads the JSON and produces the rendered chart. Altair is the Python binding that generates Vega-Lite specs from Python code.

Q3. The canonical Altair pattern is:

A) plt.plot(data) B) alt.Chart(data).mark_*().encode(...) C) sns.scatterplot(data) D) go.Scatter(data)

Answer **B.** Every Altair chart starts with `alt.Chart(data)`, then adds a mark type (`mark_point`, `mark_line`, etc.), and finally specifies encodings via `.encode(...)`. Additional methods add scales, transforms, and composition.

Q4. Which data-type shorthand is used for a date column?

A) :D B) :T C) :Q D) :N

Answer **B.** `:T` means temporal. `:Q` is quantitative, `:N` is nominal, `:O` is ordinal. Altair uses these four types to infer appropriate scales and axis formats.

Q5. Which Altair composition operator stacks charts vertically?

A) + B) | C) & D) .facet()

Answer **C.** `&` concatenates vertically, `|` concatenates horizontally, `+` layers on the same axes, `.facet()` creates small multiples.

Q6. What is the purpose of transform_filter(brush)?

A) To filter the DataFrame in pandas before plotting B) To hide unwanted traces from the legend C) To filter the chart's data based on a selection, so that the chart updates when the selection changes D) To apply a matplotlib filter to the output image

Answer **C.** `transform_filter` applies a predicate (often a selection) inside the chart spec, so the filter re-runs when the selection changes. This is how linked views work: brushing chart A changes chart B via `transform_filter(brush)`.

Q7. Which Altair function creates an interval brush selection?

A) alt.brush() B) alt.selection_interval() C) alt.selection_point() D) alt.range()

Answer **B.** `alt.selection_interval()` creates an interval brush. `alt.selection_point()` creates a click-based point or category selection.

Q8. The famous 5000-row limit in Altair can be removed with:

A) alt.set_row_limit(None) B) alt.data_transformers.disable_max_rows() C) alt.Chart.no_limit() D) You cannot remove it

Answer **B.** `alt.data_transformers.disable_max_rows()` removes the cap entirely. For very large datasets, enable VegaFusion (`alt.data_transformers.enable("vegafusion")`) for more scalable handling.

Q9. What does alt.value(0.5) do in an encoding?

A) Maps a column to the value 0.5 B) Sets a constant value of 0.5 for the encoded channel C) Filters to values at 0.5 D) Creates a scale with midpoint 0.5

Answer **B.** `alt.value(x)` sets a constant value rather than mapping from a data column. Useful for setting a constant opacity, size, or color without moving it to the mark-level style.

Q10. Which library provides Altair's static image export?

A) altair_saver or vl-convert-python B) matplotlib C) kaleido D) pillow

Answer **A.** `altair_saver` (older) or `vl-convert-python` (newer) handles PNG/SVG/PDF export. Plotly uses kaleido; matplotlib uses its own backends; Altair uses a separate helper.

Part II: Short Answer (10 questions)

Q11. Describe the difference between Altair's declarative approach and matplotlib's imperative approach.

Answer matplotlib requires step-by-step drawing commands (create axes, plot this, set that). Altair requires a declarative description of the chart: data, marks, encodings. You do not tell Altair how to draw; you tell it what the chart should look like. The renderer handles the how. This mirrors the SQL vs. procedural-programming distinction.

Q12. Write Altair code to create a scatter plot with a clickable legend that dims non-selected categories.

Answer
select = alt.selection_point(fields=["Origin"], bind="legend")
alt.Chart(cars).mark_point().encode(
    x="Horsepower:Q",
    y="Miles_per_Gallon:Q",
    color="Origin:N",
    opacity=alt.condition(select, alt.value(1.0), alt.value(0.1)),
).add_params(select)

Q13. Explain the difference between mark_point(size=100, color="blue") and encode(size="Population:Q", color="Region:N").

Answer The first sets constant mark-level properties: all points have size 100 and color blue. The second maps data columns to visual channels: points vary in size based on population and in color based on region. Mark-level is constant; encoding-level is data-driven.

Q14. What is the purpose of alt.X("col:Q", scale=alt.Scale(type="log")) as opposed to just x="col:Q"?

Answer The bare shorthand uses defaults. Wrapping in `alt.X` (or `alt.Y`, `alt.Color`, etc.) gives access to scale, axis, and other properties — logarithmic scale, explicit domain, custom tick format, custom title, etc. Use the wrapper when you need customization beyond the defaults.

Q15. Describe how Altair's .repeat() operator works and what chart type it is often used to produce.

Answer `.repeat(row=[...], column=[...])` takes a list of columns and reproduces the chart once for each combination, filling the `row` and `column` positions. The encoding uses `alt.repeat("row")` and `alt.repeat("column")` to reference the current iteration's column name. It is most often used to produce pair plots — a grid of pairwise scatter plots for a set of numeric columns, similar to seaborn's pairplot or Plotly's scatter_matrix.

Q16. What is the chapter's threshold concept, and how does it relate to SQL?

Answer "Declaration over instruction." In SQL you describe what data you want (`SELECT ... WHERE ...`), not how to retrieve it. In Altair you describe what chart you want (data + marks + encodings), not how to draw it. Both rely on a separate system (database engine, Vega-Lite renderer) to handle the implementation. The compositionality of SQL (joins, unions) has analogues in Altair (layering, concatenation).

Q17. Explain the purpose of transform_calculate and give an example.

Answer `transform_calculate(new_col="expression")` computes a new column from existing ones, using Vega's expression language. The computed column lives inside the chart spec — it is not added to the original DataFrame. Example: `transform_calculate(efficiency_tier="datum.Miles_per_Gallon > 25 ? 'Efficient' : 'Thirsty'")` creates a categorical column that can then be encoded as `color="efficiency_tier:N"`.

Q18. What is the practical difference between Altair and Plotly for linked-view dashboards?

Answer Altair makes linked views trivially declarative: define a selection, apply `transform_filter(selection)` to each dependent chart, and the charts update automatically when the selection changes. Plotly can achieve the same effect but requires custom `updatemenus` or Dash callbacks — more code, more state management, and more room for bugs. For linked-view-heavy projects, Altair is usually the cleaner choice.

Q19. List three limitations of Altair and describe when each becomes a problem.

Answer (1) **5000-row default limit** — a problem for datasets larger than 5000 rows until you enable VegaFusion or disable_max_rows. (2) **Limited 3D and custom marks** — a problem when you need 3D surfaces, Sankey diagrams, or other marks not in the built-in list. (3) **Less fine-grained layout control than matplotlib** — a problem when you need pixel-perfect subplot positioning. For these cases, switch tools.

Q20. Describe the Altair theme system and how it compares to matplotlib style sheets.

Answer Altair themes are functions registered with `alt.themes.register()` and enabled with `alt.themes.enable()`. Each theme returns a config dict with global styling properties (font, axis, legend, view, etc.). The system parallels matplotlib's `plt.style.use` and Plotly's templates: define once, enable globally, keep charts consistent. Built-in themes include `"default"`, `"dark"`, `"fivethirtyeight"`, and others; custom themes are easy to add for organizational brand consistency.

Scoring Rubric

Score Level Meaning
18–20 Mastery You understand the grammar of graphics and can build compositional Altair charts confidently.
14–17 Proficient You know the main APIs; review selections and composition operators.
10–13 Developing You grasp the declarative approach; re-read Sections 22.7-22.10 and work all Part B exercises.
< 10 Review Re-read the full chapter and complete all Part A and Part B exercises. Part V is now complete, and Part VI (geospatial, networks, time series, etc.) begins next.

After this quiz, continue to Chapter 23 (Geospatial Visualization), which begins Part VI and introduces maps, choropleths, and geographic libraries.