Every exercise in 83_stretch_charts.py, with the solution and why it works. Each answer stays closed until you swear you tried it first. Opening one changes nothing in your notebook; your check still has to go green from your own code. Read the whole notebook, with its lessons.
Stretch S3.1. Small multiples by province
The task
Seven provinces' tomato prices in one chart are seven tangled lines. Small multiples give each province a small chart of its own, side by side, so the eye compares shapes. They compare honestly only if every panel shares one y scale: with a scale of its own, a province whose price barely moved fills its panel as fully as one whose price doubled.
Write province_panels(prices, commodity): one panel per province, each with the commodity's farmgate price by month as a line, every panel on the same y scale, and September 2023 a gap in every line. Return the whole figure:
- matplotlib:
fig, axes = plt.subplots(2, 4, sharey=True), one province perax(titled with its name), an unused panel removed (ax.remove()), andreturn fig; - seaborn:
sns.relplot(..., col="province", col_wrap=4, kind="line")returns a grid whose panels share their scale; titles likeprovince = Iloiloare fine. It drops missing values, so draw each unbroken stretch as its own line (units=, withestimator=None), as module 7 section 2 shows; - Altair:
.encode(..., facet=alt.Facet("province:N", columns=4)); a missing price breaks the line.
The check reads each panel's points, counts lines drawn across a missing month, and asks whether the panels share one y scale.
If the check is red: common causes
| what you see | the usual cause | look at |
|---|---|---|
'one y scale': False |
Each panel scaled itself. | sharey=True; in Altair, no resolve_scale(y="independent") |
'lines drawn across a gap': 7 |
September 2023 has no row; or seaborn dropped its missing price. | reindex(pd.period_range(...)); in seaborn, units= and estimator=None |
| TypeError: chart_data reads a figure with one chart | An ax or the array of axes was returned. |
return fig (or the seaborn grid, or the Altair chart) |
Panels named '' |
The panels have no titles. | ax.set_title(province) |
Still stuck after ten minutes? Ask your table, then raise your hand.
The solution, and why it works
Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.
The idea
Give every month a row (the missing one with no price), then draw one province per panel. Sharing the y axis makes the panels comparable: a line's height means the same price in every panel.
The function, solved
Copy it with the button on the code's frame, and paste it over all the code in the exercise's cell, so the indentation stays right.
def province_panels(prices: pd.DataFrame, commodity: str): """A small chart per province of one commodity's price by month, on one shared y scale, the missing month a gap.""" one = prices[prices["commodity"] == commodity] wide = one.pivot(index="month", columns="province", values="price") wide = wide.reindex(pd.period_range(wide.index.min(), wide.index.max(), freq="M")) columns = 4 rows = -(-len(wide.columns) // columns) # rounded up: enough rows for every province fig, axes = plt.subplots(rows, columns, figsize=(12, 2.6 * rows), sharex=True, sharey=True, squeeze=False) for ax, province in zip(axes.flat, wide.columns): ax.plot(wide.index.to_timestamp(), wide[province], color="#4c78a8") ax.set_title(province, loc="left", fontsize=10) ax.xaxis.set_major_locator(mdates.YearLocator()) # a tick each January, labeled with its year ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y")) for ax in axes.flat[len(wide.columns):]: ax.remove() fig.supylabel(f"{commodity}, farmgate price (PHP/kg)", fontsize=10) fig.tight_layout() return figWhy each part is there
pivot(index="month", columns="province", values="price")andreindex(pd.period_range(...))give one column per province and one row per month, withNaNfor September 2023, so every line breaks there.plt.subplots(rows, 4, sharey=True, sharex=True, squeeze=False)makes the grid;sharey=Trueis the honest-chart rule: one scale for all.squeeze=Falsekeepsaxesa grid even when it has one row, soaxes.flatalways works.- Each panel's title is its province's name, which is how a reader (and the check) knows which is which.
- Panels left over in the grid are removed, so they don't show as empty frames;
fig.supylabeltitles the shared y axis once, with the unit. return fig: the figure holds every panel. Oneaxwould be one province.
Stretch S3.2. Honest error bars, or none
The task
An error bar says "the true value is probably in here". seaborn's default bars answer that for a simple random sample of plots; this survey sampled households, with weights. So either draw intervals computed for the weighted mean, or draw none. Here is an approximate 95% interval for a weighted mean $\bar y$, from yields $y$ and weights $w$:
$$\bar y = \frac{\sum w y}{\sum w} \qquad se = \frac{\sqrt{\sum w^2 (y - \bar y)^2}}{\sum w} \qquad \text{interval: } \bar y \pm 1.96 , se$$
It treats each plot as drawn on its own. The survey drew households, and a household's plots are alike, so the survey's true interval is somewhat wider: say "approximate" on the chart.
Write interval_bars(table): for a table of plots, a bar per province (its weighted mean yield) with an error bar from the interval's low end to its high end, a value axis from zero titled with t/ha.
- matplotlib:
ax.barh(...), thenax.errorbar(means, provinces, xerr=[means - low, high - means], fmt="none")(xerrtakes the distances below and above); - seaborn:
sns.barplot(summary, ..., errorbar=None, ax=ax), then the sameax.errorbar: seaborn's ownerrorbar=cannot draw intervals you computed; - Altair: a layer of
mark_bar()andmark_errorbar()withx="low:Q", x2="high:Q".
The check reads the bars, each error bar's two ends (to 3 decimals), and the value axis.
If the check is red: common causes
| what you see | the usual cause | look at |
|---|---|---|
| The error bars are close but not right | seaborn's own error bars (a bootstrap), or a plain standard error. | compute se from the formula, with the weights |
| The error bars are twice too long, or end at the wrong place | xerr given the ends, not the distances to them. |
xerr=[mean - low, high - mean] |
'error bars': [] |
The error bars are drawn another way (ax.hlines, ax.plot). |
ax.errorbar, or Altair's mark_errorbar / mark_rule with x2 |
'value axis names the unit': False |
No axis title, or one without t/ha. | ax.set_xlabel(...) |
Still stuck after ten minutes? Ask your table, then raise your hand.
The solution, and why it works
Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.
The idea
Compute the weighted mean and its standard error per province in pandas, then draw the bars and, on them, error bars from the interval's two ends. The interval is for the survey's weighted mean, not a library's default for a plain one.
The function, solved
Copy it with the button on the code's frame, and paste it over all the code in the exercise's cell, so the indentation stays right.
def interval_bars(table: pd.DataFrame): """Each province's weighted mean yield as a bar, with an approximate 95% interval as its error bar.""" rows = [] for province, plots in table.groupby("province"): w, y = plots["weight"], plots["yield_t_ha"] mean = (w * y).sum() / w.sum() se = np.sqrt((w**2 * (y - mean) ** 2).sum()) / w.sum() rows.append({"province": province, "yield_t_ha": mean, "low": mean - 1.96 * se, "high": mean + 1.96 * se}) summary = pd.DataFrame(rows).sort_values("yield_t_ha") fig, ax = plt.subplots(figsize=(7, 3.4)) ax.barh(summary["province"], summary["yield_t_ha"], color="#9ecae9") ax.errorbar(summary["yield_t_ha"], summary["province"], xerr=[summary["yield_t_ha"] - summary["low"], summary["high"] - summary["yield_t_ha"]], fmt="none", color="black", capsize=3) ax.set_xlabel("Weighted mean yield (t/ha), with an approximate 95% interval") return axWhy each part is there
- For each province,
(w * y).sum() / w.sum()is the weighted mean, andnp.sqrt((w**2 * (y - mean)**2).sum()) / w.sum()its approximate standard error: plots with large weights move the mean most, so they count most in its uncertainty. mean ± 1.96 * segives the two ends of an approximate 95% interval. On the check's small table, Iloilo's mean is 3.667 and its interval runs from 2.852 to 4.482.ax.errorbar(..., xerr=[mean - low, high - mean], fmt="none")draws a line fromlowtohighon each bar:xerrtakes the distance on each side, andfmt="none"draws no marker or line between the bars.- The axis title says what the bars and the error bars are, with the unit, and that the interval is approximate.
Stretch S3.3. Seasons: price by month of the year
The task
Four years of tomato prices in one long line show a trend; laid over each other, one line per year with January to December along the x axis, they show the season: when in the year the price climbs and falls, and whether every year does it.
Write seasonal_lines(prices, commodity): the commodity's price, averaged over the provinces for each month; then one line per year (named by the year, as text: "2022"), x the month of the year as a number (1 to 12), every month from the first to the last a point, and September 2023 a gap in 2023's line. The value axis's title contains PHP/kg.
Tools: groupby("month")["price"].mean() and the reindex from module 7; on a period index, .year and .month give each month's year and month number. ax.set_xticks(range(1, 13), calendar.month_abbr[1:]) labels the months by name (the check reads the numbers, not the labels).
If the check is red: common causes
| what you see | the usual cause | look at |
|---|---|---|
'lines drawn across a gap': 1 |
September 2023 has no row, so 2023's line joins August to October. | reindex before turning the months around |
Every row's series is None |
The years' lines have no names. | label=str(year); in seaborn hue="year"; in Altair color="year:N" |
Many more lines, or x values like '2023-07-01' |
The months were not turned into month numbers. | .month on the period index |
| Prices off by a little | The provinces were not averaged, or one province was picked. | groupby("month")["price"].mean() |
Still stuck after ten minutes? Ask your table, then raise your hand.
The solution, and why it works
Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.
The idea
Average the provinces into one price per month, give every month a row, and turn the months around: the month of the year along the x axis, and a line for each year.
The function, solved
Copy it with the button on the code's frame, and paste it over all the code in the exercise's cell, so the indentation stays right.
def seasonal_lines(prices: pd.DataFrame, commodity: str): """One commodity's mean price by month of the year, one line per year, the missing month a gap.""" monthly = prices[prices["commodity"] == commodity].groupby("month")["price"].mean() monthly = monthly.reindex(pd.period_range(monthly.index.min(), monthly.index.max(), freq="M")) table = pd.DataFrame({"year": monthly.index.year, "month": monthly.index.month, "price": monthly.to_numpy()}) wide = table.pivot(index="month", columns="year", values="price") fig, ax = plt.subplots(figsize=(8, 3.6)) for year in wide.columns: ax.plot(wide.index, wide[year], label=str(year), marker="o", markersize=3) ax.set_xticks(range(1, 13), calendar.month_abbr[1:]) ax.set_ylabel(f"{commodity}, mean farmgate price (PHP/kg)") ax.legend(title="Year", frameon=False, loc="upper left", bbox_to_anchor=(1, 1)) fig.tight_layout() return axWhy each part is there
groupby("month")["price"].mean()gives one price per month (the provinces' mean);reindex(pd.period_range(...))adds September 2023 back asNaN.- A period index knows each month's
.yearand.month, so a table of year, month number and price takes one line;pivot(index="month", columns="year")makes a column per year. - One
ax.plotper year,label=str(year): each line is a year, named so the legend (and the check) can tell them apart. 2023'sNaNin September breaks its line. - Month names on the ticks read better than numbers; the axis still runs 1 to 12, evenly spaced.
Stretch S3.4. A trend line: fertilizer and yield
The task
Do rice plots that get more fertilizer per hectare yield more? A scatter shows every plot as a point; a trend line summarizes how yield changes, on average, as fertilizer grows. Fit it, don't draw it by eye: the least-squares line is the straight line that keeps the points' squared vertical distances to it as small as possible, and np.polyfit(x, y, 1) gives its slope and intercept.
Write fertilizer_trend(table): for a table of plots, keep those with fertilizer recorded; x is fertilizer per hectare (fertilizer_kg / area_ha), y the yield (t/ha). Draw every plot as a point, and the least-squares line as a line from the smallest x to the largest.
- matplotlib:
ax.scatter(x, y), thenax.plot([x_min, x_max], [...])from the slope and intercept; - seaborn:
sns.regplot(data, x=..., y=..., ax=ax)fits and draws it in one call (its shaded band is not checked); - Altair: Altair's own
transform_regressiondraws in your browser, where the check cannot read it, so fit withnp.polyfitand layer the points with a line drawn from a two-row table.
The check reads the points, and the line's slope and intercept. Then read your chart: what does the line say?
If the check is red: common causes
| what you see | the usual cause | look at |
|---|---|---|
LinAlgError or nan from np.polyfit |
Plots without fertilizer recorded are still in. | table[table["fertilizer_kg"].notna()] |
| The slope is far off | x is total fertilizer, not per hectare. | fertilizer_kg / area_ha |
'trend line (slope, intercept)': None |
No line is drawn, or Altair's transform_regression (the check can't run it). |
a line from np.polyfit's slope and intercept |
| More points than plots | The line was drawn with markers. | ax.plot(...) without marker=; points with ax.scatter |
Still stuck after ten minutes? Ask your table, then raise your hand.
The solution, and why it works
Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.
The idea
Points for the plots, and over them the straight line that fits them best, computed rather than guessed. Its slope is the summary: how much yield changes, on average, for each extra kilogram per hectare.
The function, solved
Copy it with the button on the code's frame, and paste it over all the code in the exercise's cell, so the indentation stays right.
def fertilizer_trend(table: pd.DataFrame): """Each plot's yield against its fertilizer per hectare, with the least-squares line.""" plots = table[table["fertilizer_kg"].notna()] x = plots["fertilizer_kg"] / plots["area_ha"] y = plots["yield_t_ha"] slope, intercept = np.polyfit(x, y, 1) ends = np.array([x.min(), x.max()]) fig, ax = plt.subplots(figsize=(6.5, 4)) ax.scatter(x, y, s=10, alpha=0.4, color="#4c78a8") ax.plot(ends, slope * ends + intercept, color="#e45756", linewidth=2) ax.set_xlabel("Fertilizer (kg/ha)") ax.set_ylabel("Yield (t/ha)") ax.set_title(f"Least-squares line: {slope:+.4f} t/ha per kg/ha", loc="left", fontsize=10) return axWhy each part is there
- Plots without fertilizer recorded are left out: a missing value has no place on the x axis, and
np.polyfitrefuses missing values. fertilizer_kg / area_hamakes plots of different sizes comparable; total fertilizer would mostly measure plot size.np.polyfit(x, y, 1)returns the slope and intercept of the least-squares line (degree 1: a straight line). Two points, at the smallest and largest x, are enough to draw it.- On the survey's rice plots the slope is about 0.0098 t/ha per kg/ha: 100 kg more per hectare goes with about 1 t/ha more. The points scatter widely around the line, so fertilizer is one part of the story. And a line shows a link, not a cause: farmers who expect a good harvest may also buy more fertilizer.
Stretch S3.5. A tooltip that answers the next question
The task
A reader looking at a bar of yields asks two things next: what exactly is it? and how many plots is it based on? On a screen, a tooltip answers both when the pointer rests on the bar, without crowding the chart. Only Altair (of the three) makes charts with tooltips: matplotlib and seaborn draw pictures.
Write tooltip_bars(table), in Altair: for a table of plots, one bar per province with its weighted mean yield, a value axis from zero titled with t/ha, and a tooltip that shows the province, the yield (yield_t_ha, formatted to two decimals) and the number of plots in the survey behind it (a column plots).
Tools: groupby("province").agg(...) with named aggregations for the sums and the count (module 4, section 1); tooltip=[alt.Tooltip("province:N", title="Province"), alt.Tooltip("yield_t_ha:Q", format=".2f", title="..."), ...]. The check reads the bars, the value axis, and which columns the tooltip shows.
If the check is red: common causes
| what you see | the usual cause | look at |
|---|---|---|
'tooltip shows': [] |
A matplotlib or seaborn chart (a picture: no tooltip), or no tooltip= in the encoding. |
alt.Chart(...).encode(..., tooltip=[...]) |
'plots' is missing from the tooltip |
The summary has no count of plots, or it is named otherwise. | plots=("yield_t_ha", "size") in agg |
| The bars are close but not right | Altair's mean(yield_t_ha), a plain mean. |
the weighted mean, computed in pandas |
'value axis names the unit': False |
The axis is titled with the column's name. | alt.X(..., title="Weighted mean yield (t/ha)") |
Still stuck after ten minutes? Ask your table, then raise your hand.
The solution, and why it works
Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.
The idea
Summarize first: one row per province with its weighted mean and its count of plots. Then the chart maps columns to channels, and the tooltip is one more channel, listing the columns to show.
The function, solved
Copy it with the button on the code's frame, and paste it over all the code in the exercise's cell, so the indentation stays right.
def tooltip_bars(table: pd.DataFrame): """Each province's weighted mean yield as an Altair bar, with a tooltip of the province, the yield and its plots.""" sums = table.assign(weighted_yield=table["yield_t_ha"] * table["weight"]).groupby("province", as_index=False).agg( weighted_yield=("weighted_yield", "sum"), weight=("weight", "sum"), plots=("yield_t_ha", "size")) summary = sums.assign(yield_t_ha=sums["weighted_yield"] / sums["weight"]) return alt.Chart(summary).mark_bar().encode( x=alt.X("yield_t_ha:Q", title="Weighted mean yield (t/ha)"), y=alt.Y("province:N", title=None, sort="-x"), tooltip=[alt.Tooltip("province:N", title="Province"), alt.Tooltip("yield_t_ha:Q", format=".2f", title="Yield (t/ha)"), alt.Tooltip("plots:Q", title="Plots in the survey")], ).properties(width=420)Why each part is there
- One
groupby("province").agg(...)gives the two sums for the weighted mean andplots=("yield_t_ha", "size"), the count of plots: a tooltip can only show what the chart's table holds. alt.X("yield_t_ha:Q", title=...)makes the yield the bars' length, from zero, titled with its unit;sort="-x"puts the longest bar first.- Each
alt.Tooltipnames a column, how to show it (format=".2f": two decimals) and its label. The count tells the reader how much to trust each bar: a mean of few plots moves a lot with one more.