Skip to content
Learn Python with BNHR

Charts and sharing

On this page
  1. Overview
  2. 7.1 · Comparison: weighted mean yield by province
  3. 7.2 · Trend: farmgate prices by month, with the gap shown
  4. 7.3 · One summary for every library: weighted means by group
  5. 7.4 · Irrigated or not: grouped bars from the summary
  6. 7.5 · The app's filter
  7. 7.6 · The app's chart

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.

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.

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.

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.

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.

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.

Something new

Identify results
LayerExplore a module
ModuleCheck: Round 2 and the contract
AboutModule 3 of Python for Data Management: write the round's rules once, check every round against them, and clean what can be cleaned as code. Its sections, its notebooks to read on the web, and the companion's solutions and explanations.
CoursePython for Data Management, free