Every exercise in 60_charts_and_sharing.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.
Exercise 7.1. Comparison: weighted mean yield by province
The task
Why it matters: "which province yields most?" is a comparison, and the survey weights decide the answer: Nueva Ecija's plots stand for more households than any other province's. A plain mean of the plots in the sample answers a different question.
Input → output: a table of plots (columns province, yield_t_ha, weight, among others) → a bar chart with one bar per province, whose length is the province's weighted mean yield, on a value axis that starts at zero and whose title contains the unit t/ha.
The weighted mean is the sum of yield × weight over the sum of weights (module 4 (30_summarize_and_combine.py), section 3). For two plots in one province:
| yield_t_ha | weight | yield × weight |
|---|---|---|
| 6.0 | 300 | 1,800 |
| 2.0 | 100 | 200 |
| sum | 400 | 2,000 |
Weighted mean: 2,000 / 400 = 5.0 t/ha, where the plain mean says 4.0.
Tools: table.assign(...) and groupby("province")[[...]].sum() for the two sums; then any library. In matplotlib: fig, ax = plt.subplots(), ax.barh(names, values), ax.set_xlabel("Weighted mean yield (t/ha)"), and return ax. In seaborn, sns.barplot(table, x=..., y=..., weights="weight", errorbar=None, ax=ax) computes the weighted means itself. In Altair, chart a table of the means with alt.X("...:Q", title="...").
If the check is red: common causes
| what you see | the usual cause | look at |
|---|---|---|
'value axis names the unit': False |
The value axis has no title, or seaborn's default one (the column name, yield_t_ha). |
ax.set_xlabel("Weighted mean yield (t/ha)") (or set_ylabel for upright bars); in Altair alt.X("...:Q", title="...") |
'value axis starts at zero': False |
The axis was moved off zero. | remove set_xlim / set_ylim, or a domain in alt.Scale |
| The bars are close but not right | A plain mean: .mean(), or seaborn without weights="weight", or Altair's mean(yield_t_ha). |
the sum of yield × weight over the sum of weights |
Extra rows with 'mark': 'line' |
seaborn's error bars, drawn by default. | errorbar=None |
| TypeError: chart_data reads ... not a builtins.NoneType | The function draws but returns nothing. | end with return ax (or return chart in Altair) |
| Bars from another chart appear too | seaborn drew on the last chart still open. | start with fig, ax = plt.subplots() and pass ax=ax |
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
A comparison is a bar per group, measured from zero. The bar's length is the province's weighted mean, so first compute it in pandas (the sum of yield times weight over the sum of weights), then draw it and say the unit.
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 yield_bars(table: pd.DataFrame): """A bar chart of each province's weighted mean yield, from zero, with the unit on the value axis.""" # Exercise 7.1: the task and examples are in the text above this cell. weighted = table.assign(weighted_yield=table["yield_t_ha"] * table["weight"]).groupby("province")[["weighted_yield", "weight"]].sum() means = (weighted["weighted_yield"] / weighted["weight"]).sort_values() fig, ax = plt.subplots(figsize=(7, 3.2)) ax.barh(means.index, means.to_numpy(), color="#4c78a8") ax.set_xlabel("Weighted mean yield (t/ha)") ax.set_title("Weighted mean yield by province", loc="left") return axWhy each part is there
assign(weighted_yield=table["yield_t_ha"] * table["weight"])puts each plot's yield times its weight in a column, so onegroupby("province")[["weighted_yield", "weight"]].sum()gives both sums per province.- Dividing the two sums gives the weighted mean. A plain
.mean()would count every plot in the sample once, whatever it stands for; on the check's small table it gives 11.25 for Bukidnon instead of 6.875. sort_values()orders the bars by value, so the reader sees the ranking at once;ax.barhlays the bars sideways, so long province names stay readable.- matplotlib starts a bar chart's axis at zero by itself; nothing here moves it.
set_xlabel("Weighted mean yield (t/ha)")names the measure and its unit, which the check reads. The function returnsax, the chart, so the check (and the Try it cell) can read and show it.
In seaborn the whole function is sns.barplot(table, x="yield_t_ha", y="province", weights="weight", errorbar=None, ax=ax) and a label; errorbar=None matters, since seaborn's default error bars are not the survey's margins of error.
Exercise 7.2. Trend: farmgate prices by month, with the gap shown
The task
Why it matters: a price chart is read for its rises and falls. A line drawn straight across September 2023 invents a month of prices; a reader planning a sale from it trusts a number that was never collected.
Input → output: the prices table (month as periods, province, commodity, price) and a province's name → a line chart with one line per commodity, months on the x axis as dates, every month from the first to the last, and September 2023 a gap in every line. The value axis's title contains the unit PHP/kg.
| month | Palay | Tomato |
|---|---|---|
| 2023-08 | 21.4 | 30.2 |
| 2023-09 | NaN | NaN |
| 2023-10 | 22.0 | 35.9 |
Tools: filter the province; pivot(index="month", columns="commodity", values="price") (module 4 (30_summarize_and_combine.py), section 4) gives one column per commodity; reindex(pd.period_range(first, last, freq="M")) brings back the missing month as NaN; .to_timestamp() turns the months into dates. In matplotlib, one ax.plot(dates, column, label=commodity) per column, ax.set_ylabel(...), ax.legend(), and return ax. In Altair, melt the wide table back to long and use color="commodity:N": a missing price breaks an Altair line too. seaborn drops missing values before it draws, which bridges the gap; section 2 shows the way around it.
If the check is red: common causes
| what you see | the usual cause | look at |
|---|---|---|
'lines drawn across a gap': 4 (or 8, 12) |
The missing month has no row, so each line joins August to October. | reindex(pd.period_range(...)) before drawing |
| TypeError: float() argument must be a string or a real number, not 'Period' | matplotlib was given periods. | wide.index.to_timestamp() |
'value axis names the unit': False |
No y-axis title, or one without the unit. | ax.set_ylabel("Farmgate price (PHP/kg)") |
Every row's series is None |
The lines have no names, so the check can't tell them apart. | label=commodity in each ax.plot; in Altair color="commodity:N" |
The x values look like '2023-07' |
The months were drawn as text, so they aren't spaced as time. | dates, from .to_timestamp() |
| KeyError: 'Palay' or an empty chart | The province's name is misspelt, or the filter kept no rows. | prices["province"].unique() |
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
A line joins each point to the next, so a month without a row is silently bridged. Give every month a row, with a missing price where there was none, and the line breaks there by itself.
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 price_lines(prices: pd.DataFrame, province: str): """Each commodity's farmgate price by month in one province, one line each, the missing month a gap.""" # Exercise 7.2: the task and examples are in the text above this cell. one = prices[prices["province"] == province] wide = one.pivot(index="month", columns="commodity", values="price") wide = wide.reindex(pd.period_range(wide.index.min(), wide.index.max(), freq="M")) fig, ax = plt.subplots(figsize=(9, 3.4)) for commodity in wide.columns: ax.plot(wide.index.to_timestamp(), wide[commodity], label=commodity) ax.set_ylabel("Farmgate price (PHP/kg)") ax.set_title(f"Farmgate prices in {province}, by month", loc="left") ax.legend(title="Commodity", frameon=False, loc="upper left", bbox_to_anchor=(1, 1)) # beside the chart, off the lines fig.tight_layout() return axWhy each part is there
pivot(index="month", columns="commodity", values="price")makes one row per month and one column per commodity: each column becomes one line.reindex(pd.period_range(wide.index.min(), wide.index.max(), freq="M"))lists every month from the first to the last; September 2023, which had no rows, comes back withNaNin every column. Without it, the check counts a line drawn across the gap for each commodity.wide.index.to_timestamp()gives each month as the date of its first day, which matplotlib places on a time axis; it cannot place a period.ax.plotstops a line at aNaNand starts again after it, so the gap shows.label=commoditynames each line for the legend (and for the check, which reads which line is which).set_ylabel("Farmgate price (PHP/kg)")gives the unit. The axis need not start at zero: a line is read by its shape.
Exercise 7.3. One summary for every library: weighted means by group
The task
Why it matters: compute the statistic once, in pandas, where you can check it, and any library can draw it. It also stops a library's own default (a plain mean) from deciding your numbers.
Input → output: a table of plots and a list of column names to group by → a tidy table: the group columns, then yield_t_ha (the group's weighted mean yield), one row per group, with a fresh index (0, 1, 2, …).
weighted_means(rice, ["province", "irrigated"]):
| province | irrigated | yield_t_ha | |
|---|---|---|---|
| 0 | Bukidnon | No | 3.4686 |
| 1 | Bukidnon | Yes | 4.8730 |
| … | … | … | … |
Tools: as in Exercise 7.1, with groupby(by) for any list of columns; .rename("yield_t_ha") names the result, and .reset_index() turns the groups back into columns.
If the check is red: common causes
| what you see | the usual cause | look at |
|---|---|---|
| The values are close but not right | A plain mean, groupby(by)["yield_t_ha"].mean(). |
the sum of yield × weight over the sum of weights |
| The groups are the index, not columns | .reset_index() is missing. |
.reset_index() |
The last column has another name (0, weighted_yield) |
The result was not named before reset_index. |
.rename("yield_t_ha") |
| KeyError: 'province' (or another column) | by was used as one name, or a column is misspelt. |
groupby(by), with by the list given |
| TypeError ... unhashable type: 'list' | The list was put in another list: groupby([by]). |
groupby(by) |
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
The same weighted mean as Exercise 7.1, for any grouping, returned as a tidy table: one row per bar a chart will draw, so any library draws it as it is.
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 weighted_means(table: pd.DataFrame, by: list[str]) -> pd.DataFrame: """The weighted mean yield of each group of plots: the group columns, then yield_t_ha.""" # Exercise 7.3: the task and examples are in the text above this cell. weighted = table.assign(weighted_yield=table["yield_t_ha"] * table["weight"]) sums = weighted.groupby(by)[["weighted_yield", "weight"]].sum() return (sums["weighted_yield"] / sums["weight"]).rename("yield_t_ha").reset_index()Why each part is there
assign(weighted_yield=...)andgroupby(by)[["weighted_yield", "weight"]].sum()give both sums for every group;byis a list, so one column or several work alike.- Dividing the sums gives a Series whose index is the groups.
.rename("yield_t_ha")names it, so.reset_index()makes a table with the group columns first andyield_t_halast, with a fresh index. - A plain mean differs on every group with unequal weights: Isabela's two rain-fed plots in the check's small table give 3.75 weighted, (3.0 × 100 + 4.0 × 300) / 400, and 3.5 plain.
Exercise 7.4. Irrigated or not: grouped bars from the summary
The task
Why it matters: the summary table is the hand-off point between computing and drawing. Draw it in the library that fits you; the check reads the bars, so any of the three passes.
Input → output: a summary table like Exercise 7.3's (province, irrigated, yield_t_ha) → a bar chart with a pair of bars per province, one per value of irrigated (the check needs to tell the two apart: a legend in seaborn, label= in matplotlib, color= in Altair), a value axis that starts at zero and whose title contains t/ha. A province with only one kind of plot gets one bar: no bar of zero for the missing kind.
Tools: any of the three charts above. In seaborn, sns.barplot(summary, x="yield_t_ha", y="province", hue="irrigated", ax=ax): with one row per bar there is nothing to average, so errorbar=None adds nothing but keeps it clear.
If the check is red: common causes
| what you see | the usual cause | look at |
|---|---|---|
Every row's series is None |
The two kinds of bar are not named: no hue, no label=, or seaborn's legend removed (legend=False). |
hue="irrigated" (seaborn), label= (matplotlib), color="irrigated:N" (Altair) |
A bar with 'y': 0 for Bukidnon |
The missing combination was filled with 0 (fillna(0) after a pivot). |
leave it missing: no bar |
'value axis names the unit': False |
The title is the column name, yield_t_ha. |
ax.set_xlabel(...) or alt.X(..., title=...) |
| The bars of a province are stacked into one | Altair stacks bars that share a place. | yOffset="irrigated:N" (or xOffset for upright bars) |
| KeyError: 'weight' | The function computes means again; the summary has them already. | draw yield_t_ha as it is |
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
The summary already holds one row per bar, so the chart only places them: province on one axis, the weighted mean as the length, and irrigation as the color that tells the pair apart.
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 irrigation_bars(summary: pd.DataFrame): """Each province's weighted mean yield, irrigated and not, as a pair of bars from zero.""" # Exercise 7.4: the task and examples are in the text above this cell. fig, ax = plt.subplots(figsize=(7, 4)) sns.barplot(summary, x="yield_t_ha", y="province", hue="irrigated", errorbar=None, ax=ax) ax.set_xlabel("Weighted mean yield (t/ha)") ax.set_ylabel("") ax.legend(title="Irrigated", frameon=False) return axWhy each part is there
sns.barplot(summary, x="yield_t_ha", y="province", hue="irrigated", ...)draws a bar per row;huesplits each province into a pair and adds the legend that names each color, which the check reads to tell the bars apart.- With one row per bar, seaborn has nothing to average, so the bar is the summary's own value.
errorbar=Nonesays so: there is no interval to draw. - A province with only rain-fed plots has no row for "Yes", and no bar is drawn: a bar of zero would claim a yield of zero.
- seaborn titles the value axis with the column's name;
set_xlabel(...)replaces it with the measure and its unit. The axis starts at zero by itself.
Exercise 7.5. The app's filter
The task
Why it matters: every view the app shows comes from this one function, so it must be right for every choice a user can make, including the odd ones: every province, or no crop at all.
Input → output: the plots table, a province's name (or ALL_PROVINCES, the text "All provinces") and a list of crops → the rows of that province (of every province, for ALL_PROVINCES) whose crop is in the list, in their original order. An empty list of crops gives no rows.
| province | crops | rows kept |
|---|---|---|
"Iloilo" |
["Rice"] |
Iloilo's rice plots |
"Iloilo" |
["Rice", "Corn"] |
Iloilo's rice and corn plots |
ALL_PROVINCES |
["Corn"] |
every corn plot |
"Iloilo" |
[] |
none (the table's columns, no rows) |
Tools: table["crop"].isin(crops) is True for each row whose crop is in the list; combine conditions with &, each in brackets (module 3 (20_dataframes.py), section 3); an if adds the province's condition only when a province is chosen.
If the check is red: common causes
| what you see | the usual cause | look at |
|---|---|---|
| ValueError: ('Lengths must match to compare', ...) | table["crop"] == crops compares a column with a list. |
table["crop"].isin(crops) |
| ValueError: The truth value of a Series is ambiguous | and or or between two conditions. |
&, with each condition in brackets |
| "all provinces, corn" gives no rows | The province's condition is applied even for ALL_PROVINCES. |
if province != ALL_PROVINCES: |
| "no crop chosen" gives every row | An empty list was treated as "no filter". | let isin([]) do its work: no rows |
| Rows of other provinces appear | The province's condition is built but not used. | rows = rows & (...) |
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
Build the condition in steps: the crop condition always applies, and the province's only when one is chosen. Then keep the rows where the condition holds.
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 filter_plots(table: pd.DataFrame, province: str, crops: list[str]) -> pd.DataFrame: """The plots of one province (or of every province, for ALL_PROVINCES) whose crop is in the list.""" # Exercise 7.5: the task and examples are in the text above this cell. rows = table["crop"].isin(crops) if province != ALL_PROVINCES: rows = rows & (table["province"] == province) return table[rows]Why each part is there
table["crop"].isin(crops)compares each row's crop with the whole list.==would compare the column with the list item by item and fail (Lengths must match); with an empty list,isinisFalseeverywhere, so no rows are kept, as asked.if province != ALL_PROVINCES:adds the province's condition only for a real province; for "All provinces" no row has that name, so filtering on it would keep nothing.rows & (table["province"] == province)keeps rows that meet both. The brackets matter, since&binds tighter than==; Python'sandfails on columns (The truth value of a Series is ambiguous).table[rows]keeps the rows in their order. The check compares rows and columns, not the index, so resetting it or not are both right.
Exercise 7.6. The app's chart
The task
Why it matters: the app calls this function with whatever rows the controls leave: one province or all, one crop or four. A function from table to chart draws every one of them the same honest way.
Input → output: a table of plots (any selection of them) → a bar chart with one bar per crop in the table, whose length is the crop's weighted mean yield, on a value axis that starts at zero, titled with t/ha.
Tools: the weighted mean by crop (your weighted_means(table, ["crop"]) from Exercise 7.3, or the two sums again), then any library. The app is a screen, so this is a natural place for Altair: alt.Chart(summary).mark_bar().encode(x=alt.X("yield_t_ha:Q", title="..."), y=alt.Y("crop:N", title=None, sort="-x"), tooltip=[...]), and return the chart.
If the check is red: common causes
| what you see | the usual cause | look at |
|---|---|---|
| The bars are close but not right | A plain mean (Altair's mean(yield_t_ha) is one). |
compute the weighted mean in pandas, then chart it |
| ... field "crop" not found or an empty chart | The crops are the summary's index, not a column. | as_index=False, or .reset_index() |
'value axis names the unit': False |
Altair titles the axis with the column name. | alt.X("yield_t_ha:Q", title="Weighted mean yield (t/ha)") |
'value axis starts at zero': False |
A domain that starts above zero, or a log scale. |
leave the scale as it is |
| TypeError: chart_data reads ... not a builtins.NoneType | The chart is built but not returned. | return alt.Chart(...)... |
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
The same comparison as Exercise 7.1, by crop, drawn for whatever rows the app passes in. In Altair the chart is a description of the summary table, and the tooltip gives the exact value on screen.
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 yield_chart(table: pd.DataFrame): """The app's chart: each crop's weighted mean yield in the rows given, as bars from zero.""" # Exercise 7.6: the task and examples are in the text above this cell. sums = table.assign(weighted_yield=table["yield_t_ha"] * table["weight"]).groupby("crop", as_index=False)[["weighted_yield", "weight"]].sum() 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("crop:N", title=None, sort="-x"), tooltip=["crop", alt.Tooltip("yield_t_ha:Q", format=".2f", title="t/ha")], ).properties(width=420, height=alt.Step(30))Why each part is there
- The two sums by crop, divided, give each crop's weighted mean;
as_index=Falsekeepscropas a column, which Altair needs (it reads columns, not the index). alt.X("yield_t_ha:Q", title="Weighted mean yield (t/ha)")makes the yield the bars' length, on a quantitative axis that starts at zero by default, titled with the unit the check reads.alt.Y("crop:N", title=None, sort="-x")puts one bar per crop, longest first; the crop names say what the axis is, so it needs no title.- The tooltip shows the crop and its yield to two decimals when the pointer rests on a bar: on a screen, the exact number without cluttering the chart.