import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Charts%20and%20sharing%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20from%20chart_checks%20import%20altair_layers%2C%20altair_title%2C%20bridged%2C%20column_of%2C%20mark_of%2C%20one_axes%2C%20value_axis%2C%20y_title%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20settings%20every%20other%20cell%20uses.%0A%20%20%20%20import%20altair%20as%20alt%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20seaborn%20as%20sns%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20the%20data%2C%20and%20the%20exercise%20checks%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20attempt%2C%20check_cases%2C%20waiting_for%0A%0A%20%20%20%20ALL_PROVINCES%20%3D%20%22All%20provinces%22%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Charts%20and%20sharing%0A%0A%20%20%20%20In%20module%201%20you%20drew%20one%20chart%20three%20ways.%20Since%20then%20every%20module%20has%20drawn%20charts%20in%20one%20library%20or%20another.%20This%20module%20is%20about%20**choosing**%3A%20which%20chart%20answers%20which%20question%2C%20how%20to%20label%20it%20so%20it%20cannot%20mislead%2C%20which%20of%20the%20three%20libraries%20fits%20the%20job%2C%20and%20how%20to%20turn%20a%20table%20and%20a%20chart%20into%20a%20small%20**app**%20that%20a%20colleague%20can%20use%20without%20reading%20your%20code.%0A%0A%20%20%20%20In%20this%20module%20you%20will%3A%0A%0A%20%20%20%201.%20match%20the%20chart%20to%20the%20question%3A%20a%20**comparison**%2C%20a%20**distribution**%20or%20a%20**trend**%2C%20each%20with%20the%20rule%20that%20keeps%20it%20honest%3B%0A%20%20%20%202.%20draw%20the%20same%20question%20in%20**matplotlib**%2C%20**seaborn**%20and%20**Altair**%2C%20and%20see%20where%20each%20fits%3B%0A%20%20%20%203.%20build%20a%20small%20**interactive%20app**%20from%20the%20farm%20survey%3A%20a%20dropdown%20and%20a%20list%20of%20crops%20that%20drive%20a%20table%20and%20a%20chart.%0A%0A%20%20%20%20Exercises%20work%20as%20in%20the%20earlier%20modules%3A%20write%20the%20function%2C%20delete%20the%20%60raise%60%20line%2C%20use%20the%20**Try%20it**%20cell%2C%20read%20the%20check.%20If%20something%20goes%20red%2C%20read%20the%20error's%20**last%20line**%20first%3A%20it%20names%20what%20went%20wrong.%0A%0A%20%20%20%20**How%20the%20checks%20read%20a%20chart.**%20A%20chart's%20check%20never%20looks%20at%20the%20picture.%20It%20reads%20the%20data%20behind%20it%3A%20each%20bar's%20category%20and%20length%2C%20each%20line's%20points%2C%20each%20point's%20place.%20So%20a%20chart%20drawn%20in%20matplotlib%2C%20in%20seaborn%20or%20in%20Altair%20passes%20the%20same%20check%2C%20and%20you%20choose%20the%20library.%20Where%20an%20exercise%20asks%20for%20an%20honest%20label%20or%20baseline%2C%20the%20check%20also%20reads%20that%3A%20the%20title%20of%20the%20value%20axis%2C%20and%20where%20the%20axis%20starts.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%227.0%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20data%20for%20this%20module%0A%0A%20%20%20%20The%20plots%20of%20the%20farm%20survey%2C%20each%20with%20its%20household's%20province%20and%20survey%20weight%2C%20and%20its%20**yield**%20in%20metric%20tons%20per%20hectare%20(t%2Fha)%3A%20production%20in%20kilograms%2C%20divided%20by%20the%20area%20in%20hectares%2C%20divided%20by%201%2C000.%20Plots%20without%20a%20crop%20recorded%20(14%20of%20them)%20are%20left%20out.%20You%20built%20tables%20like%20this%20in%20modules%204%20and%205%3B%20%60survey_plots()%60%20below%20makes%20it%20in%20one%20step%2C%20so%20this%20module%20starts%20here.%20The%20farmgate%20prices%20come%20from%20%60course.prices()%60%3A%20one%20row%20per%20month%2C%20province%20and%20commodity%2C%20with%20**no%20rows%20for%20September%202023**%2C%20the%20month%20whose%20file%20never%20came.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20survey_plots()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Every%20plot%20with%20a%20crop%20recorded%2C%20with%20its%20household's%20province%20and%20weight%2C%20and%20its%20yield%20in%20t%2Fha.%22%22%22%0A%20%20%20%20plots%20%3D%20course.plots()%20%20%23%20clean%3A%20duplicates%20dropped%2C%20every%20area%20in%20hectares%20as%20area_ha%0A%20%20%20%20households%20%3D%20course.households()%5B%5B%22hh_id%22%2C%20%22province%22%2C%20%22weight%22%5D%5D%0A%20%20%20%20table%20%3D%20plots.merge(households%2C%20on%3D%22hh_id%22%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20table%20%3D%20table%5Btable%5B%22crop%22%5D.notna()%5D.reset_index(drop%3DTrue)%0A%20%20%20%20return%20table.assign(yield_t_ha%3Dtable%5B%22production_kg%22%5D%20%2F%20table%5B%22area_ha%22%5D%20%2F%201000)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20plots%20%3D%20survey_plots()%0A%20%20%20%20rice%20%3D%20plots%5Bplots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D%0A%20%20%20%20prices%20%3D%20course.prices()%0A%20%20%20%20households%20%3D%20course.households()%0A%20%20%20%20mo.md(f%22**%7Blen(plots)%3A%2C%7D%20plots**%20(%7Blen(rice)%3A%2C%7D%20of%20them%20rice)%20from%20**%7Bplots%5B'hh_id'%5D.nunique()%3A%2C%7D%20households**%2C%20and%20**%7Blen(prices)%3A%2C%7D%20prices**%20from%20%7Bprices%5B'month'%5D.min()%7D%20to%20%7Bprices%5B'month'%5D.max()%7D.%22)%0A%20%20%20%20return%20households%2C%20plots%2C%20prices%2C%20rice%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20The%20chart%20for%20the%20question%2C%20honestly%20labeled%3A%20comparison%2C%20distribution%2C%20trend%0A%0A%20%20%20%20Three%20questions%20an%20extension%20office%20asks%20about%20the%20survey%3A%0A%0A%20%20%20%20-%20*Which%20province%20has%20the%20most%20farm%20households%3F*%0A%20%20%20%20-%20*Do%20most%20rice%20plots%20yield%20about%20the%20same%2C%20or%20do%20a%20few%20pull%20the%20average%20up%3F*%0A%20%20%20%20-%20*Has%20the%20price%20of%20palay%20gone%20up%3F*%0A%0A%20%20%20%20Each%20needs%20a%20different%20chart.%20Start%20from%20the%20question%2C%20not%20from%20the%20chart%20type%20a%20library%20makes%20easiest%3A%20the%20question%20says%20what%20the%20reader%20must%20see%2C%20and%20the%20chart%20is%20the%20one%20that%20shows%20it%20with%20the%20least%20effort%20and%20the%20least%20chance%20of%20a%20wrong%20reading.%0A%0A%20%20%20%20%7C%20the%20question%20asks%20%7C%20it%20is%20a%20%7C%20draw%20%7C%20the%20rule%20that%20keeps%20it%20honest%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20which%20group%20is%20larger%2C%20and%20by%20how%20much%20%7C%20**comparison**%20%7C%20bars%2C%20one%20per%20group%20%7C%20bars%20start%20at%20**zero**%3A%20a%20bar's%20length%20is%20its%20value%2C%20so%20a%20bar%20cut%20short%20lies%20about%20the%20ratio%20%7C%0A%20%20%20%20%7C%20how%20values%20spread%3A%20typical%2C%20unusual%2C%20lopsided%20%7C%20**distribution**%20%7C%20a%20histogram%3B%20a%20box%20plot%20per%20group%20%7C%20say%20what%20is%20counted%20(plots%2C%20or%20households%20represented)%20and%20keep%20the%20bins%20equal%20%7C%0A%20%20%20%20%7C%20how%20a%20value%20changes%20over%20time%20%7C%20**trend**%20%7C%20a%20line%20over%20time%20%7C%20time%20runs%20left%20to%20right%20at%20an%20even%20pace%3B%20a%20period%20with%20no%20data%20is%20a%20**gap**%2C%20never%20a%20straight%20line%20across%20it%20%7C%0A%0A%20%20%20%20And%20for%20every%20chart%3A%20a%20**title**%20that%20says%20what%20it%20shows%2C%20**axis%20titles%20with%20units**%20(t%2Fha%2C%20PHP%2Fkg%2C%20households)%2C%20and%20the%20source%20when%20it%20leaves%20your%20notebook.%20The%20examples%20in%20this%20section%20use%20**matplotlib**%2C%20whose%20every%20part%20you%20set%20yourself.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Comparison%3A%20bars%20start%20at%20zero%0A%0A%20%20%20%20The%20survey%20weights%20say%20how%20many%20farm%20households%20each%20household%20in%20the%20survey%20stands%20for%2C%20so%20their%20sum%20by%20province%20estimates%20how%20many%20farm%20households%20each%20province%20has.%20Nueva%20Ecija's%20total%20is%20about%20**4.3%20times**%20Ilocos%20Norte's.%20Move%20the%20slider%20to%20start%20the%20value%20axis%20somewhere%20other%20than%20zero%2C%20and%20watch%20what%20the%20bars%20claim.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20baseline%20%3D%20mo.ui.slider(start%3D0%2C%20stop%3D50_000%2C%20step%3D5_000%2C%20value%3D0%2C%20label%3D%22Where%20the%20value%20axis%20starts%3A%22%2C%20show_value%3DTrue)%0A%20%20%20%20baseline%0A%20%20%20%20return%20(baseline%2C)%0A%0A%0A%40app.cell%0Adef%20_(baseline%2C%20households)%3A%0A%20%20%20%20%23%20This%20cell%20reads%20baseline.value%2C%20so%20it%20runs%20again%20whenever%20you%20move%20the%20slider.%0A%20%20%20%20_totals%20%3D%20households.groupby(%22province%22)%5B%22weight%22%5D.sum().sort_values()%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(7%2C%203.2))%0A%20%20%20%20_ax.barh(_totals.index%2C%20_totals.to_numpy()%2C%20color%3D%22%234c78a8%22)%0A%20%20%20%20_ax.set_xlim(left%3Dbaseline.value)%20%20%23%20where%20the%20axis%20starts%3A%20anything%20but%200%20cuts%20every%20bar%20short%0A%20%20%20%20_ax.set_xlabel(%22Farm%20households%20(estimated%20from%20the%20survey%20weights)%22)%0A%20%20%20%20_ax.set_title(%22Farm%20households%20by%20province%22%2C%20loc%3D%22left%22)%0A%20%20%20%20_ax.xaxis.set_major_formatter(%22%7Bx%3A%2C.0f%7D%22)%0A%20%20%20%20_real%20%3D%20_totals.max()%20%2F%20_totals.min()%0A%20%20%20%20_drawn%20%3D%20(_totals.max()%20-%20baseline.value)%20%2F%20(_totals.min()%20-%20baseline.value)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20_ax%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22Nueva%20Ecija%20has%20**%7B_real%3A.1f%7D%20times**%20as%20many%20farm%20households%20as%20Ilocos%20Norte.%20With%20the%20axis%20starting%20at%20**%7Bbaseline.value%3A%2C%7D**%2C%20its%20bar%20is%20drawn%20**%7B_drawn%3A.1f%7D%20times**%20as%20long.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2B%20(%22%22%20if%20baseline.value%20%3D%3D%200%20else%20%22%20The%20numbers%20did%20not%20change%3B%20the%20picture%20did.%22))%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Bars%20are%20read%20by%20their%20**length**%2C%20so%20the%20axis%20under%20bars%20always%20starts%20at%20zero.%20matplotlib%2C%20seaborn%20and%20Altair%20all%20start%20a%20bar%20chart's%20axis%20at%20zero%20by%20themselves%3B%20what%20moves%20it%20is%20a%20line%20you%20add%20(%60ax.set_xlim(left%3D...)%60%20or%20%60ax.set_ylim(bottom%3D...)%60%20in%20matplotlib%2C%20a%20%60domain%60%20that%20starts%20above%20zero%20in%20Altair%2C%20%60scale%3Dalt.Scale(domain%3D%5B3.5%2C%205%5D)%60%3B%20Altair's%20%60zero%3DFalse%60%20does%20not%20move%20it%2C%20since%20a%20bar's%20own%20base%20is%20at%20zero).%20If%20the%20differences%20are%20too%20small%20to%20see%20from%20zero%2C%20the%20honest%20fix%20is%20a%20different%20chart%20(a%20dot%20plot%20of%20the%20values%2C%20or%20the%20differences%20themselves)%2C%20not%20a%20shorter%20axis.%0A%0A%20%20%20%20Two%20more%20choices%20make%20a%20comparison%20easy%20to%20read%3A%20**horizontal%20bars**%20when%20the%20category%20names%20are%20long%20(province%20names%20are)%2C%20and%20**sorting**%20the%20bars%20by%20value%2C%20so%20the%20reader%20sees%20the%20order%20at%20once.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Distribution%3A%20what%20is%20counted%2C%20and%20how%20the%20values%20spread%0A%0A%20%20%20%20A%20mean%20is%20one%20number%3B%20a%20distribution%20shows%20all%20of%20them.%20A%20**histogram**%20cuts%20the%20range%20of%20values%20into%20equal%20**bins**%20and%20draws%20how%20many%20fall%20in%20each.%20A%20**box%20plot**%20draws%20five%20numbers%20per%20group%3A%20the%20line%20inside%20the%20box%20is%20the%20**median**%20(half%20the%20values%20are%20above%20it)%2C%20the%20box%20holds%20the%20middle%20half%20of%20the%20values%2C%20the%20whiskers%20reach%20the%20furthest%20values%20within%201.5%20box-lengths%2C%20and%20dots%20beyond%20them%20are%20single%20unusual%20values.%0A%0A%20%20%20%20The%20histogram%20below%20counts%20**plots%20in%20the%20survey**.%20The%20chart%20under%20it%20compares%2C%20bin%20by%20bin%2C%20the%20share%20of%20plots%20in%20the%20survey%20(the%20black%20outline)%20with%20the%20share%20of%20**plots%20represented**%20(orange)%3A%20each%20plot%20weighted%20by%20its%20household's%20survey%20weight%2C%20so%20it%20describes%20the%20provinces'%20rice%20plots%20rather%20than%20the%20sample.%20Here%20the%20two%20shapes%20are%20close%3B%20the%20weighted%20one%20leans%20a%20little%20toward%20higher%20yields%2C%20since%20Nueva%20Ecija's%20plots%20stand%20for%20the%20most%20households.%20Say%20which%20one%20you%20drew%2C%20in%20the%20axis%20title.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(rice)%3A%0A%20%20%20%20_fig%2C%20(_counted%2C%20_boxes)%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(10%2C%203.6))%0A%20%20%20%20_bins%20%3D%20np.arange(1.5%2C%209.5%2C%200.5)%20%20%23%20equal%20bins%2C%200.5%20t%2Fha%20wide%3A%20say%20so%2C%20since%20other%20bins%20draw%20another%20shape%0A%20%20%20%20_counted.hist(rice%5B%22yield_t_ha%22%5D%2C%20bins%3D_bins%2C%20color%3D%22%234c78a8%22%2C%20edgecolor%3D%22white%22)%0A%20%20%20%20_counted.set_xlabel(%22Rice%20yield%20(t%2Fha)%2C%20bins%200.5%20t%2Fha%20wide%22)%0A%20%20%20%20_counted.set_ylabel(%22Plots%20in%20the%20survey%22)%0A%20%20%20%20_counted.set_title(%22Most%20rice%20plots%20yield%203%20to%205.5%20t%2Fha%22%2C%20loc%3D%22left%22)%0A%20%20%20%20_order%20%3D%20rice.groupby(%22province%22)%5B%22yield_t_ha%22%5D.median().sort_values().index%0A%20%20%20%20_boxes.boxplot(%5Brice.loc%5Brice%5B%22province%22%5D%20%3D%3D%20p%2C%20%22yield_t_ha%22%5D%20for%20p%20in%20_order%5D%2C%20tick_labels%3D_order%2C%20orientation%3D%22horizontal%22)%0A%20%20%20%20_boxes.set_xlabel(%22Rice%20yield%20(t%2Fha)%22)%0A%20%20%20%20_boxes.set_title(%22Rice%20yield%20by%20province%2C%20plot%20by%20plot%22%2C%20loc%3D%22left%22)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_weighted%2C%20_%20%3D%20np.histogram(rice%5B%22yield_t_ha%22%5D%2C%20bins%3D_bins%2C%20weights%3Drice%5B%22weight%22%5D)%0A%20%20%20%20_plain%2C%20_%20%3D%20np.histogram(rice%5B%22yield_t_ha%22%5D%2C%20bins%3D_bins)%0A%20%20%20%20_fig2%2C%20_ax2%20%3D%20plt.subplots(figsize%3D(10%2C%202.6))%0A%20%20%20%20_ax2.bar(_bins%5B%3A-1%5D%2C%20_weighted%20%2F%20_weighted.sum()%2C%20width%3D0.5%2C%20align%3D%22edge%22%2C%20color%3D%22%23f58518%22%2C%20edgecolor%3D%22white%22%2C%20label%3D%22plots%20represented%20(weighted)%22)%0A%20%20%20%20_ax2.stairs(_plain%20%2F%20_plain.sum()%2C%20_bins%2C%20color%3D%22black%22%2C%20linewidth%3D1.5%2C%20label%3D%22plots%20in%20the%20survey%22)%0A%20%20%20%20_ax2.set_xlabel(%22Rice%20yield%20(t%2Fha)%2C%20bins%200.5%20t%2Fha%20wide%22)%0A%20%20%20%20_ax2.set_ylabel(%22Share%20of%20plots%22)%0A%20%20%20%20_ax2.legend(frameon%3DFalse)%0A%20%20%20%20mo.vstack(%5B_fig%2C%20_ax2%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20box%20plot%20shows%20what%20a%20bar%20of%20means%20hides%3A%20in%20every%20province%20the%20plots%20spread%20widely%2C%20and%20the%20provinces%20overlap.%20A%20chart%20of%20means%20alone%20would%20let%20a%20reader%20think%20every%20plot%20in%20Nueva%20Ecija%20out-yields%20every%20plot%20in%20Camarines%20Sur%3B%20the%20boxes%20show%20it%20isn't%20so.%0A%0A%20%20%20%20%23%23%23%20Trend%3A%20time%20runs%20evenly%2C%20and%20a%20gap%20stays%20a%20gap%0A%0A%20%20%20%20A%20line%20chart%20joins%20each%20point%20to%20the%20next.%20When%20a%20month%20has%20**no%20row**%20(September%202023%20here)%2C%20the%20line%20joins%20August%20straight%20to%20October%20and%20draws%20a%20month%20nobody%20measured.%20The%20chart%20on%20the%20left%20does%20that%3B%20the%20one%20on%20the%20right%20gives%20September%20a%20row%20whose%20price%20is%20**missing**%20(%60NaN%60)%2C%20and%20matplotlib%20breaks%20the%20line%20there.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(prices)%3A%0A%20%20%20%20_palay%20%3D%20prices%5Bprices%5B%22commodity%22%5D%20%3D%3D%20%22Palay%22%5D.groupby(%22month%22)%5B%22price%22%5D.mean()%20%20%23%20one%20price%20per%20month%3A%20the%20provinces'%20mean%0A%20%20%20%20_every_month%20%3D%20_palay.reindex(pd.period_range(_palay.index.min()%2C%20_palay.index.max()%2C%20freq%3D%22M%22))%20%20%23%20September%202023%20comes%20back%2C%20as%20NaN%0A%20%20%20%20_fig%2C%20(_bridged%2C%20_gap)%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(10%2C%203)%2C%20sharey%3DTrue)%0A%20%20%20%20_bridged.plot(_palay.index.to_timestamp()%2C%20_palay.to_numpy()%2C%20color%3D%22%23e45756%22)%0A%20%20%20%20_bridged.set_title(%22Bridged%3A%20September%202023%20drawn%20as%20if%20measured%22%2C%20loc%3D%22left%22%2C%20fontsize%3D10)%0A%20%20%20%20_gap.plot(_every_month.index.to_timestamp()%2C%20_every_month.to_numpy()%2C%20color%3D%22%234c78a8%22)%0A%20%20%20%20_gap.set_title(%22Honest%3A%20the%20missing%20month%20is%20a%20gap%22%2C%20loc%3D%22left%22%2C%20fontsize%3D10)%0A%20%20%20%20_bridged.set_ylabel(%22Palay%2C%20mean%20farmgate%20price%20(PHP%2Fkg)%22)%0A%20%20%20%20for%20_ax%20in%20(_bridged%2C%20_gap)%3A%0A%20%20%20%20%20%20%20%20_ax.set_xlim(pd.Timestamp(%222023-03-01%22)%2C%20pd.Timestamp(%222024-03-01%22))%20%20%23%20zoom%20in%20on%20the%20missing%20month%0A%20%20%20%20%20%20%20%20_ax.tick_params(axis%3D%22x%22%2C%20labelrotation%3D45)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Three%20things%20to%20know%20before%20you%20draw%20a%20trend%3A%0A%0A%20%20%20%20-%20**Months%20as%20dates.**%20%60course.prices()%60%20keeps%20months%20as%20pandas%20**periods**%20(%602023-09%60).%20matplotlib%20cannot%20place%20a%20period%20on%20an%20axis%20(%60TypeError%3A%20float()%20argument%20must%20be%20...%20not%20'Period'%60)%3B%20%60.to_timestamp()%60%20on%20a%20period%20index%2C%20or%20%60.dt.to_timestamp()%60%20on%20a%20column%2C%20turns%20each%20into%20the%20date%20of%20its%20first%20day.%0A%20%20%20%20-%20**The%20missing%20month%20back%2C%20as%20a%20missing%20value.**%20%60table.reindex(pd.period_range(first%2C%20last%2C%20freq%3D%22M%22))%60%20gives%20a%20table%20one%20row%20per%20month%2C%20with%20%60NaN%60%20wherever%20a%20month%20had%20no%20row%20(%5Bmodule%205%5D(%2F%3Ffile%3D40_cleaning_and_time.py)%2C%20section%204).%0A%20%20%20%20-%20**A%20trend%20needn't%20start%20at%20zero.**%20A%20line%20is%20read%20by%20its%20rise%20and%20fall%2C%20not%20its%20length%2C%20so%20its%20axis%20may%20start%20near%20the%20data.%20Bars%20may%20not.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%207.1.%20Comparison%3A%20weighted%20mean%20yield%20by%20province%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20%22which%20province%20yields%20most%3F%22%20is%20a%20comparison%2C%20and%20the%20survey%20weights%20decide%20the%20answer%3A%20Nueva%20Ecija's%20plots%20stand%20for%20more%20households%20than%20any%20other%20province's.%20A%20plain%20mean%20of%20the%20plots%20in%20the%20sample%20answers%20a%20different%20question.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20of%20plots%20(columns%20%60province%60%2C%20%60yield_t_ha%60%2C%20%60weight%60%2C%20among%20others)%20%E2%86%92%20a%20**bar%20chart**%20with%20one%20bar%20per%20province%2C%20whose%20length%20is%20the%20province's%20**weighted%20mean%20yield**%2C%20on%20a%20value%20axis%20that%20**starts%20at%20zero**%20and%20whose%20title%20contains%20the%20unit%20**%60t%2Fha%60**.%0A%0A%20%20%20%20The%20weighted%20mean%20is%20the%20sum%20of%20yield%20%C3%97%20weight%20over%20the%20sum%20of%20weights%20(%5Bmodule%204%5D(%2F%3Ffile%3D30_summarize_and_combine.py)%2C%20section%203).%20For%20two%20plots%20in%20one%20province%3A%0A%0A%20%20%20%20%7C%20yield_t_ha%20%7C%20weight%20%7C%20yield%20%C3%97%20weight%20%7C%0A%20%20%20%20%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%206.0%20%7C%20300%20%7C%201%2C800%20%7C%0A%20%20%20%20%7C%202.0%20%7C%20100%20%7C%20200%20%7C%0A%20%20%20%20%7C%20**sum**%20%7C%20**400**%20%7C%20**2%2C000**%20%7C%0A%0A%20%20%20%20Weighted%20mean%3A%202%2C000%20%2F%20400%20%3D%20**5.0**%20t%2Fha%2C%20where%20the%20plain%20mean%20says%204.0.%0A%0A%20%20%20%20Tools%3A%20%60table.assign(...)%60%20and%20%60groupby(%22province%22)%5B%5B...%5D%5D.sum()%60%20for%20the%20two%20sums%3B%20then%20any%20library.%20In%20matplotlib%3A%20%60fig%2C%20ax%20%3D%20plt.subplots()%60%2C%20%60ax.barh(names%2C%20values)%60%2C%20%60ax.set_xlabel(%22Weighted%20mean%20yield%20(t%2Fha)%22)%60%2C%20and%20%60return%20ax%60.%20In%20seaborn%2C%20%60sns.barplot(table%2C%20x%3D...%2C%20y%3D...%2C%20weights%3D%22weight%22%2C%20errorbar%3DNone%2C%20ax%3Dax)%60%20computes%20the%20weighted%20means%20itself.%20In%20Altair%2C%20chart%20a%20table%20of%20the%20means%20with%20%60alt.X(%22...%3AQ%22%2C%20title%3D%22...%22)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20yield_bars(table%3A%20pd.DataFrame)%3A%0A%20%20%20%20%22%22%22A%20bar%20chart%20of%20each%20province's%20weighted%20mean%20yield%2C%20from%20zero%2C%20with%20the%20unit%20on%20the%20value%20axis.%22%22%22%0A%20%20%20%20%23%20Exercise%207.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20add%20a%20column%20of%20yield%20%C3%97%20weight%3B%20group%20by%20province%20and%20sum%20it%20and%20the%20weights%0A%20%20%20%20%23%20Step%202%3A%20divide%20the%20two%20sums%3A%20each%20province's%20weighted%20mean%20yield%20(sort%20it%2C%20for%20an%20easy%20read)%0A%20%20%20%20%23%20Step%203%3A%20fig%2C%20ax%20%3D%20plt.subplots()%2C%20then%20one%20bar%20per%20province%20(ax.barh%20suits%20long%20names)%0A%20%20%20%20%23%20Step%204%3A%20title%20the%20value%20axis%20with%20the%20unit%2C%20such%20as%20%22Weighted%20mean%20yield%20(t%2Fha)%22%2C%20and%20return%20ax%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%207.1%22)%0A%0A%0A%40app.function%0Adef%20plots_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Eight%20hand-made%20plots%20in%20three%20provinces%2C%20where%20a%20weighted%20mean%20and%20a%20plain%20one%20differ%20(Exercises%207.1%2C%207.3%2C%207.5%2C%207.6).%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B%22HH-0001%22%2C%20%22HH-0001%22%2C%20%22HH-0002%22%2C%20%22HH-0003%22%2C%20%22HH-0003%22%2C%20%22HH-0004%22%2C%20%22HH-0005%22%2C%20%22HH-0005%22%5D%2C%0A%20%20%20%20%20%20%20%20%22plot_no%22%3A%20%5B1%2C%202%2C%201%2C%201%2C%202%2C%201%2C%201%2C%202%5D%2C%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22Iloilo%22%2C%20%22Iloilo%22%2C%20%22Isabela%22%2C%20%22Isabela%22%2C%20%22Isabela%22%2C%20%22Bukidnon%22%2C%20%22Bukidnon%22%2C%20%22Iloilo%22%5D%2C%0A%20%20%20%20%20%20%20%20%22crop%22%3A%20%5B%22Rice%22%2C%20%22Rice%22%2C%20%22Rice%22%2C%20%22Corn%22%2C%20%22Corn%22%2C%20%22Banana%22%2C%20%22Corn%22%2C%20%22Vegetables%22%5D%2C%0A%20%20%20%20%20%20%20%20%22irrigated%22%3A%20%5B%22Yes%22%2C%20%22No%22%2C%20%22Yes%22%2C%20%22No%22%2C%20%22No%22%2C%20%22No%22%2C%20%22No%22%2C%20%22No%22%5D%2C%0A%20%20%20%20%20%20%20%20%22yield_t_ha%22%3A%20%5B6.0%2C%202.0%2C%205.0%2C%203.0%2C%204.0%2C%2020.0%2C%202.5%2C%2012.0%5D%2C%0A%20%20%20%20%20%20%20%20%22weight%22%3A%20%5B300.0%2C%20100.0%2C%20200.0%2C%20100.0%2C%20300.0%2C%2050.0%2C%20150.0%2C%20150.0%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(plots%2C%20rice)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%207.1%22%2C%20yield_bars%2C%0A%20%20%20%20%20%20%20%20%5B(%22eight%20hand-made%20plots%20in%20three%20provinces%22%2C%20(plots_fixture()%2C))%2C%20(%22the%20survey's%20rice%20plots%22%2C%20(rice%2C))%2C%20(%22the%20survey's%20corn%20plots%22%2C%20(plots%5Bplots%5B%22crop%22%5D%20%3D%3D%20%22Corn%22%5D%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dbars_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20check%20reads%20each%20bar's%20province%20and%20length%2C%20whether%20the%20value%20axis%20starts%20at%20zero%2C%20and%20whether%20its%20title%20contains%20t%2Fha.%20On%20the%20hand-made%20table%2C%20Isabela's%20weighted%20mean%20is%204.1667%20and%20Bukidnon's%206.875%20(its%20plain%20mean%2C%2011.25%2C%20is%20far%20off).%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plots)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20chart%20of%20the%20corn%20plots.%20Change%20%22Corn%22%20to%20%22Rice%22%2C%20%22Vegetables%22%20or%20%22Banana%22.%0A%20%20%20%20show_chart(yield_bars%2C%20plots%5Bplots%5B%22crop%22%5D%20%3D%3D%20%22Corn%22%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%207.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60'value%20axis%20names%20the%20unit'%3A%20False%60%20%7C%20The%20value%20axis%20has%20no%20title%2C%20or%20seaborn's%20default%20one%20(the%20column%20name%2C%20%60yield_t_ha%60).%20%7C%20%60ax.set_xlabel(%22Weighted%20mean%20yield%20(t%2Fha)%22)%60%20(or%20%60set_ylabel%60%20for%20upright%20bars)%3B%20in%20Altair%20%60alt.X(%22...%3AQ%22%2C%20title%3D%22...%22)%60%20%7C%0A%20%20%20%20%7C%20%60'value%20axis%20starts%20at%20zero'%3A%20False%60%20%7C%20The%20axis%20was%20moved%20off%20zero.%20%7C%20remove%20%60set_xlim%60%20%2F%20%60set_ylim%60%2C%20or%20a%20%60domain%60%20in%20%60alt.Scale%60%20%7C%0A%20%20%20%20%7C%20The%20bars%20are%20close%20but%20not%20right%20%7C%20A%20plain%20mean%3A%20%60.mean()%60%2C%20or%20seaborn%20without%20%60weights%3D%22weight%22%60%2C%20or%20Altair's%20%60mean(yield_t_ha)%60.%20%7C%20the%20sum%20of%20yield%20%C3%97%20weight%20over%20the%20sum%20of%20weights%20%7C%0A%20%20%20%20%7C%20Extra%20rows%20with%20%60'mark'%3A%20'line'%60%20%7C%20seaborn's%20error%20bars%2C%20drawn%20by%20default.%20%7C%20%60errorbar%3DNone%60%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20chart_data%20reads%20...%20not%20a%20builtins.NoneType*%20%7C%20The%20function%20draws%20but%20returns%20nothing.%20%7C%20end%20with%20%60return%20ax%60%20(or%20%60return%20chart%60%20in%20Altair)%20%7C%0A%20%20%20%20%7C%20Bars%20from%20another%20chart%20appear%20too%20%7C%20seaborn%20drew%20on%20the%20last%20chart%20still%20open.%20%7C%20start%20with%20%60fig%2C%20ax%20%3D%20plt.subplots()%60%20and%20pass%20%60ax%3Dax%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%207.2.%20Trend%3A%20farmgate%20prices%20by%20month%2C%20with%20the%20gap%20shown%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20price%20chart%20is%20read%20for%20its%20rises%20and%20falls.%20A%20line%20drawn%20straight%20across%20September%202023%20invents%20a%20month%20of%20prices%3B%20a%20reader%20planning%20a%20sale%20from%20it%20trusts%20a%20number%20that%20was%20never%20collected.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20prices%20table%20(%60month%60%20as%20periods%2C%20%60province%60%2C%20%60commodity%60%2C%20%60price%60)%20and%20a%20province's%20name%20%E2%86%92%20a%20**line%20chart**%20with%20one%20line%20per%20commodity%2C%20months%20on%20the%20x%20axis%20as%20**dates**%2C%20every%20month%20from%20the%20first%20to%20the%20last%2C%20and%20**September%202023%20a%20gap**%20in%20every%20line.%20The%20value%20axis's%20title%20contains%20the%20unit%20**%60PHP%2Fkg%60**.%0A%0A%20%20%20%20%7C%20month%20%7C%20Palay%20%7C%20Tomato%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%202023-08%20%7C%2021.4%20%7C%2030.2%20%7C%0A%20%20%20%20%7C%202023-09%20%7C%20NaN%20%7C%20NaN%20%7C%0A%20%20%20%20%7C%202023-10%20%7C%2022.0%20%7C%2035.9%20%7C%0A%0A%20%20%20%20Tools%3A%20filter%20the%20province%3B%20%60pivot(index%3D%22month%22%2C%20columns%3D%22commodity%22%2C%20values%3D%22price%22)%60%20(%5Bmodule%204%5D(%2F%3Ffile%3D30_summarize_and_combine.py)%2C%20section%204)%20gives%20one%20column%20per%20commodity%3B%20%60reindex(pd.period_range(first%2C%20last%2C%20freq%3D%22M%22))%60%20brings%20back%20the%20missing%20month%20as%20%60NaN%60%3B%20%60.to_timestamp()%60%20turns%20the%20months%20into%20dates.%20In%20matplotlib%2C%20one%20%60ax.plot(dates%2C%20column%2C%20label%3Dcommodity)%60%20per%20column%2C%20%60ax.set_ylabel(...)%60%2C%20%60ax.legend()%60%2C%20and%20%60return%20ax%60.%20In%20Altair%2C%20melt%20the%20wide%20table%20back%20to%20long%20and%20use%20%60color%3D%22commodity%3AN%22%60%3A%20a%20missing%20price%20breaks%20an%20Altair%20line%20too.%20seaborn%20drops%20missing%20values%20before%20it%20draws%2C%20which%20bridges%20the%20gap%3B%20section%202%20shows%20the%20way%20around%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20price_lines(prices%3A%20pd.DataFrame%2C%20province%3A%20str)%3A%0A%20%20%20%20%22%22%22Each%20commodity's%20farmgate%20price%20by%20month%20in%20one%20province%2C%20one%20line%20each%2C%20the%20missing%20month%20a%20gap.%22%22%22%0A%20%20%20%20%23%20Exercise%207.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20keep%20the%20province's%20rows%3B%20pivot%20to%20one%20column%20per%20commodity%2C%20one%20row%20per%20month%0A%20%20%20%20%23%20Step%202%3A%20reindex%20to%20every%20month%20from%20the%20first%20to%20the%20last%20(pd.period_range)%2C%20so%20the%20missing%20month%20is%20NaN%0A%20%20%20%20%23%20Step%203%3A%20fig%2C%20ax%20%3D%20plt.subplots()%3B%20one%20ax.plot%20per%20commodity%2C%20with%20the%20months%20as%20dates%20(.to_timestamp())%0A%20%20%20%20%23%20Step%204%3A%20label%20the%20value%20axis%20with%20the%20unit%20(PHP%2Fkg)%2C%20add%20a%20legend%2C%20and%20return%20ax%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%207.2%22)%0A%0A%0A%40app.function%0Adef%20prices_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Two%20commodities%20in%20two%20provinces%2C%20July%20to%20November%202023%2C%20with%20no%20rows%20for%20September%20(Exercise%207.2).%22%22%22%0A%20%20%20%20months%20%3D%20pd.PeriodIndex(%5B%222023-07%22%2C%20%222023-08%22%2C%20%222023-10%22%2C%20%222023-11%22%5D%2C%20freq%3D%22M%22)%0A%20%20%20%20rows%20%3D%20%5B(month%2C%20province%2C%20commodity%2C%20base%20%2B%20step%20*%20i)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20province%2C%20shift%20in%20((%22Iloilo%22%2C%200.0)%2C%20(%22Isabela%22%2C%201.5))%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20commodity%2C%20base%2C%20step%20in%20((%22Palay%22%2C%2020.0%20%2B%20shift%2C%200.5)%2C%20(%22Tomato%22%2C%2030.0%20%2B%20shift%2C%202.25))%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20i%2C%20month%20in%20enumerate(months)%5D%0A%20%20%20%20return%20pd.DataFrame(rows%2C%20columns%3D%5B%22month%22%2C%20%22province%22%2C%20%22commodity%22%2C%20%22price%22%5D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(prices)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%207.2%22%2C%20price_lines%2C%0A%20%20%20%20%20%20%20%20%5B(%22two%20commodities%20in%20Iloilo%2C%20no%20rows%20for%20September%202023%22%2C%20(prices_fixture()%2C%20%22Iloilo%22))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20farmgate%20prices%2C%20Nueva%20Ecija%22%2C%20(prices%2C%20%22Nueva%20Ecija%22))%2C%20(%22the%20farmgate%20prices%2C%20Cotabato%22%2C%20(prices%2C%20%22Cotabato%22))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlines_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20check%20reads%20every%20point%20each%20line%20draws%2C%20counts%20the%20lines%20drawn%20straight%20across%20a%20missing%20month%20(there%20should%20be%20none)%2C%20and%20reads%20whether%20the%20value%20axis's%20title%20contains%20PHP%2Fkg.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(prices)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20chart%20for%20Iloilo.%20Try%20another%20province%3A%20Ilocos%20Norte%2C%20Isabela%2C%20Camarines%20Sur%2C%20Bukidnon.%0A%20%20%20%20show_chart(price_lines%2C%20prices%2C%20%22Iloilo%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%207.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60'lines%20drawn%20across%20a%20gap'%3A%204%60%20(or%208%2C%2012)%20%7C%20The%20missing%20month%20has%20no%20row%2C%20so%20each%20line%20joins%20August%20to%20October.%20%7C%20%60reindex(pd.period_range(...))%60%20before%20drawing%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20float()%20argument%20must%20be%20a%20string%20or%20a%20real%20number%2C%20not%20'Period'*%20%7C%20matplotlib%20was%20given%20periods.%20%7C%20%60wide.index.to_timestamp()%60%20%7C%0A%20%20%20%20%7C%20%60'value%20axis%20names%20the%20unit'%3A%20False%60%20%7C%20No%20y-axis%20title%2C%20or%20one%20without%20the%20unit.%20%7C%20%60ax.set_ylabel(%22Farmgate%20price%20(PHP%2Fkg)%22)%60%20%7C%0A%20%20%20%20%7C%20Every%20row's%20%60series%60%20is%20%60None%60%20%7C%20The%20lines%20have%20no%20names%2C%20so%20the%20check%20can't%20tell%20them%20apart.%20%7C%20%60label%3Dcommodity%60%20in%20each%20%60ax.plot%60%3B%20in%20Altair%20%60color%3D%22commodity%3AN%22%60%20%7C%0A%20%20%20%20%7C%20The%20x%20values%20look%20like%20%60'2023-07'%60%20%7C%20The%20months%20were%20drawn%20as%20text%2C%20so%20they%20aren't%20spaced%20as%20time.%20%7C%20dates%2C%20from%20%60.to_timestamp()%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'Palay'*%20or%20an%20empty%20chart%20%7C%20The%20province's%20name%20is%20misspelt%2C%20or%20the%20filter%20kept%20no%20rows.%20%7C%20%60prices%5B%22province%22%5D.unique()%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202%20%C2%B7%20matplotlib%2C%20seaborn%20or%20Altair%3A%20when%20each%20fits%0A%0A%20%20%20%20You%20now%20know%20which%20chart%20a%20question%20needs.%20The%20libraries%20differ%20in%20**what%20you%20describe**%20and%20**what%20they%20decide%20for%20you**%2C%20and%20those%20decisions%20can%20change%20what%20a%20chart%20says.%20One%20question%2C%20drawn%20in%20all%20three%3A%20*do%20irrigated%20rice%20plots%20yield%20more%20than%20rain-fed%20ones%2C%20in%20every%20province%3F*%20It%20is%20a%20comparison%20by%20two%20categories%20at%20once%3A%20a%20pair%20of%20bars%20per%20province.%0A%0A%20%20%20%20%23%23%23%20seaborn%3A%20from%20the%20raw%20rows%2C%20with%20statistics%20built%20in%0A%0A%20%20%20%20seaborn%20takes%20the%20**whole%20table**%20and%20the%20column%20names%2C%20and%20computes%20the%20statistic%20itself%20(a%20mean%2C%20by%20default).%20It%20names%20each%20color%20in%20a%20legend%2C%20and%20adds%20**error%20bars**.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(rice)%3A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(7%2C%204))%0A%20%20%20%20sns.barplot(rice%2C%20x%3D%22yield_t_ha%22%2C%20y%3D%22province%22%2C%20hue%3D%22irrigated%22%2C%20weights%3D%22weight%22%2C%20ax%3D_ax)%20%20%23%20weights%3D%3A%20a%20weighted%20mean%0A%20%20%20%20_ax.set_xlabel(%22Weighted%20mean%20rice%20yield%20(t%2Fha)%22)%0A%20%20%20%20_ax.set_ylabel(%22%22)%0A%20%20%20%20_ax.legend(title%3D%22Irrigated%22%2C%20frameon%3DFalse)%0A%20%20%20%20_ax%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20One%20line%20drew%20fourteen%20bars%2C%20weighted%20(%60weights%3D%60%20arrived%20in%20seaborn%200.13.1).%20Its%20decisions%20are%20worth%20knowing%3A%0A%0A%20%20%20%20-%20**The%20mean%20is%20plain%20unless%20you%20say%20otherwise.**%20Without%20%60weights%3D%22weight%22%60%2C%20every%20plot%20counts%20once.%20For%20a%20survey%2C%20that%20is%20the%20wrong%20number.%0A%20%20%20%20-%20**The%20black%20lines%20are%20a%2095%25%20interval%20from%20a%20bootstrap**%3A%20seaborn%20resamples%20the%20plots%20a%20thousand%20times%20and%20redraws%20the%20mean.%20It%20treats%20the%20plots%20as%20a%20simple%20random%20sample%2C%20which%20a%20survey%20is%20not%20(households%20were%20sampled%2C%20and%20a%20household's%20plots%20are%20alike)%2C%20so%20these%20are%20not%20the%20survey's%20margins%20of%20error.%20%60errorbar%3DNone%60%20leaves%20them%20out%3B%20the%20stretch%20exercises%20draw%20honest%20ones.%0A%20%20%20%20-%20**Missing%20values%20are%20dropped%20before%20drawing.**%20Even%20when%20the%20missing%20month%20has%20a%20row%2C%20seaborn%20drops%20its%20missing%20price%2C%20and%20the%20line%20goes%20straight%20across.%20To%20keep%20the%20gap%2C%20draw%20each%20unbroken%20stretch%20as%20its%20own%20line%3A%20number%20the%20stretches%20(%60stretch%20%3D%20price.isna().cumsum()%60%20within%20each%20line)%2C%20then%20%60sns.lineplot(...%2C%20units%3D%22stretch%22%2C%20estimator%3DNone)%60.%0A%0A%20%20%20%20%23%23%23%20matplotlib%20and%20Altair%3A%20from%20a%20table%20of%20what%20to%20draw%0A%0A%20%20%20%20matplotlib%20draws%20exactly%20the%20marks%20you%20give%20it%2C%20and%20Altair%20computes%20only%20plain%20statistics%20(%60mean(yield_t_ha)%60%20in%20an%20encoding%20is%20a%20plain%20mean).%20For%20a%20weighted%20mean%2C%20both%20want%20a%20**summary%20table**%3A%20one%20row%20per%20bar%2C%20the%20weighted%20means%20already%20computed.%20In%20that%20tidy%20form%20(%5Bmodule%204%5D(%2F%3Ffile%3D30_summarize_and_combine.py)%2C%20section%204)%2C%20every%20library%20draws%20the%20same%20bars.%20That%20table%20is%20your%20next%20exercise.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%207.3.%20One%20summary%20for%20every%20library%3A%20weighted%20means%20by%20group%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20compute%20the%20statistic%20once%2C%20in%20pandas%2C%20where%20you%20can%20check%20it%2C%20and%20any%20library%20can%20draw%20it.%20It%20also%20stops%20a%20library's%20own%20default%20(a%20plain%20mean)%20from%20deciding%20your%20numbers.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20of%20plots%20and%20a%20**list**%20of%20column%20names%20to%20group%20by%20%E2%86%92%20a%20tidy%20table%3A%20the%20group%20columns%2C%20then%20%60yield_t_ha%60%20(the%20group's%20weighted%20mean%20yield)%2C%20one%20row%20per%20group%2C%20with%20a%20fresh%20index%20(0%2C%201%2C%202%2C%20%E2%80%A6).%0A%0A%20%20%20%20%60weighted_means(rice%2C%20%5B%22province%22%2C%20%22irrigated%22%5D)%60%3A%0A%0A%20%20%20%20%7C%20%7C%20province%20%7C%20irrigated%20%7C%20yield_t_ha%20%7C%0A%20%20%20%20%7C--%3A%7C%3A--%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%200%20%7C%20Bukidnon%20%7C%20No%20%7C%203.4686%20%7C%0A%20%20%20%20%7C%201%20%7C%20Bukidnon%20%7C%20Yes%20%7C%204.8730%20%7C%0A%20%20%20%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20Tools%3A%20as%20in%20Exercise%207.1%2C%20with%20%60groupby(by)%60%20for%20any%20list%20of%20columns%3B%20%60.rename(%22yield_t_ha%22)%60%20names%20the%20result%2C%20and%20%60.reset_index()%60%20turns%20the%20groups%20back%20into%20columns.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20weighted_means(table%3A%20pd.DataFrame%2C%20by%3A%20list%5Bstr%5D)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20weighted%20mean%20yield%20of%20each%20group%20of%20plots%3A%20the%20group%20columns%2C%20then%20yield_t_ha.%22%22%22%0A%20%20%20%20%23%20Exercise%207.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20add%20a%20column%20of%20yield%20%C3%97%20weight%0A%20%20%20%20%23%20Step%202%3A%20group%20by%20the%20columns%20in%20%60by%60%3B%20sum%20that%20column%20and%20the%20weights%0A%20%20%20%20%23%20Step%203%3A%20divide%3B%20name%20the%20result%20yield_t_ha%20(.rename)%20and%20turn%20the%20groups%20into%20columns%20(.reset_index())%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%207.3%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(plots%2C%20rice)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%207.3%22%2C%20weighted_means%2C%0A%20%20%20%20%20%20%20%20%5B(%22eight%20hand-made%20plots%2C%20by%20province%22%2C%20(plots_fixture()%2C%20%5B%22province%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20(%22eight%20hand-made%20plots%2C%20by%20province%20and%20irrigation%22%2C%20(plots_fixture()%2C%20%5B%22province%22%2C%20%22irrigated%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20survey's%20rice%20plots%2C%20by%20province%20and%20irrigation%22%2C%20(rice%2C%20%5B%22province%22%2C%20%22irrigated%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20(%22every%20plot%20in%20the%20survey%2C%20by%20crop%22%2C%20(plots%2C%20%5B%22crop%22%5D))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20result%20has%20the%20group%20columns%2C%20then%20yield_t_ha%2C%20and%20the%20index%200%2C%201%2C%202%2C%20%E2%80%A6.%20On%20the%20hand-made%20table%20by%20province%2C%20Iloilo's%20weighted%20mean%20is%206.9091%3A%20(6.0%20%C3%97%20300%20%2B%202.0%20%C3%97%20100%20%2B%2012.0%20%C3%97%20150)%20%2F%20550.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(rice)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20rice%20plots%20by%20province%20and%20irrigation%2C%20as%20Exercise%207.4%20and%20the%20charts%20below%20use%20them.%0A%20%20%20%20course.try_it(weighted_means%2C%20rice%2C%20%5B%22province%22%2C%20%22irrigated%22%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%207.3%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20The%20values%20are%20close%20but%20not%20right%20%7C%20A%20plain%20mean%2C%20%60groupby(by)%5B%22yield_t_ha%22%5D.mean()%60.%20%7C%20the%20sum%20of%20yield%20%C3%97%20weight%20over%20the%20sum%20of%20weights%20%7C%0A%20%20%20%20%7C%20The%20groups%20are%20the%20index%2C%20not%20columns%20%7C%20%60.reset_index()%60%20is%20missing.%20%7C%20%60.reset_index()%60%20%7C%0A%20%20%20%20%7C%20The%20last%20column%20has%20another%20name%20(%600%60%2C%20%60weighted_yield%60)%20%7C%20The%20result%20was%20not%20named%20before%20%60reset_index%60.%20%7C%20%60.rename(%22yield_t_ha%22)%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'province'*%20(or%20another%20column)%20%7C%20%60by%60%20was%20used%20as%20one%20name%2C%20or%20a%20column%20is%20misspelt.%20%7C%20%60groupby(by)%60%2C%20with%20%60by%60%20the%20list%20given%20%7C%0A%20%20%20%20%7C%20*TypeError%20...%20unhashable%20type%3A%20'list'*%20%7C%20The%20list%20was%20put%20in%20another%20list%3A%20%60groupby(%5Bby%5D)%60.%20%7C%20%60groupby(by)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20With%20the%20summary%2C%20here%20is%20the%20same%20question%20in%20matplotlib%20and%20in%20Altair.%20Compare%20how%20much%20each%20asks%20of%20you.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(rice)%3A%0A%20%20%20%20_summary%20%3D%20attempt(weighted_means%2C%20rice%2C%20%5B%22province%22%2C%20%22irrigated%22%5D)%0A%20%20%20%20mo.stop(_summary%20is%20NOT_YET%2C%20waiting_for(%22Exercise%207.3%22))%0A%20%20%20%20%23%20matplotlib%3A%20one%20bar%20per%20value%20of%20irrigated%2C%20each%20pair%20shifted%20half%20a%20bar%20width%20either%20side%20of%20its%20province%0A%20%20%20%20_wide%20%3D%20_summary.pivot(index%3D%22province%22%2C%20columns%3D%22irrigated%22%2C%20values%3D%22yield_t_ha%22)%0A%20%20%20%20_rows%20%3D%20np.arange(len(_wide))%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(7%2C%204))%0A%20%20%20%20for%20_shift%2C%20_value%20in%20zip((-0.2%2C%200.2)%2C%20_wide.columns)%3A%0A%20%20%20%20%20%20%20%20_ax.barh(_rows%20%2B%20_shift%2C%20_wide%5B_value%5D%2C%20height%3D0.4%2C%20label%3D_value)%0A%20%20%20%20_ax.set_yticks(_rows%2C%20_wide.index)%0A%20%20%20%20_ax.set_xlabel(%22Weighted%20mean%20rice%20yield%20(t%2Fha)%22)%0A%20%20%20%20_ax.legend(title%3D%22Irrigated%22%2C%20frameon%3DFalse)%0A%20%20%20%20_ax%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(rice)%3A%0A%20%20%20%20_summary%20%3D%20attempt(weighted_means%2C%20rice%2C%20%5B%22province%22%2C%20%22irrigated%22%5D)%0A%20%20%20%20mo.stop(_summary%20is%20NOT_YET%2C%20waiting_for(%22Exercise%207.3%22))%0A%20%20%20%20%23%20Altair%3A%20each%20column%20of%20the%20summary%20goes%20to%20a%20channel%3B%20rest%20the%20pointer%20on%20a%20bar%20for%20its%20tooltip%0A%20%20%20%20alt.Chart(_summary).mark_bar().encode(%0A%20%20%20%20%20%20%20%20x%3Dalt.X(%22yield_t_ha%3AQ%22%2C%20title%3D%22Weighted%20mean%20rice%20yield%20(t%2Fha)%22)%2C%0A%20%20%20%20%20%20%20%20y%3Dalt.Y(%22province%3AN%22%2C%20title%3DNone)%2C%0A%20%20%20%20%20%20%20%20color%3Dalt.Color(%22irrigated%3AN%22%2C%20title%3D%22Irrigated%22)%2C%0A%20%20%20%20%20%20%20%20yOffset%3D%22irrigated%3AN%22%2C%20%20%23%20the%20bars%20of%20one%20province%20side%20by%20side%2C%20not%20stacked%0A%20%20%20%20%20%20%20%20tooltip%3D%5B%22province%22%2C%20%22irrigated%22%2C%20alt.Tooltip(%22yield_t_ha%3AQ%22%2C%20format%3D%22.2f%22%2C%20title%3D%22t%2Fha%22)%5D%2C%0A%20%20%20%20).properties(width%3D420)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20In%20every%20province%20irrigated%20plots%20yield%20more%2C%20by%20about%201%20to%201.7%20t%2Fha.%20Three%20charts%2C%20one%20answer%3B%20what%20differs%20is%20the%20work%20and%20what%20you%20can%20do%20with%20the%20result.%0A%0A%20%20%20%20%7C%20%7C%20matplotlib%20%7C%20seaborn%20%7C%20Altair%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20**you%20describe**%20%7C%20each%20mark%3A%20where%20every%20bar%20goes%2C%20its%20height%2C%20its%20label%20%7C%20the%20data%20and%20which%20column%20goes%20where%3B%20it%20computes%20the%20statistic%20%7C%20which%20column%20goes%20to%20which%20**channel**%20(x%2C%20y%2C%20color%2C%20tooltip)%20%7C%0A%20%20%20%20%7C%20**fits%20when**%20%7C%20you%20need%20**full%20control**%3A%20a%20figure%20for%20a%20report%2C%20a%20layout%20no%20template%20has%2C%20exact%20sizes%20and%20fonts%3B%20seaborn%20and%20pandas'%20%60.plot%60%20are%20built%20on%20it%20%7C%20a%20quick%20**statistical%20look**%20at%20raw%20rows%3A%20distributions%2C%20means%20by%20group%2C%20regression%20lines%2C%20with%20good%20defaults%20%7C%20the%20chart%20lives%20on%20a%20**screen**%3A%20tooltips%2C%20zoom%2C%20selections%2C%20and%20marimo%20apps%20%7C%0A%20%20%20%20%7C%20**watch%20for**%20%7C%20groups%20and%20legends%20take%20more%20code%20(the%20offsets%20above)%20%7C%20its%20defaults%3A%20a%20plain%20mean%2C%20bootstrap%20error%20bars%2C%20missing%20values%20dropped%20(a%20trend's%20gap%20bridged)%20%7C%20its%20own%20statistics%20are%20plain%20(unweighted)%3B%20it%20puts%20the%20data%20inside%20the%20chart%2C%20so%20outside%20marimo%20it%20refuses%20tables%20over%205%2C000%20rows%20(marimo%20allows%2020%2C000)%3A%20summarize%20first%20%7C%0A%20%20%20%20%7C%20**saves%20as**%20%7C%20%60fig.savefig(%22chart.png%22%2C%20dpi%3D200)%60%20%7C%20the%20same%3A%20a%20seaborn%20chart%20is%20a%20matplotlib%20figure%20(%60ax.figure.savefig(...)%60)%20%7C%20%60chart.save(%22chart.html%22)%60%2C%20or%20%60chart.save(%22chart.png%22)%60%20%7C%0A%0A%20%20%20%20pandas'%20own%20%60.plot%60%20(%60summary.plot.barh()%60)%20is%20matplotlib%20underneath%3A%20handy%20for%20a%20quick%20look%2C%20with%20matplotlib's%20control%20when%20you%20need%20it.%0A%0A%20%20%20%20No%20library%20is%20right%20for%20everything.%20A%20useful%20habit%3A%20compute%20in%20pandas%2C%20look%20in%20seaborn%2C%20publish%20in%20matplotlib%2C%20share%20on%20screen%20in%20Altair.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%207.4.%20Irrigated%20or%20not%3A%20grouped%20bars%20from%20the%20summary%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20summary%20table%20is%20the%20hand-off%20point%20between%20computing%20and%20drawing.%20Draw%20it%20in%20the%20library%20that%20fits%20you%3B%20the%20check%20reads%20the%20bars%2C%20so%20any%20of%20the%20three%20passes.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20summary%20table%20like%20Exercise%207.3's%20(%60province%60%2C%20%60irrigated%60%2C%20%60yield_t_ha%60)%20%E2%86%92%20a%20**bar%20chart**%20with%20a%20pair%20of%20bars%20per%20province%2C%20one%20per%20value%20of%20%60irrigated%60%20(the%20check%20needs%20to%20tell%20the%20two%20apart%3A%20a%20legend%20in%20seaborn%2C%20%60label%3D%60%20in%20matplotlib%2C%20%60color%3D%60%20in%20Altair)%2C%20a%20value%20axis%20that%20**starts%20at%20zero**%20and%20whose%20title%20contains%20**%60t%2Fha%60**.%20A%20province%20with%20only%20one%20kind%20of%20plot%20gets%20one%20bar%3A%20no%20bar%20of%20zero%20for%20the%20missing%20kind.%0A%0A%20%20%20%20Tools%3A%20any%20of%20the%20three%20charts%20above.%20In%20seaborn%2C%20%60sns.barplot(summary%2C%20x%3D%22yield_t_ha%22%2C%20y%3D%22province%22%2C%20hue%3D%22irrigated%22%2C%20ax%3Dax)%60%3A%20with%20one%20row%20per%20bar%20there%20is%20nothing%20to%20average%2C%20so%20%60errorbar%3DNone%60%20adds%20nothing%20but%20keeps%20it%20clear.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20irrigation_bars(summary%3A%20pd.DataFrame)%3A%0A%20%20%20%20%22%22%22Each%20province's%20weighted%20mean%20yield%2C%20irrigated%20and%20not%2C%20as%20a%20pair%20of%20bars%20from%20zero.%22%22%22%0A%20%20%20%20%23%20Exercise%207.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20fig%2C%20ax%20%3D%20plt.subplots()%0A%20%20%20%20%23%20Step%202%3A%20one%20bar%20per%20row%20of%20the%20summary%3A%20province%20on%20one%20axis%2C%20yield_t_ha%20the%20length%2C%20irrigated%20as%20the%20color%0A%20%20%20%20%23%20Step%203%3A%20title%20the%20value%20axis%20with%20the%20unit%20(t%2Fha)%2C%20and%20return%20ax%20(or%20your%20Altair%20chart)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%207.4%22)%0A%0A%0A%40app.function%0Adef%20summary_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20hand-made%20summary%3A%20three%20provinces%2C%20one%20of%20them%20with%20rain-fed%20plots%20only%20(Exercise%207.4).%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22Iloilo%22%2C%20%22Iloilo%22%2C%20%22Isabela%22%2C%20%22Isabela%22%2C%20%22Bukidnon%22%5D%2C%0A%20%20%20%20%20%20%20%20%22irrigated%22%3A%20%5B%22No%22%2C%20%22Yes%22%2C%20%22No%22%2C%20%22Yes%22%2C%20%22No%22%5D%2C%0A%20%20%20%20%20%20%20%20%22yield_t_ha%22%3A%20%5B3.25%2C%204.5%2C%203.75%2C%205.0%2C%203.5%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%207.4%22%2C%20irrigation_bars%2C%0A%20%20%20%20%20%20%20%20%5B(%22a%20hand-made%20summary%3A%20Bukidnon%20with%20rain-fed%20plots%20only%22%2C%20(summary_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20same%2C%20Isabela's%20irrigated%20plots%20left%20out%22%2C%20(summary_fixture().drop(index%3D3)%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dbars_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20check%20reads%20each%20bar's%20province%2C%20its%20value%20of%20irrigated%20and%20its%20length%2C%20whether%20the%20value%20axis%20starts%20at%20zero%2C%20and%20whether%20its%20title%20contains%20t%2Fha.%20Bukidnon%20has%20one%20bar.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(rice)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20chart%20of%20the%20survey's%20rice%20plots%20(it%20uses%20your%20weighted_means%20from%20Exercise%207.3).%0A%20%20%20%20_summary%20%3D%20attempt(weighted_means%2C%20rice%2C%20%5B%22province%22%2C%20%22irrigated%22%5D)%0A%20%20%20%20mo.stop(_summary%20is%20NOT_YET%2C%20waiting_for(%22Exercise%207.3%22))%0A%20%20%20%20show_chart(irrigation_bars%2C%20_summary)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%207.4%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Every%20row's%20%60series%60%20is%20%60None%60%20%7C%20The%20two%20kinds%20of%20bar%20are%20not%20named%3A%20no%20%60hue%60%2C%20no%20%60label%3D%60%2C%20or%20seaborn's%20legend%20removed%20(%60legend%3DFalse%60).%20%7C%20%60hue%3D%22irrigated%22%60%20(seaborn)%2C%20%60label%3D%60%20(matplotlib)%2C%20%60color%3D%22irrigated%3AN%22%60%20(Altair)%20%7C%0A%20%20%20%20%7C%20A%20bar%20with%20%60'y'%3A%200%60%20for%20Bukidnon%20%7C%20The%20missing%20combination%20was%20filled%20with%200%20(%60fillna(0)%60%20after%20a%20pivot).%20%7C%20leave%20it%20missing%3A%20no%20bar%20%7C%0A%20%20%20%20%7C%20%60'value%20axis%20names%20the%20unit'%3A%20False%60%20%7C%20The%20title%20is%20the%20column%20name%2C%20%60yield_t_ha%60.%20%7C%20%60ax.set_xlabel(...)%60%20or%20%60alt.X(...%2C%20title%3D...)%60%20%7C%0A%20%20%20%20%7C%20The%20bars%20of%20a%20province%20are%20stacked%20into%20one%20%7C%20Altair%20stacks%20bars%20that%20share%20a%20place.%20%7C%20%60yOffset%3D%22irrigated%3AN%22%60%20(or%20%60xOffset%60%20for%20upright%20bars)%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'weight'*%20%7C%20The%20function%20computes%20means%20again%3B%20the%20summary%20has%20them%20already.%20%7C%20draw%20%60yield_t_ha%60%20as%20it%20is%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203%20%C2%B7%20A%20small%20interactive%20marimo%20app%20from%20a%20DataFrame%0A%0A%20%20%20%20The%20extension%20office%20wants%20to%20look%20at%20yields%20by%20province%20and%20crop%20**themselves**%2C%20for%20whichever%20province%20a%20farmer%20asks%20about%2C%20without%20editing%20code.%20A%20chart%20per%20province%20and%20crop%20would%20be%20dozens%20of%20charts.%20An%20**app**%20is%20one%20table%20and%20one%20chart%2C%20with%20controls%20that%20choose%20what%20they%20show.%0A%0A%20%20%20%20%23%23%23%20How%20marimo%20makes%20an%20app%0A%0A%20%20%20%20You%20have%20used%20marimo's%20controls%20since%20module%201%3A%20the%20slider%20in%20section%201%20is%20one.%20A%20control%20(%60mo.ui.slider%60%2C%20%60mo.ui.dropdown%60%2C%20%60mo.ui.multiselect%60%2C%20%E2%80%A6)%20is%20a%20Python%20object.%20You%20create%20it%20in%20one%20cell%20and%20show%20it%20as%20that%20cell's%20output%3B%20another%20cell%20reads%20its%20%60.value%60.%20When%20someone%20moves%20the%20control%2C%20marimo%20**runs%20again%20every%20cell%20that%20reads%20its%20value**%2C%20and%20every%20cell%20that%20uses%20what%20those%20cells%20define.%20That%20is%20the%20whole%20mechanism%3A%20no%20buttons%20to%20wire%2C%20no%20callbacks.%20marimo%20knows%20which%20cells%20read%20what%20because%20it%20reads%20the%20names%20each%20cell%20uses.%0A%0A%20%20%20%20Three%20rules%20follow%20from%20it%3A%0A%0A%20%20%20%20%7C%20rule%20%7C%20why%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20assign%20the%20control%20to%20a%20variable%2C%20and%20show%20it%20as%20a%20cell's%20output%20(alone%2C%20or%20inside%20%60mo.hstack%60)%20%7C%20marimo%20tracks%20a%20control%20by%20its%20variable%2C%20and%20the%20page%20shows%20it%20where%20it%20is%20output%20%7C%0A%20%20%20%20%7C%20read%20%60.value%60%20in%20**another**%20cell%2C%20not%20the%20one%20that%20creates%20it%20%7C%20running%20the%20creating%20cell%20again%20would%20make%20a%20new%20control%2C%20back%20at%20its%20starting%20value%20%7C%0A%20%20%20%20%7C%20put%20the%20work%20in%20functions%20%7C%20a%20function%20of%20a%20table%20is%20easy%20to%20test%2C%20and%20the%20app%20is%20only%20controls%2C%20one%20call%20and%20an%20output%20%7C%0A%0A%20%20%20%20So%20the%20app%20needs%20two%20functions%3A%20one%20that%20**filters**%20the%20plots%20by%20the%20controls'%20values%2C%20and%20one%20that%20**draws**%20a%20chart%20of%20whatever%20rows%20it%20is%20given.%20Those%20are%20the%20two%20exercises%3B%20the%20app%20itself%20comes%20after%20them.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%207.5.%20The%20app's%20filter%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20every%20view%20the%20app%20shows%20comes%20from%20this%20one%20function%2C%20so%20it%20must%20be%20right%20for%20every%20choice%20a%20user%20can%20make%2C%20including%20the%20odd%20ones%3A%20every%20province%2C%20or%20no%20crop%20at%20all.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20table%2C%20a%20province's%20name%20(or%20%60ALL_PROVINCES%60%2C%20the%20text%20%60%22All%20provinces%22%60)%20and%20a%20**list**%20of%20crops%20%E2%86%92%20the%20rows%20of%20that%20province%20(of%20every%20province%2C%20for%20%60ALL_PROVINCES%60)%20whose%20crop%20is%20in%20the%20list%2C%20in%20their%20original%20order.%20An%20empty%20list%20of%20crops%20gives%20no%20rows.%0A%0A%20%20%20%20%7C%20province%20%7C%20crops%20%7C%20rows%20kept%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%22Iloilo%22%60%20%7C%20%60%5B%22Rice%22%5D%60%20%7C%20Iloilo's%20rice%20plots%20%7C%0A%20%20%20%20%7C%20%60%22Iloilo%22%60%20%7C%20%60%5B%22Rice%22%2C%20%22Corn%22%5D%60%20%7C%20Iloilo's%20rice%20and%20corn%20plots%20%7C%0A%20%20%20%20%7C%20%60ALL_PROVINCES%60%20%7C%20%60%5B%22Corn%22%5D%60%20%7C%20every%20corn%20plot%20%7C%0A%20%20%20%20%7C%20%60%22Iloilo%22%60%20%7C%20%60%5B%5D%60%20%7C%20none%20(the%20table's%20columns%2C%20no%20rows)%20%7C%0A%0A%20%20%20%20Tools%3A%20%60table%5B%22crop%22%5D.isin(crops)%60%20is%20%60True%60%20for%20each%20row%20whose%20crop%20is%20in%20the%20list%3B%20combine%20conditions%20with%20%60%26%60%2C%20each%20in%20brackets%20(%5Bmodule%203%5D(%2F%3Ffile%3D20_dataframes.py)%2C%20section%203)%3B%20an%20%60if%60%20adds%20the%20province's%20condition%20only%20when%20a%20province%20is%20chosen.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20filter_plots(table%3A%20pd.DataFrame%2C%20province%3A%20str%2C%20crops%3A%20list%5Bstr%5D)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20plots%20of%20one%20province%20(or%20of%20every%20province%2C%20for%20ALL_PROVINCES)%20whose%20crop%20is%20in%20the%20list.%22%22%22%0A%20%20%20%20%23%20Exercise%207.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20a%20condition%20that%20is%20True%20for%20each%20row%20whose%20crop%20is%20in%20%60crops%60%20(.isin)%0A%20%20%20%20%23%20Step%202%3A%20if%20a%20province%20is%20chosen%20(not%20ALL_PROVINCES)%2C%20combine%20it%20with%20the%20province's%20condition%20using%20%26%0A%20%20%20%20%23%20Step%203%3A%20return%20the%20rows%20where%20the%20condition%20is%20True%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%207.5%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_plots%20%3D%20plots_fixture()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%207.5%22%2C%20filter_plots%2C%0A%20%20%20%20%20%20%20%20%5B(%22Iloilo%2C%20rice%22%2C%20(_plots%2C%20%22Iloilo%22%2C%20%5B%22Rice%22%5D))%2C%20(%22Iloilo%2C%20rice%20and%20corn%22%2C%20(_plots%2C%20%22Iloilo%22%2C%20%5B%22Rice%22%2C%20%22Corn%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20(%22all%20provinces%2C%20corn%22%2C%20(_plots%2C%20ALL_PROVINCES%2C%20%5B%22Corn%22%5D))%2C%20(%22Iloilo%2C%20no%20crop%20chosen%22%2C%20(_plots%2C%20%22Iloilo%22%2C%20%5B%5D))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22choices%22%2C%20summarize%3Dlambda%20table%3A%20table.reset_index(drop%3DTrue)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22All%20provinces%20keeps%20every%20province's%20rows%3B%20an%20empty%20list%20of%20crops%20keeps%20none%2C%20with%20the%20table's%20columns.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plots)%3A%0A%20%20%20%20%23%20Try%20it%3A%20change%20the%20province%20and%20the%20crops%2C%20and%20see%20how%20many%20rows%20your%20filter%20keeps.%0A%20%20%20%20course.try_it(filter_plots%2C%20plots%2C%20%22Bukidnon%22%2C%20%5B%22Banana%22%2C%20%22Corn%22%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%207.5%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*ValueError%3A%20('Lengths%20must%20match%20to%20compare'%2C%20...)*%20%7C%20%60table%5B%22crop%22%5D%20%3D%3D%20crops%60%20compares%20a%20column%20with%20a%20list.%20%7C%20%60table%5B%22crop%22%5D.isin(crops)%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20The%20truth%20value%20of%20a%20Series%20is%20ambiguous*%20%7C%20%60and%60%20or%20%60or%60%20between%20two%20conditions.%20%7C%20%60%26%60%2C%20with%20each%20condition%20in%20brackets%20%7C%0A%20%20%20%20%7C%20%22all%20provinces%2C%20corn%22%20gives%20no%20rows%20%7C%20The%20province's%20condition%20is%20applied%20even%20for%20%60ALL_PROVINCES%60.%20%7C%20%60if%20province%20!%3D%20ALL_PROVINCES%3A%60%20%7C%0A%20%20%20%20%7C%20%22no%20crop%20chosen%22%20gives%20every%20row%20%7C%20An%20empty%20list%20was%20treated%20as%20%22no%20filter%22.%20%7C%20let%20%60isin(%5B%5D)%60%20do%20its%20work%3A%20no%20rows%20%7C%0A%20%20%20%20%7C%20Rows%20of%20other%20provinces%20appear%20%7C%20The%20province's%20condition%20is%20built%20but%20not%20used.%20%7C%20%60rows%20%3D%20rows%20%26%20(...)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%207.6.%20The%20app's%20chart%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20app%20calls%20this%20function%20with%20whatever%20rows%20the%20controls%20leave%3A%20one%20province%20or%20all%2C%20one%20crop%20or%20four.%20A%20function%20from%20table%20to%20chart%20draws%20every%20one%20of%20them%20the%20same%20honest%20way.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20of%20plots%20(any%20selection%20of%20them)%20%E2%86%92%20a%20**bar%20chart**%20with%20one%20bar%20per%20crop%20in%20the%20table%2C%20whose%20length%20is%20the%20crop's%20**weighted%20mean%20yield**%2C%20on%20a%20value%20axis%20that%20**starts%20at%20zero**%2C%20titled%20with%20**%60t%2Fha%60**.%0A%0A%20%20%20%20Tools%3A%20the%20weighted%20mean%20by%20crop%20(your%20%60weighted_means(table%2C%20%5B%22crop%22%5D)%60%20from%20Exercise%207.3%2C%20or%20the%20two%20sums%20again)%2C%20then%20any%20library.%20The%20app%20is%20a%20screen%2C%20so%20this%20is%20a%20natural%20place%20for%20**Altair**%3A%20%60alt.Chart(summary).mark_bar().encode(x%3Dalt.X(%22yield_t_ha%3AQ%22%2C%20title%3D%22...%22)%2C%20y%3Dalt.Y(%22crop%3AN%22%2C%20title%3DNone%2C%20sort%3D%22-x%22)%2C%20tooltip%3D%5B...%5D)%60%2C%20and%20%60return%60%20the%20chart.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20yield_chart(table%3A%20pd.DataFrame)%3A%0A%20%20%20%20%22%22%22The%20app's%20chart%3A%20each%20crop's%20weighted%20mean%20yield%20in%20the%20rows%20given%2C%20as%20bars%20from%20zero.%22%22%22%0A%20%20%20%20%23%20Exercise%207.6%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20each%20crop's%20weighted%20mean%20yield%3A%20a%20table%20with%20columns%20crop%20and%20yield_t_ha%0A%20%20%20%20%23%20Step%202%3A%20one%20bar%20per%20crop%20(Altair%3A%20alt.Chart(...).mark_bar().encode(...))%2C%20yield%20as%20the%20length%0A%20%20%20%20%23%20Step%203%3A%20title%20the%20value%20axis%20with%20the%20unit%20(t%2Fha)%2C%20and%20return%20the%20chart%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%207.6%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(plots)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%207.6%22%2C%20yield_chart%2C%0A%20%20%20%20%20%20%20%20%5B(%22eight%20hand-made%20plots%2C%20four%20crops%22%2C%20(plots_fixture()%2C))%2C%20(%22the%20survey's%20plots%20in%20Bukidnon%22%2C%20(plots%5Bplots%5B%22province%22%5D%20%3D%3D%20%22Bukidnon%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22rice%20plots%20only%2C%20every%20province%22%2C%20(plots%5Bplots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dbars_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20check%20reads%20each%20bar's%20crop%20and%20length%2C%20whether%20the%20value%20axis%20starts%20at%20zero%2C%20and%20whether%20its%20title%20contains%20t%2Fha.%20On%20the%20hand-made%20table%2C%20rice's%20weighted%20mean%20is%205.0%20and%20corn's%203.4091.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plots)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20chart%20for%20Isabela's%20plots.%0A%20%20%20%20show_chart(yield_chart%2C%20plots%5Bplots%5B%22province%22%5D%20%3D%3D%20%22Isabela%22%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%207.6%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20The%20bars%20are%20close%20but%20not%20right%20%7C%20A%20plain%20mean%20(Altair's%20%60mean(yield_t_ha)%60%20is%20one).%20%7C%20compute%20the%20weighted%20mean%20in%20pandas%2C%20then%20chart%20it%20%7C%0A%20%20%20%20%7C%20*...%20field%20%22crop%22%20not%20found*%20or%20an%20empty%20chart%20%7C%20The%20crops%20are%20the%20summary's%20index%2C%20not%20a%20column.%20%7C%20%60as_index%3DFalse%60%2C%20or%20%60.reset_index()%60%20%7C%0A%20%20%20%20%7C%20%60'value%20axis%20names%20the%20unit'%3A%20False%60%20%7C%20Altair%20titles%20the%20axis%20with%20the%20column%20name.%20%7C%20%60alt.X(%22yield_t_ha%3AQ%22%2C%20title%3D%22Weighted%20mean%20yield%20(t%2Fha)%22)%60%20%7C%0A%20%20%20%20%7C%20%60'value%20axis%20starts%20at%20zero'%3A%20False%60%20%7C%20A%20%60domain%60%20that%20starts%20above%20zero%2C%20or%20a%20log%20scale.%20%7C%20leave%20the%20scale%20as%20it%20is%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20chart_data%20reads%20...%20not%20a%20builtins.NoneType*%20%7C%20The%20chart%20is%20built%20but%20not%20returned.%20%7C%20%60return%20alt.Chart(...)...%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20app%0A%0A%20%20%20%20Two%20controls%2C%20your%20two%20functions%2C%20a%20table%20and%20a%20chart.%20Change%20the%20province%20or%20the%20crops%3A%20the%20cells%20below%20run%20again%20by%20themselves.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plots)%3A%0A%20%20%20%20province%20%3D%20mo.ui.dropdown(options%3D%5BALL_PROVINCES%2C%20*sorted(plots%5B%22province%22%5D.unique())%5D%2C%20value%3DALL_PROVINCES%2C%20label%3D%22Province%3A%22)%0A%20%20%20%20crops%20%3D%20mo.ui.multiselect(options%3Dsorted(plots%5B%22crop%22%5D.unique())%2C%20value%3Dsorted(plots%5B%22crop%22%5D.unique())%2C%20label%3D%22Crops%3A%22)%0A%20%20%20%20mo.hstack(%5Bprovince%2C%20crops%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20crops%2C%20province%0A%0A%0A%40app.cell%0Adef%20_(crops%2C%20plots%2C%20province)%3A%0A%20%20%20%20%23%20Reads%20province.value%20and%20crops.value%2C%20so%20it%20runs%20again%20whenever%20either%20control%20changes.%0A%20%20%20%20_selected%20%3D%20attempt(filter_plots%2C%20plots%2C%20province.value%2C%20crops.value)%0A%20%20%20%20mo.stop(_selected%20is%20NOT_YET%2C%20waiting_for(%22Exercise%207.5%22))%0A%20%20%20%20mo.stop(len(_selected)%20%3D%3D%200%2C%20mo.callout(mo.md(%22**No%20plots%20match.**%20Choose%20at%20least%20one%20crop.%22)%2C%20kind%3D%22warn%22))%0A%20%20%20%20_households%20%3D%20_selected.drop_duplicates(%22hh_id%22)%0A%20%20%20%20_chart%20%3D%20attempt(yield_chart%2C%20_selected)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Bprovince.value%7D**%2C%20%7B'%2C%20'.join(crops.value)%7D%3A%20**%7Blen(_selected)%3A%2C%7D%20plots**%20from%20**%7Blen(_households)%3A%2C%7D%20households**%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22which%20stand%20for%20about%20**%7B_households%5B'weight'%5D.sum()%3A%2C.0f%7D%20farm%20households**.%22)%2C%0A%20%20%20%20%20%20%20%20waiting_for(%22Exercise%207.6%22)%20if%20_chart%20is%20NOT_YET%20else%20_chart%2C%0A%20%20%20%20%20%20%20%20mo.ui.table(_selected%5B%5B%22hh_id%22%2C%20%22plot_no%22%2C%20%22province%22%2C%20%22crop%22%2C%20%22irrigated%22%2C%20%22area_ha%22%2C%20%22production_kg%22%2C%20%22yield_t_ha%22%5D%5D.round(%7B%22yield_t_ha%22%3A%202%7D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20selection%3DNone%2C%20page_size%3D8)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20What%20ran%2C%20and%20when%3A%0A%0A%20%20%20%20%7C%20cell%20%7C%20reads%20%7C%20runs%20again%20when%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20the%20controls%20%7C%20%60plots%60%20%7C%20the%20data%20changes%20(never%2C%20while%20you%20use%20the%20app)%20%7C%0A%20%20%20%20%7C%20the%20app%20%7C%20%60province.value%60%2C%20%60crops.value%60%2C%20%60plots%60%20%7C%20you%20change%20either%20control%2C%20or%20edit%20%60filter_plots%60%20or%20%60yield_chart%60%20%7C%0A%0A%20%20%20%20Editing%20a%20function%20counts%20too%3A%20change%20%60yield_chart%60%20and%20run%20its%20cell%2C%20and%20the%20app%20redraws%20with%20it.%20While%20an%20exercise%20is%20blank%2C%20the%20app%20waits%20for%20it%2C%20which%20is%20how%20every%20cell%20in%20this%20course%20that%20uses%20an%20exercise%20works.%0A%0A%20%20%20%20%23%23%23%20A%20chart%20you%20can%20click%0A%0A%20%20%20%20A%20chart%20can%20be%20a%20control%20too.%20%60mo.ui.altair_chart(chart)%60%20shows%20an%20Altair%20chart%20and%20makes%20its%20**selection**%20a%20value%3A%20click%20a%20bar%20(Shift%20%2B%20click%20for%20more%20than%20one)%2C%20and%20the%20cell%20that%20reads%20%60.value%60%20gets%20the%20rows%20of%20the%20chart's%20table%20behind%20the%20bars%20you%20chose.%20With%20nothing%20selected%2C%20%60.value%60%20is%20an%20empty%20table.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(rice)%3A%0A%20%20%20%20_by_province%20%3D%20attempt(weighted_means%2C%20rice%2C%20%5B%22province%22%5D)%0A%20%20%20%20mo.stop(_by_province%20is%20NOT_YET%2C%20waiting_for(%22Exercise%207.3%22))%0A%20%20%20%20clickable%20%3D%20mo.ui.altair_chart(%0A%20%20%20%20%20%20%20%20alt.Chart(_by_province).mark_bar().encode(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dalt.X(%22yield_t_ha%3AQ%22%2C%20title%3D%22Weighted%20mean%20rice%20yield%20(t%2Fha)%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dalt.Y(%22province%3AN%22%2C%20title%3DNone%2C%20sort%3D%22-x%22)%2C%0A%20%20%20%20%20%20%20%20).properties(width%3D420)%2C%0A%20%20%20%20%20%20%20%20chart_selection%3D%22point%22%2C%20legend_selection%3DFalse%2C%0A%20%20%20%20)%0A%20%20%20%20clickable%0A%20%20%20%20return%20(clickable%2C)%0A%0A%0A%40app.cell%0Adef%20_(clickable%2C%20rice)%3A%0A%20%20%20%20_chosen%20%3D%20clickable.value%20%20%23%20the%20rows%20behind%20the%20bars%20you%20clicked%3B%20nothing%20clicked%3A%20no%20rows%0A%20%20%20%20_names%20%3D%20_chosen%5B%22province%22%5D.tolist()%20if%20len(_chosen)%20else%20sorted(rice%5B%22province%22%5D.unique())%0A%20%20%20%20_rows%20%3D%20rice%5Brice%5B%22province%22%5D.isin(_names)%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(_rows)%3A%2C%7D%20rice%20plots**%20in%20%7B'%2C%20'.join(_names)%20if%20len(_chosen)%20else%20'every%20province%20(click%20a%20bar%20to%20choose)'%7D%3B%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B(_rows%5B'irrigated'%5D%20%3D%3D%20'Yes').mean()%3A.0%25%7D%20of%20them%20irrigated.%22)%2C%0A%20%20%20%20%20%20%20%20mo.ui.table(_rows%5B%5B%22hh_id%22%2C%20%22plot_no%22%2C%20%22province%22%2C%20%22irrigated%22%2C%20%22area_ha%22%2C%20%22yield_t_ha%22%5D%5D.round(%7B%22yield_t_ha%22%3A%202%7D)%2C%20selection%3DNone%2C%20page_size%3D5)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Sharing%20it%0A%0A%20%20%20%20%7C%20to%20share%20%7C%20do%20%7C%20what%20they%20get%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20**the%20app%2C%20running**%20%7C%20in%20a%20terminal%20in%20the%20course%20folder%3A%20%60uv%20run%20marimo%20run%2060_charts_and_sharing.py%60%20%7C%20the%20notebook%20as%20an%20app%20in%20a%20browser%20tab%3A%20outputs%20and%20controls%2C%20no%20code.%20It%20runs%20on%20your%20computer%3B%20a%20colleague%20with%20the%20course%20folder%20runs%20it%20the%20same%20way%20on%20theirs%20%7C%0A%20%20%20%20%7C%20**a%20snapshot**%20%7C%20%60uv%20run%20marimo%20export%20html%2060_charts_and_sharing.py%20-o%20charts.html%20--no-include-code%60%20%7C%20one%20HTML%20file%20anyone%20can%20open%2C%20with%20every%20output%20as%20it%20was.%20The%20controls%20show%2C%20but%20change%20nothing%3A%20no%20Python%20runs%20behind%20them%20%7C%0A%20%20%20%20%7C%20**a%20chart**%20%7C%20matplotlib%20or%20seaborn%3A%20%60ax.figure.savefig(%22yields.png%22%2C%20dpi%3D200%2C%20bbox_inches%3D%22tight%22)%60%3B%20Altair%3A%20%60chart.save(%22yields.png%22)%60%20or%20%60chart.save(%22yields.html%22)%60%20%7C%20a%20file%20for%20a%20report%20or%20a%20slide%3B%20Altair's%20HTML%20keeps%20the%20tooltips%20%7C%0A%0A%20%20%20%20Before%20you%20share%2C%20read%20your%20charts%20once%20more%20as%20a%20stranger%20would%3A%20does%20the%20title%20say%20what%20it%20shows%2C%20does%20every%20axis%20have%20its%20unit%2C%20do%20bars%20start%20at%20zero%2C%20is%20a%20missing%20month%20a%20gap%2C%20and%20does%20the%20reader%20know%20whether%20the%20numbers%20are%20weighted%3F%0A%0A%20%20%20%20**Next%3A**%20the%20stretch%20exercises%20in%20%6083_stretch_charts.py%60%20(small%20multiples%2C%20honest%20error%20bars%2C%20seasons%2C%20a%20trend%20line%2C%20tooltips)%2C%20and%20the%20capstone%2C%20where%20an%20app%20like%20this%20one%20can%20be%20your%20group's%20tool.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function(hide_code%3DTrue)%0Adef%20show_chart(function%2C%20*args)%3A%0A%20%20%20%20%22%22%22A%20Try%20it%20cell's%20chart%3A%20what%20your%20function%20drew%2C%20or%20why%20it%20could%20not%20draw%20it.%22%22%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20chart%20%3D%20function(*args)%0A%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(f%22%60%7Bfunction.__name__%7D%60%3A%20**not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%20NotImplementedError%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20except%20Exception%3A%0A%20%20%20%20%20%20%20%20return%20course.try_it(function%2C%20*args)%20%20%23%20runs%20it%20again%2C%20to%20show%20the%20error%20and%20what%20it%20usually%20means%0A%20%20%20%20if%20chart%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(f%22%60%7Bfunction.__name__%7D%60%20gave%20back%20%60None%60%3A%20end%20it%20with%20%60return%20ax%60%20(matplotlib%20or%20seaborn)%20or%20%60return%20chart%60%20(Altair).%22)%2C%20kind%3D%22warn%22)%0A%20%20%20%20return%20chart%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20How%20the%20checks%20read%20a%20chart%0A%0A%20%20%20%20The%20functions%20below%20are%20what%20this%20module's%20checks%20use.%20You%20do%20not%20need%20to%20read%20them.%20%60course.chart_data%60%20(from%20the%20course's%20helpers)%20reads%20what%20a%20chart%20draws%2C%20whichever%20library%20drew%20it%3B%20these%20add%20the%20honest-chart%20rules%3A%20where%20a%20bar%20chart's%20value%20axis%20starts%20and%20what%20its%20title%20says%2C%20and%20whether%20a%20line%20is%20drawn%20across%20a%20missing%20month.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function(hide_code%3DTrue)%0Adef%20close_chart(chart)%20-%3E%20None%3A%0A%20%20%20%20%22%22%22Close%20a%20matplotlib%20or%20seaborn%20chart%20once%20the%20check%20has%20read%20it%2C%20so%20the%20next%20one%20starts%20on%20a%20fresh%20figure.%22%22%22%0A%20%20%20%20figure%20%3D%20getattr(one_axes(chart)%2C%20%22figure%22%2C%20None)%0A%20%20%20%20if%20type(figure).__name__%20in%20(%22Figure%22%2C%20%22SubFigure%22)%3A%0A%20%20%20%20%20%20%20%20plt.close(figure)%0A%0A%0A%40app.function(hide_code%3DTrue)%0Adef%20bars_summary(chart%2C%20unit%3A%20str%20%3D%20%22t%2Fha%22)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22What%20a%20bar%20chart%20draws%2C%20and%20whether%20it%20is%20honest%3A%20its%20value%20axis%20starts%20at%20zero%2C%20and%20its%20title%20names%20the%20unit.%0A%20%20%20%20A%20bar%20or%20error%20bar%20of%20no%20value%20(a%20missing%20number)%20is%20not%20drawn%2C%20so%20it%20is%20left%20out.%22%22%22%0A%20%20%20%20drawn%20%3D%20%5Brow%20for%20row%20in%20course.chart_data(chart)%20if%20None%20not%20in%20(row%5B%22x%22%5D%2C%20row%5B%22y%22%5D)%5D%0A%20%20%20%20title%2C%20from_zero%20%3D%20value_axis(chart)%0A%20%20%20%20close_chart(chart)%0A%20%20%20%20return%20%7B%22value%20axis%20starts%20at%20zero%22%3A%20from_zero%2C%20%22value%20axis%20names%20the%20unit%22%3A%20unit%20in%20title%2C%20%22bars%22%3A%20drawn%7D%0A%0A%0A%40app.function(hide_code%3DTrue)%0Adef%20lines_summary(chart%2C%20unit%3A%20str%20%3D%20%22PHP%2Fkg%22)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22What%20a%20line%20chart%20draws%20(its%20points%20that%20have%20a%20value)%2C%20how%20many%20times%20a%20line%20bridges%20a%20gap%2C%20and%20whether%20the%0A%20%20%20%20value%20axis's%20title%20names%20the%20unit.%22%22%22%0A%20%20%20%20drawn%20%3D%20%5Brow%20for%20row%20in%20course.chart_data(chart%2C%20marks%3D(%22line%22%2C))%20if%20None%20not%20in%20(row%5B%22x%22%5D%2C%20row%5B%22y%22%5D)%5D%0A%20%20%20%20gaps%2C%20title%20%3D%20bridged(chart)%2C%20y_title(chart)%0A%20%20%20%20close_chart(chart)%0A%20%20%20%20return%20%7B%22value%20axis%20names%20the%20unit%22%3A%20unit%20in%20title%2C%20%22lines%20drawn%20across%20a%20gap%22%3A%20gaps%2C%20%22lines%22%3A%20drawn%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%227.3%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
43f77fcc17f5abd8a3da27c83554097d