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%22Stretch%3A%20charts%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%20import%20calendar%0A%0A%20%20%20%20import%20altair%20as%20alt%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib.dates%20as%20mdates%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%0A%20%20%20%20from%20course%20import%20check_cases%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%20Stretch%3A%20charts%0A%0A%20%20%20%20For%20when%20you%20have%20finished%20module%207%20(%6060_charts_and_sharing.py%60).%20Less%20help%2C%20more%20to%20think%20about.%20Do%20them%20in%20any%20order%2C%20and%20draw%20each%20in%20the%20library%20you%20like%3A%20the%20checks%20read%20the%20data%20behind%20the%20chart%2C%20as%20in%20module%207%2C%20plus%20the%20honest-chart%20rule%20each%20exercise%20names.%0A%0A%20%20%20%20%7C%20%7C%20the%20chart%20%7C%20the%20rule%20it%20keeps%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20S3.1%20%7C%20small%20multiples%3A%20a%20panel%20per%20province%20%7C%20one%20y%20scale%20for%20every%20panel%20%7C%0A%20%20%20%20%7C%20S3.2%20%7C%20bars%20with%20error%20bars%20%7C%20intervals%20that%20mean%20what%20they%20say%2C%20or%20none%20%7C%0A%20%20%20%20%7C%20S3.3%20%7C%20a%20season%2C%20year%20over%20year%20%7C%20a%20missing%20month%20stays%20a%20gap%20%7C%0A%20%20%20%20%7C%20S3.4%20%7C%20a%20scatter%20with%20a%20trend%20line%20%7C%20the%20line%20is%20fitted%2C%20not%20drawn%20by%20eye%20%7C%0A%20%20%20%20%7C%20S3.5%20%7C%20an%20Altair%20chart%20with%20a%20tooltip%20%7C%20the%20exact%20number%20one%20pointer%20away%20%7C%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%20(as%20in%20module%207).%22%22%22%0A%20%20%20%20plots%20%3D%20course.plots()%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%20return%20plots%2C%20prices%2C%20rice%0A%0A%0A%40app.function%0Adef%20stretch_prices()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Two%20commodities%20in%20two%20provinces%2C%20June%202022%20to%20November%202023%2C%20with%20no%20rows%20for%20September%202023%20(S3.1%2C%20S3.3).%22%22%22%0A%20%20%20%20months%20%3D%20%5Bm%20for%20m%20in%20pd.period_range(%222022-06%22%2C%20%222023-11%22%2C%20freq%3D%22M%22)%20if%20str(m)%20!%3D%20%222023-09%22%5D%0A%20%20%20%20rows%20%3D%20%5B(month%2C%20province%2C%20commodity%2C%20round(base%20*%20level%20*%20(1%20%2B%20swing%20*%20np.cos(2%20*%20np.pi%20*%20(month.month%20-%207)%20%2F%2012))%2C%202))%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20province%2C%20level%20in%20((%22Iloilo%22%2C%201.0)%2C%20(%22Isabela%22%2C%200.9))%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20commodity%2C%20base%2C%20swing%20in%20((%22Tomato%22%2C%2032.0%2C%200.45)%2C%20(%22Palay%22%2C%2020.0%2C%200.08))%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20month%20in%20months%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.function%0Adef%20stretch_plots()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Seven%20hand-made%20rice%20plots%20in%20two%20provinces%2C%20one%20without%20fertilizer%20recorded%20(S3.2%2C%20S3.4%2C%20S3.5).%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-0004%22%2C%20%22HH-0004%22%2C%20%22HH-0005%22%5D%2C%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22Iloilo%22%2C%20%22Iloilo%22%2C%20%22Iloilo%22%2C%20%22Isabela%22%2C%20%22Isabela%22%2C%20%22Isabela%22%2C%20%22Isabela%22%5D%2C%0A%20%20%20%20%20%20%20%20%22crop%22%3A%20%5B%22Rice%22%5D%20*%207%2C%0A%20%20%20%20%20%20%20%20%22area_ha%22%3A%20%5B0.5%2C%201.0%2C%200.25%2C%202.0%2C%200.5%2C%201.0%2C%200.8%5D%2C%0A%20%20%20%20%20%20%20%20%22fertilizer_kg%22%3A%20%5B75.0%2C%20200.0%2C%20np.nan%2C%20200.0%2C%20125.0%2C%20180.0%2C%2096.0%5D%2C%0A%20%20%20%20%20%20%20%20%22yield_t_ha%22%3A%20%5B4.0%2C%205.0%2C%203.0%2C%203.5%2C%206.0%2C%204.5%2C%204.0%5D%2C%0A%20%20%20%20%20%20%20%20%22weight%22%3A%20%5B200.0%2C%20100.0%2C%20300.0%2C%20150.0%2C%20150.0%2C%20250.0%2C%2050.0%5D%2C%0A%20%20%20%20%7D)%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%20S3.1%20%C2%B7%20Small%20multiples%20by%20province%0A%0A%20%20%20%20Seven%20provinces'%20tomato%20prices%20in%20one%20chart%20are%20seven%20tangled%20lines.%20**Small%20multiples**%20give%20each%20province%20a%20small%20chart%20of%20its%20own%2C%20side%20by%20side%2C%20so%20the%20eye%20compares%20shapes.%20They%20compare%20honestly%20only%20if%20every%20panel%20shares%20**one%20y%20scale**%3A%20with%20a%20scale%20of%20its%20own%2C%20a%20province%20whose%20price%20barely%20moved%20fills%20its%20panel%20as%20fully%20as%20one%20whose%20price%20doubled.%0A%0A%20%20%20%20Write%20%60province_panels(prices%2C%20commodity)%60%3A%20one%20panel%20per%20province%2C%20each%20with%20the%20commodity's%20farmgate%20price%20by%20month%20as%20a%20line%2C%20every%20panel%20on%20the%20**same%20y%20scale**%2C%20and%20**September%202023%20a%20gap**%20in%20every%20line.%20Return%20the%20whole%20figure%3A%0A%0A%20%20%20%20-%20matplotlib%3A%20%60fig%2C%20axes%20%3D%20plt.subplots(2%2C%204%2C%20sharey%3DTrue)%60%2C%20one%20province%20per%20%60ax%60%20(titled%20with%20its%20name)%2C%20an%20unused%20panel%20removed%20(%60ax.remove()%60)%2C%20and%20%60return%20fig%60%3B%0A%20%20%20%20-%20seaborn%3A%20%60sns.relplot(...%2C%20col%3D%22province%22%2C%20col_wrap%3D4%2C%20kind%3D%22line%22)%60%20returns%20a%20grid%20whose%20panels%20share%20their%20scale%3B%20titles%20like%20%60province%20%3D%20Iloilo%60%20are%20fine.%20It%20drops%20missing%20values%2C%20so%20draw%20each%20unbroken%20stretch%20as%20its%20own%20line%20(%60units%3D%60%2C%20with%20%60estimator%3DNone%60)%2C%20as%20module%207%20section%202%20shows%3B%0A%20%20%20%20-%20Altair%3A%20%60.encode(...%2C%20facet%3Dalt.Facet(%22province%3AN%22%2C%20columns%3D4))%60%3B%20a%20missing%20price%20breaks%20the%20line.%0A%0A%20%20%20%20The%20check%20reads%20each%20panel's%20points%2C%20counts%20lines%20drawn%20across%20a%20missing%20month%2C%20and%20asks%20whether%20the%20panels%20share%20one%20y%20scale.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20province_panels(prices%3A%20pd.DataFrame%2C%20commodity%3A%20str)%3A%0A%20%20%20%20%22%22%22A%20small%20chart%20per%20province%20of%20one%20commodity's%20price%20by%20month%2C%20on%20one%20shared%20y%20scale%2C%20the%20missing%20month%20a%20gap.%22%22%22%0A%20%20%20%20raise%20NotImplementedError(%22Stretch%20S3.1%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(prices)%3A%0A%20%20%20%20check_cases(%22Stretch%20S3.1%22%2C%20province_panels%2C%20%5B%0A%20%20%20%20%20%20%20%20(%22two%20provinces'%20tomato%20prices%2C%20no%20rows%20for%20September%202023%22%2C%20(stretch_prices()%2C%20%22Tomato%22))%2C%0A%20%20%20%20%20%20%20%20(%22the%20farmgate%20prices%20of%20tomato%22%2C%20(prices%2C%20%22Tomato%22))%2C%20(%22the%20farmgate%20prices%20of%20palay%22%2C%20(prices%2C%20%22Palay%22))%2C%0A%20%20%20%20%5D%2C%20what%3D%22tables%22%2C%20summarize%3Dpanels_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22One%20panel%20per%20province%2C%20titled%20with%20its%20name%3B%20one%20y%20scale%20for%20all%20(sharey%3DTrue%20in%20matplotlib)%3B%20no%20line%20across%20September%202023.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(prices)%3A%0A%20%20%20%20%23%20Try%20it%3A%20try%20%22Eggplant%22%2C%20%22Palay%22%20or%20%22Yellow%20corn%22%20too.%0A%20%20%20%20show_chart(province_panels%2C%20prices%2C%20%22Tomato%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%20Stretch%20S3.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'one%20y%20scale'%3A%20False%60%20%7C%20Each%20panel%20scaled%20itself.%20%7C%20%60sharey%3DTrue%60%3B%20in%20Altair%2C%20no%20%60resolve_scale(y%3D%22independent%22)%60%20%7C%0A%20%20%20%20%7C%20%60'lines%20drawn%20across%20a%20gap'%3A%207%60%20%7C%20September%202023%20has%20no%20row%3B%20or%20seaborn%20dropped%20its%20missing%20price.%20%7C%20%60reindex(pd.period_range(...))%60%3B%20in%20seaborn%2C%20%60units%3D%60%20and%20%60estimator%3DNone%60%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20chart_data%20reads%20a%20figure%20with%20one%20chart*%20%7C%20An%20%60ax%60%20or%20the%20array%20of%20axes%20was%20returned.%20%7C%20%60return%20fig%60%20(or%20the%20seaborn%20grid%2C%20or%20the%20Altair%20chart)%20%7C%0A%20%20%20%20%7C%20Panels%20named%20%60''%60%20%7C%20The%20panels%20have%20no%20titles.%20%7C%20%60ax.set_title(province)%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%20S3.2%20%C2%B7%20Honest%20error%20bars%2C%20or%20none%0A%0A%20%20%20%20An%20error%20bar%20says%20%22the%20true%20value%20is%20probably%20in%20here%22.%20seaborn's%20default%20bars%20answer%20that%20for%20a%20simple%20random%20sample%20of%20plots%3B%20this%20survey%20sampled%20households%2C%20with%20weights.%20So%20either%20draw%20intervals%20computed%20for%20the%20weighted%20mean%2C%20or%20draw%20none.%20Here%20is%20an%20approximate%2095%25%20interval%20for%20a%20weighted%20mean%20%24%5Cbar%20y%24%2C%20from%20yields%20%24y%24%20and%20weights%20%24w%24%3A%0A%0A%20%20%20%20%24%24%5Cbar%20y%20%3D%20%5Cfrac%7B%5Csum%20w%20y%7D%7B%5Csum%20w%7D%20%5Cqquad%20se%20%3D%20%5Cfrac%7B%5Csqrt%7B%5Csum%20w%5E2%20(y%20-%20%5Cbar%20y)%5E2%7D%7D%7B%5Csum%20w%7D%20%5Cqquad%20%5Ctext%7Binterval%3A%20%7D%20%5Cbar%20y%20%5Cpm%201.96%20%5C%2C%20se%24%24%0A%0A%20%20%20%20It%20treats%20each%20plot%20as%20drawn%20on%20its%20own.%20The%20survey%20drew%20households%2C%20and%20a%20household's%20plots%20are%20alike%2C%20so%20the%20survey's%20true%20interval%20is%20somewhat%20wider%3A%20say%20%22approximate%22%20on%20the%20chart.%0A%0A%20%20%20%20Write%20%60interval_bars(table)%60%3A%20for%20a%20table%20of%20plots%2C%20a%20bar%20per%20province%20(its%20weighted%20mean%20yield)%20with%20an%20**error%20bar%20from%20the%20interval's%20low%20end%20to%20its%20high%20end**%2C%20a%20value%20axis%20from%20zero%20titled%20with%20**%60t%2Fha%60**.%0A%0A%20%20%20%20-%20matplotlib%3A%20%60ax.barh(...)%60%2C%20then%20%60ax.errorbar(means%2C%20provinces%2C%20xerr%3D%5Bmeans%20-%20low%2C%20high%20-%20means%5D%2C%20fmt%3D%22none%22)%60%20(%60xerr%60%20takes%20the%20distances%20below%20and%20above)%3B%0A%20%20%20%20-%20seaborn%3A%20%60sns.barplot(summary%2C%20...%2C%20errorbar%3DNone%2C%20ax%3Dax)%60%2C%20then%20the%20same%20%60ax.errorbar%60%3A%20seaborn's%20own%20%60errorbar%3D%60%20cannot%20draw%20intervals%20you%20computed%3B%0A%20%20%20%20-%20Altair%3A%20a%20layer%20of%20%60mark_bar()%60%20and%20%60mark_errorbar()%60%20with%20%60x%3D%22low%3AQ%22%2C%20x2%3D%22high%3AQ%22%60.%0A%0A%20%20%20%20The%20check%20reads%20the%20bars%2C%20each%20error%20bar's%20two%20ends%20(to%203%20decimals)%2C%20and%20the%20value%20axis.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20interval_bars(table%3A%20pd.DataFrame)%3A%0A%20%20%20%20%22%22%22Each%20province's%20weighted%20mean%20yield%20as%20a%20bar%2C%20with%20an%20approximate%2095%25%20interval%20as%20its%20error%20bar.%22%22%22%0A%20%20%20%20raise%20NotImplementedError(%22Stretch%20S3.2%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(rice)%3A%0A%20%20%20%20check_cases(%22Stretch%20S3.2%22%2C%20interval_bars%2C%20%5B%0A%20%20%20%20%20%20%20%20(%22seven%20hand-made%20rice%20plots%20in%20two%20provinces%22%2C%20(stretch_plots()%2C))%2C%20(%22the%20survey's%20rice%20plots%22%2C%20(rice%2C))%2C%0A%20%20%20%20%5D%2C%20what%3D%22tables%22%2C%20summarize%3Dinterval_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22On%20the%20hand-made%20table%2C%20Iloilo's%20weighted%20mean%20is%203.667%20and%20its%20interval%20runs%20from%202.852%20to%204.482%3B%20Isabela's%20runs%20from%203.702%20to%205.465.%22)%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.%20Then%20compare%20seaborn's%20default%20error%20bars%3A%20sns.barplot(rice%2C%20x%3D%22yield_t_ha%22%2C%20y%3D%22province%22%2C%20weights%3D%22weight%22).%0A%20%20%20%20show_chart(interval_bars%2C%20rice)%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%20Stretch%20S3.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%20The%20error%20bars%20are%20close%20but%20not%20right%20%7C%20seaborn's%20own%20error%20bars%20(a%20bootstrap)%2C%20or%20a%20plain%20standard%20error.%20%7C%20compute%20%60se%60%20from%20the%20formula%2C%20with%20the%20weights%20%7C%0A%20%20%20%20%7C%20The%20error%20bars%20are%20twice%20too%20long%2C%20or%20end%20at%20the%20wrong%20place%20%7C%20%60xerr%60%20given%20the%20ends%2C%20not%20the%20distances%20to%20them.%20%7C%20%60xerr%3D%5Bmean%20-%20low%2C%20high%20-%20mean%5D%60%20%7C%0A%20%20%20%20%7C%20%60'error%20bars'%3A%20%5B%5D%60%20%7C%20The%20error%20bars%20are%20drawn%20another%20way%20(%60ax.hlines%60%2C%20%60ax.plot%60).%20%7C%20%60ax.errorbar%60%2C%20or%20Altair's%20%60mark_errorbar%60%20%2F%20%60mark_rule%60%20with%20%60x2%60%20%7C%0A%20%20%20%20%7C%20%60'value%20axis%20names%20the%20unit'%3A%20False%60%20%7C%20No%20axis%20title%2C%20or%20one%20without%20t%2Fha.%20%7C%20%60ax.set_xlabel(...)%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%20S3.3%20%C2%B7%20Seasons%3A%20price%20by%20month%20of%20the%20year%0A%0A%20%20%20%20Four%20years%20of%20tomato%20prices%20in%20one%20long%20line%20show%20a%20trend%3B%20laid%20**over%20each%20other**%2C%20one%20line%20per%20year%20with%20January%20to%20December%20along%20the%20x%20axis%2C%20they%20show%20the%20**season**%3A%20when%20in%20the%20year%20the%20price%20climbs%20and%20falls%2C%20and%20whether%20every%20year%20does%20it.%0A%0A%20%20%20%20Write%20%60seasonal_lines(prices%2C%20commodity)%60%3A%20the%20commodity's%20price%2C%20averaged%20over%20the%20provinces%20for%20each%20month%3B%20then%20one%20line%20per%20**year**%20(named%20by%20the%20year%2C%20as%20text%3A%20%60%222022%22%60)%2C%20x%20the%20**month%20of%20the%20year**%20as%20a%20number%20(1%20to%2012)%2C%20every%20month%20from%20the%20first%20to%20the%20last%20a%20point%2C%20and%20**September%202023%20a%20gap**%20in%202023's%20line.%20The%20value%20axis's%20title%20contains%20**%60PHP%2Fkg%60**.%0A%0A%20%20%20%20Tools%3A%20%60groupby(%22month%22)%5B%22price%22%5D.mean()%60%20and%20the%20%60reindex%60%20from%20module%207%3B%20on%20a%20period%20index%2C%20%60.year%60%20and%20%60.month%60%20give%20each%20month's%20year%20and%20month%20number.%20%60ax.set_xticks(range(1%2C%2013)%2C%20calendar.month_abbr%5B1%3A%5D)%60%20labels%20the%20months%20by%20name%20(the%20check%20reads%20the%20numbers%2C%20not%20the%20labels).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20seasonal_lines(prices%3A%20pd.DataFrame%2C%20commodity%3A%20str)%3A%0A%20%20%20%20%22%22%22One%20commodity's%20mean%20price%20by%20month%20of%20the%20year%2C%20one%20line%20per%20year%2C%20the%20missing%20month%20a%20gap.%22%22%22%0A%20%20%20%20raise%20NotImplementedError(%22Stretch%20S3.3%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(prices)%3A%0A%20%20%20%20check_cases(%22Stretch%20S3.3%22%2C%20seasonal_lines%2C%20%5B%0A%20%20%20%20%20%20%20%20(%22two%20provinces'%20tomato%20prices%2C%20June%202022%20to%20November%202023%22%2C%20(stretch_prices()%2C%20%22Tomato%22))%2C%0A%20%20%20%20%20%20%20%20(%22the%20farmgate%20prices%20of%20tomato%22%2C%20(prices%2C%20%22Tomato%22))%2C%20(%22the%20farmgate%20prices%20of%20yellow%20corn%22%2C%20(prices%2C%20%22Yellow%20corn%22))%2C%0A%20%20%20%20%5D%2C%20what%3D%22tables%22%2C%20summarize%3Dlines_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22One%20line%20per%20year%2C%20named%20%5C%222022%5C%22%2C%20%5C%222023%5C%22%2C%20%E2%80%A6%3B%20x%20is%20the%20month%20number%3B%202023's%20line%20breaks%20at%209.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(prices)%3A%0A%20%20%20%20%23%20Try%20it%3A%20when%20in%20the%20year%20is%20tomato%20dearest%3F%20Try%20%22Palay%22%20too%3A%20does%20it%20have%20a%20season%3F%0A%20%20%20%20show_chart(seasonal_lines%2C%20prices%2C%20%22Tomato%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%20Stretch%20S3.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%20%60'lines%20drawn%20across%20a%20gap'%3A%201%60%20%7C%20September%202023%20has%20no%20row%2C%20so%202023's%20line%20joins%20August%20to%20October.%20%7C%20%60reindex%60%20before%20turning%20the%20months%20around%20%7C%0A%20%20%20%20%7C%20Every%20row's%20%60series%60%20is%20%60None%60%20%7C%20The%20years'%20lines%20have%20no%20names.%20%7C%20%60label%3Dstr(year)%60%3B%20in%20seaborn%20%60hue%3D%22year%22%60%3B%20in%20Altair%20%60color%3D%22year%3AN%22%60%20%7C%0A%20%20%20%20%7C%20Many%20more%20lines%2C%20or%20x%20values%20like%20%60'2023-07-01'%60%20%7C%20The%20months%20were%20not%20turned%20into%20month%20numbers.%20%7C%20%60.month%60%20on%20the%20period%20index%20%7C%0A%20%20%20%20%7C%20Prices%20off%20by%20a%20little%20%7C%20The%20provinces%20were%20not%20averaged%2C%20or%20one%20province%20was%20picked.%20%7C%20%60groupby(%22month%22)%5B%22price%22%5D.mean()%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%20S3.4%20%C2%B7%20A%20trend%20line%3A%20fertilizer%20and%20yield%0A%0A%20%20%20%20Do%20rice%20plots%20that%20get%20more%20fertilizer%20per%20hectare%20yield%20more%3F%20A%20**scatter**%20shows%20every%20plot%20as%20a%20point%3B%20a%20**trend%20line**%20summarizes%20how%20yield%20changes%2C%20on%20average%2C%20as%20fertilizer%20grows.%20Fit%20it%2C%20don't%20draw%20it%20by%20eye%3A%20the%20**least-squares**%20line%20is%20the%20straight%20line%20that%20keeps%20the%20points'%20squared%20vertical%20distances%20to%20it%20as%20small%20as%20possible%2C%20and%20%60np.polyfit(x%2C%20y%2C%201)%60%20gives%20its%20slope%20and%20intercept.%0A%0A%20%20%20%20Write%20%60fertilizer_trend(table)%60%3A%20for%20a%20table%20of%20plots%2C%20keep%20those%20with%20fertilizer%20recorded%3B%20x%20is%20**fertilizer%20per%20hectare**%20(%60fertilizer_kg%20%2F%20area_ha%60)%2C%20y%20the%20**yield**%20(t%2Fha).%20Draw%20every%20plot%20as%20a%20point%2C%20and%20the%20least-squares%20line%20as%20a%20line%20from%20the%20smallest%20x%20to%20the%20largest.%0A%0A%20%20%20%20-%20matplotlib%3A%20%60ax.scatter(x%2C%20y)%60%2C%20then%20%60ax.plot(%5Bx_min%2C%20x_max%5D%2C%20%5B...%5D)%60%20from%20the%20slope%20and%20intercept%3B%0A%20%20%20%20-%20seaborn%3A%20%60sns.regplot(data%2C%20x%3D...%2C%20y%3D...%2C%20ax%3Dax)%60%20fits%20and%20draws%20it%20in%20one%20call%20(its%20shaded%20band%20is%20not%20checked)%3B%0A%20%20%20%20-%20Altair%3A%20Altair's%20own%20%60transform_regression%60%20draws%20in%20your%20browser%2C%20where%20the%20check%20cannot%20read%20it%2C%20so%20fit%20with%20%60np.polyfit%60%20and%20layer%20the%20points%20with%20a%20line%20drawn%20from%20a%20two-row%20table.%0A%0A%20%20%20%20The%20check%20reads%20the%20points%2C%20and%20the%20line's%20slope%20and%20intercept.%20Then%20read%20your%20chart%3A%20what%20does%20the%20line%20say%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20fertilizer_trend(table%3A%20pd.DataFrame)%3A%0A%20%20%20%20%22%22%22Each%20plot's%20yield%20against%20its%20fertilizer%20per%20hectare%2C%20with%20the%20least-squares%20line.%22%22%22%0A%20%20%20%20raise%20NotImplementedError(%22Stretch%20S3.4%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(plots%2C%20rice)%3A%0A%20%20%20%20check_cases(%22Stretch%20S3.4%22%2C%20fertilizer_trend%2C%20%5B%0A%20%20%20%20%20%20%20%20(%22seven%20hand-made%20rice%20plots%2C%20one%20without%20fertilizer%22%2C%20(stretch_plots()%2C))%2C%20(%22the%20survey's%20rice%20plots%22%2C%20(rice%2C))%2C%0A%20%20%20%20%20%20%20%20(%22the%20survey's%20corn%20plots%22%2C%20(plots%5Bplots%5B%22crop%22%5D%20%3D%3D%20%22Corn%22%5D%2C))%2C%0A%20%20%20%20%5D%2C%20what%3D%22tables%22%2C%20summarize%3Dtrend_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22On%20the%20hand-made%20table%20the%20six%20plots%20with%20fertilizer%20give%20a%20slope%20of%200.01586%20and%20an%20intercept%20of%201.857.%22)%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.%20Is%20the%20line%20steep%2C%20or%20flat%3F%0A%20%20%20%20show_chart(fertilizer_trend%2C%20rice)%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%20Stretch%20S3.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%20*LinAlgError*%20or%20%60nan%60%20from%20%60np.polyfit%60%20%7C%20Plots%20without%20fertilizer%20recorded%20are%20still%20in.%20%7C%20%60table%5Btable%5B%22fertilizer_kg%22%5D.notna()%5D%60%20%7C%0A%20%20%20%20%7C%20The%20slope%20is%20far%20off%20%7C%20x%20is%20total%20fertilizer%2C%20not%20per%20hectare.%20%7C%20%60fertilizer_kg%20%2F%20area_ha%60%20%7C%0A%20%20%20%20%7C%20%60'trend%20line%20(slope%2C%20intercept)'%3A%20None%60%20%7C%20No%20line%20is%20drawn%2C%20or%20Altair's%20%60transform_regression%60%20(the%20check%20can't%20run%20it).%20%7C%20a%20line%20from%20%60np.polyfit%60's%20slope%20and%20intercept%20%7C%0A%20%20%20%20%7C%20More%20points%20than%20plots%20%7C%20The%20line%20was%20drawn%20with%20markers.%20%7C%20%60ax.plot(...)%60%20without%20%60marker%3D%60%3B%20points%20with%20%60ax.scatter%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%20S3.5%20%C2%B7%20A%20tooltip%20that%20answers%20the%20next%20question%0A%0A%20%20%20%20A%20reader%20looking%20at%20a%20bar%20of%20yields%20asks%20two%20things%20next%3A%20*what%20exactly%20is%20it%3F*%20and%20*how%20many%20plots%20is%20it%20based%20on%3F*%20On%20a%20screen%2C%20a%20**tooltip**%20answers%20both%20when%20the%20pointer%20rests%20on%20the%20bar%2C%20without%20crowding%20the%20chart.%20Only%20Altair%20(of%20the%20three)%20makes%20charts%20with%20tooltips%3A%20matplotlib%20and%20seaborn%20draw%20pictures.%0A%0A%20%20%20%20Write%20%60tooltip_bars(table)%60%2C%20in%20Altair%3A%20for%20a%20table%20of%20plots%2C%20one%20bar%20per%20province%20with%20its%20**weighted%20mean%20yield**%2C%20a%20value%20axis%20from%20zero%20titled%20with%20**%60t%2Fha%60**%2C%20and%20a%20tooltip%20that%20shows%20the%20**province**%2C%20the%20**yield**%20(%60yield_t_ha%60%2C%20formatted%20to%20two%20decimals)%20and%20the%20number%20of%20**plots**%20in%20the%20survey%20behind%20it%20(a%20column%20%60plots%60).%0A%0A%20%20%20%20Tools%3A%20%60groupby(%22province%22).agg(...)%60%20with%20named%20aggregations%20for%20the%20sums%20and%20the%20count%20(module%204%2C%20section%201)%3B%20%60tooltip%3D%5Balt.Tooltip(%22province%3AN%22%2C%20title%3D%22Province%22)%2C%20alt.Tooltip(%22yield_t_ha%3AQ%22%2C%20format%3D%22.2f%22%2C%20title%3D%22...%22)%2C%20...%5D%60.%20The%20check%20reads%20the%20bars%2C%20the%20value%20axis%2C%20and%20which%20columns%20the%20tooltip%20shows.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20tooltip_bars(table%3A%20pd.DataFrame)%3A%0A%20%20%20%20%22%22%22Each%20province's%20weighted%20mean%20yield%20as%20an%20Altair%20bar%2C%20with%20a%20tooltip%20of%20the%20province%2C%20the%20yield%20and%20its%20plots.%22%22%22%0A%20%20%20%20raise%20NotImplementedError(%22Stretch%20S3.5%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(rice)%3A%0A%20%20%20%20check_cases(%22Stretch%20S3.5%22%2C%20tooltip_bars%2C%20%5B%0A%20%20%20%20%20%20%20%20(%22seven%20hand-made%20rice%20plots%20in%20two%20provinces%22%2C%20(stretch_plots()%2C))%2C%20(%22the%20survey's%20rice%20plots%22%2C%20(rice%2C))%2C%0A%20%20%20%20%5D%2C%20what%3D%22tables%22%2C%20summarize%3Dtooltip_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20tooltip%20shows%20three%20columns%3A%20plots%2C%20province%20and%20yield_t_ha.%20On%20the%20hand-made%20table%2C%20Iloilo's%20bar%20is%203.6667%20and%20Isabela's%204.5833.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(rice)%3A%0A%20%20%20%20%23%20Try%20it%3A%20rest%20the%20pointer%20on%20a%20bar.%0A%20%20%20%20show_chart(tooltip_bars%2C%20rice)%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%20Stretch%20S3.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%60'tooltip%20shows'%3A%20%5B%5D%60%20%7C%20A%20matplotlib%20or%20seaborn%20chart%20(a%20picture%3A%20no%20tooltip)%2C%20or%20no%20%60tooltip%3D%60%20in%20the%20encoding.%20%7C%20%60alt.Chart(...).encode(...%2C%20tooltip%3D%5B...%5D)%60%20%7C%0A%20%20%20%20%7C%20%60'plots'%60%20is%20missing%20from%20the%20tooltip%20%7C%20The%20summary%20has%20no%20count%20of%20plots%2C%20or%20it%20is%20named%20otherwise.%20%7C%20%60plots%3D(%22yield_t_ha%22%2C%20%22size%22)%60%20in%20%60agg%60%20%7C%0A%20%20%20%20%7C%20The%20bars%20are%20close%20but%20not%20right%20%7C%20Altair's%20%60mean(yield_t_ha)%60%2C%20a%20plain%20mean.%20%7C%20the%20weighted%20mean%2C%20computed%20in%20pandas%20%7C%0A%20%20%20%20%7C%20%60'value%20axis%20names%20the%20unit'%3A%20False%60%20%7C%20The%20axis%20is%20titled%20with%20the%20column's%20name.%20%7C%20%60alt.X(...%2C%20title%3D%22Weighted%20mean%20yield%20(t%2Fha)%22)%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.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%20fig%60%2C%20%60return%20ax%60%20or%20%60return%20chart%60.%22)%2C%20kind%3D%22warn%22)%0A%20%20%20%20return%20getattr(chart%2C%20%22figure%22%2C%20chart)%20if%20type(chart).__module__.startswith(%22seaborn%22)%20else%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%20these%20charts%0A%0A%20%20%20%20The%20functions%20below%20are%20what%20the%20checks%20use%3B%20you%20do%20not%20need%20to%20read%20them.%20%60course.chart_data%60%20reads%20what%20a%20chart%20draws%20in%20any%20library%3B%20these%20add%20each%20exercise's%20rule%3A%20one%20y%20scale%20across%20panels%2C%20the%20ends%20of%20error%20bars%2C%20lines%20drawn%20across%20a%20gap%2C%20a%20trend%20line's%20slope%2C%20a%20tooltip's%20columns.%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(chart%2C%20%22figure%22%2C%20None)%20if%20type(chart).__module__.startswith(%22seaborn%22)%20else%20getattr(one_axes(chart)%2C%20%22figure%22%2C%20chart)%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%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.function(hide_code%3DTrue)%0Adef%20panels_summary(chart)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22What%20a%20chart%20of%20small%20multiples%20draws%3A%20each%20panel's%20lines%20(named%20by%20the%20panel's%20title%20in%20matplotlib%20or%20seaborn%2C%0A%20%20%20%20by%20the%20facet's%20value%20in%20Altair)%2C%20how%20many%20times%20a%20line%20bridges%20a%20gap%2C%20and%20whether%20every%20panel%20shares%20one%20y%20scale.%22%22%22%0A%20%20%20%20rows%2C%20gaps%2C%20scales%20%3D%20%5B%5D%2C%200%2C%20set()%0A%20%20%20%20kind%20%3D%20f%22%7Btype(chart).__module__.split('.')%5B0%5D%7D.%7Btype(chart).__name__%7D%22%0A%20%20%20%20if%20kind.startswith(%22seaborn.%22)%3A%0A%20%20%20%20%20%20%20%20chart%2C%20kind%20%3D%20chart.figure%2C%20%22matplotlib.Figure%22%0A%20%20%20%20if%20kind%20%3D%3D%20%22matplotlib.Figure%22%3A%0A%20%20%20%20%20%20%20%20for%20ax%20in%20chart.axes%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20not%20ax.lines%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%20%20%20%20panel%20%3D%20ax.get_title(loc%3D%22left%22)%20or%20ax.get_title()%0A%20%20%20%20%20%20%20%20%20%20%20%20panel%20%3D%20panel.split(%22%20%3D%20%22)%5B-1%5D%20%20%23%20seaborn%20titles%20a%20panel%20%22province%20%3D%20Iloilo%22%0A%20%20%20%20%20%20%20%20%20%20%20%20rows%20%2B%3D%20%5B%7B**row%2C%20%22panel%22%3A%20panel%7D%20for%20row%20in%20course.chart_data(ax%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%20%20%20%20%20%20%20%20%20gaps%20%2B%3D%20bridged(ax)%0A%20%20%20%20%20%20%20%20%20%20%20%20scales.add(tuple(round(v%2C%206)%20for%20v%20in%20ax.get_ylim()))%0A%20%20%20%20%20%20%20%20shared%20%3D%20len(scales)%20%3D%3D%201%0A%20%20%20%20%20%20%20%20plt.close(chart)%0A%20%20%20%20elif%20isinstance(chart%2C%20(alt.Chart%2C%20alt.FacetChart))%3A%0A%20%20%20%20%20%20%20%20if%20isinstance(chart%2C%20alt.FacetChart)%3A%20%20%23%20chart.facet(...)%3A%20the%20panels'%20chart%20is%20chart.spec%0A%20%20%20%20%20%20%20%20%20%20%20%20facet%20%3D%20chart.to_dict()%5B%22facet%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20inner%20%3D%20chart.spec%0A%20%20%20%20%20%20%20%20%20%20%20%20plain%20%3D%20alt.Chart(chart.data%2C%20mark%3Dinner.mark%2C%20encoding%3Dinner.encoding%2C%20transform%3Dinner.transform)%0A%20%20%20%20%20%20%20%20else%3A%20%20%23%20.encode(facet%3D...)%2C%20column%3D%20or%20row%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20encoding%20%3D%20chart.to_dict()%5B%22encoding%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20facet%20%3D%20next(encoding%5Bc%5D%20for%20c%20in%20(%22facet%22%2C%20%22column%22%2C%20%22row%22)%20if%20c%20in%20encoding)%0A%20%20%20%20%20%20%20%20%20%20%20%20plain%20%3D%20chart.copy(deep%3DTrue)%0A%20%20%20%20%20%20%20%20%20%20%20%20plain.encoding.facet%20%3D%20plain.encoding.column%20%3D%20plain.encoding.row%20%3D%20alt.Undefined%0A%20%20%20%20%20%20%20%20field%20%3D%20facet.get(%22field%22)%20or%20(facet.get(%22column%22)%20or%20facet.get(%22row%22)%20or%20%7B%7D).get(%22field%22)%0A%20%20%20%20%20%20%20%20by_panel%20%3D%20plain.encode(color%3Dalt.Color(field%2C%20type%3D%22nominal%22))%20%20%23%20one%20line%20per%20panel%2C%20named%20by%20it%0A%20%20%20%20%20%20%20%20rows%20%3D%20%5B%7B**row%2C%20%22series%22%3A%20None%2C%20%22panel%22%3A%20row%5B%22series%22%5D%7D%20for%20row%20in%20course.chart_data(by_panel%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%20%20%20%20%20gaps%20%3D%20bridged(by_panel)%0A%20%20%20%20%20%20%20%20shared%20%3D%20chart.to_dict().get(%22resolve%22%2C%20%7B%7D).get(%22scale%22%2C%20%7B%7D).get(%22y%22%2C%20%22shared%22)%20!%3D%20%22independent%22%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20raise%20TypeError(f%22panels_summary%20reads%20a%20matplotlib%20figure%2C%20a%20seaborn%20grid%20or%20an%20Altair%20chart%2C%20not%20a%20%7Bkind%7D%22)%0A%20%20%20%20return%20%7B%22one%20y%20scale%22%3A%20shared%2C%20%22lines%20drawn%20across%20a%20gap%22%3A%20gaps%2C%20%22panels%22%3A%20sorted(rows%2C%20key%3Dlambda%20row%3A%20(str(row%5B%22panel%22%5D)%2C%20str(row%5B%22x%22%5D)))%7D%0A%0A%0A%40app.function(hide_code%3DTrue)%0Adef%20intervals(chart)%20-%3E%20list%5Blist%5Bfloat%5D%5D%3A%0A%20%20%20%20%22%22%22The%20error%20bars%20a%20chart%20draws%2C%20each%20as%20its%20two%20ends%20%5Blow%2C%20high%5D%20to%203%20places%2C%20sorted%3A%20from%20matplotlib's%20errorbar%2C%0A%20%20%20%20from%20seaborn's%20own%20error%20bars%2C%20or%20from%20an%20Altair%20errorbar%20or%20rule%20layer%20with%20x2%20(or%20y2).%22%22%22%0A%20%20%20%20found%20%3D%20%5B%5D%0A%20%20%20%20chart%20%3D%20one_axes(chart)%0A%20%20%20%20if%20isinstance(chart%2C%20alt.TopLevelMixin)%3A%0A%20%20%20%20%20%20%20%20for%20spec%2C%20data%20in%20altair_layers(chart)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20encoding%20%3D%20spec%5B%22encoding%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20start%2C%20end%20in%20((%22x%22%2C%20%22x2%22)%2C%20(%22y%22%2C%20%22y2%22))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20mark_of(spec)%20in%20(%22errorbar%22%2C%20%22rule%22)%20and%20end%20in%20encoding%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20found%20%2B%3D%20%5B(row%5Bcolumn_of(encoding%5Bstart%5D)%5D%2C%20row%5Bcolumn_of(encoding%5Bend%5D)%5D)%20for%20row%20in%20data.to_dict(%22records%22)%5D%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20for%20container%20in%20chart.containers%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20type(container).__name__%20%3D%3D%20%22ErrorbarContainer%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20collection%20in%20container.lines%5B2%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20(x0%2C%20y0)%2C%20(x1%2C%20y1)%20in%20collection.get_segments()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20found.append((y0%2C%20y1)%20if%20x0%20%3D%3D%20x1%20else%20(x0%2C%20x1))%0A%20%20%20%20%20%20%20%20for%20line%20in%20chart.lines%3A%20%20%23%20seaborn%20draws%20each%20of%20its%20error%20bars%20as%20a%20line%20of%20two%20points%20across%20a%20bar%0A%20%20%20%20%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20np.asarray(line.get_xdata(orig%3DFalse)%2C%20dtype%3Dfloat)%2C%20np.asarray(line.get_ydata(orig%3DFalse)%2C%20dtype%3Dfloat)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20line.get_transform()%20%3D%3D%20chart.transData%20and%20len(x)%20%3D%3D%202%20and%20line.get_linestyle()%20not%20in%20(%22None%22%2C%20%22none%22%2C%20%22%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20x%5B0%5D%20%3D%3D%20x%5B1%5D%20and%20np.isfinite(y).all()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20found.append((y%5B0%5D%2C%20y%5B1%5D))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif%20y%5B0%5D%20%3D%3D%20y%5B1%5D%20and%20np.isfinite(x).all()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20found.append((x%5B0%5D%2C%20x%5B1%5D))%0A%20%20%20%20return%20sorted(sorted(%5Bround(float(a)%2C%203)%2C%20round(float(b)%2C%203)%5D)%20for%20a%2C%20b%20in%20found)%0A%0A%0A%40app.function(hide_code%3DTrue)%0Adef%20interval_summary(chart%2C%20unit%3A%20str%20%3D%20%22t%2Fha%22)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22A%20bar%20chart%20with%20error%20bars%3A%20its%20bars%2C%20each%20error%20bar's%20two%20ends%2C%20and%20whether%20its%20value%20axis%20is%20honest.%22%22%22%0A%20%20%20%20bars%20%3D%20%5Brow%20for%20row%20in%20course.chart_data(chart%2C%20marks%3D(%22bar%22%2C))%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%20ends%20%3D%20intervals(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%22error%20bars%22%3A%20ends%2C%20%22bars%22%3A%20bars%7D%0A%0A%0A%40app.function(hide_code%3DTrue)%0Adef%20trend_summary(chart)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22A%20scatter%20with%20a%20straight%20trend%20line%3A%20the%20line's%20slope%20(4%20significant%20figures)%20and%20intercept%20(3%20places)%2C%20from%20its%0A%20%20%20%20two%20ends%2C%20and%20the%20points.%20seaborn's%20regplot%20draws%20its%20line%20as%20100%20points%20on%20it%2C%20the%20same%20line%20as%20one%20of%20two%20points.%22%22%22%0A%20%20%20%20drawn%20%3D%20course.chart_data(chart)%0A%20%20%20%20points%20%3D%20%5Brow%20for%20row%20in%20drawn%20if%20row%5B%22mark%22%5D%20%3D%3D%20%22point%22%20and%20None%20not%20in%20(row%5B%22x%22%5D%2C%20row%5B%22y%22%5D)%5D%0A%20%20%20%20line%20%3D%20sorted((row%5B%22x%22%5D%2C%20row%5B%22y%22%5D)%20for%20row%20in%20drawn%20if%20row%5B%22mark%22%5D%20%3D%3D%20%22line%22%20and%20row%5B%22y%22%5D%20is%20not%20None)%0A%20%20%20%20trend%20%3D%20None%0A%20%20%20%20if%20len(line)%20%3E%3D%202%20and%20line%5B-1%5D%5B0%5D%20!%3D%20line%5B0%5D%5B0%5D%3A%0A%20%20%20%20%20%20%20%20slope%20%3D%20(line%5B-1%5D%5B1%5D%20-%20line%5B0%5D%5B1%5D)%20%2F%20(line%5B-1%5D%5B0%5D%20-%20line%5B0%5D%5B0%5D)%0A%20%20%20%20%20%20%20%20trend%20%3D%20%5Bfloat(f%22%7Bslope%3A.4g%7D%22)%2C%20round(line%5B0%5D%5B1%5D%20-%20slope%20*%20line%5B0%5D%5B0%5D%2C%203)%5D%0A%20%20%20%20close_chart(chart)%0A%20%20%20%20return%20%7B%22trend%20line%20(slope%2C%20intercept)%22%3A%20trend%2C%20%22points%22%3A%20points%7D%0A%0A%0A%40app.function(hide_code%3DTrue)%0Adef%20tooltip_summary(chart%2C%20unit%3A%20str%20%3D%20%22t%2Fha%22)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22A%20bar%20chart's%20bars%2C%20its%20value%20axis%2C%20and%20the%20columns%20its%20tooltip%20shows%20when%20the%20pointer%20rests%20on%20a%20bar%20(Altair%0A%20%20%20%20only%3A%20a%20matplotlib%20or%20seaborn%20chart%20is%20a%20picture%2C%20with%20no%20tooltip).%22%22%22%0A%20%20%20%20fields%20%3D%20%5B%5D%0A%20%20%20%20if%20isinstance(chart%2C%20alt.TopLevelMixin)%3A%0A%20%20%20%20%20%20%20%20for%20spec%2C%20_%20in%20altair_layers(chart)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20tooltip%20%3D%20spec%5B%22encoding%22%5D.get(%22tooltip%22%2C%20%5B%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20fields%20%2B%3D%20%5Bitem%5B%22field%22%5D%20for%20item%20in%20(tooltip%20if%20isinstance(tooltip%2C%20list)%20else%20%5Btooltip%5D)%20if%20isinstance(item%2C%20dict)%20and%20%22field%22%20in%20item%5D%0A%20%20%20%20bars%20%3D%20%5Brow%20for%20row%20in%20course.chart_data(chart%2C%20marks%3D(%22bar%22%2C))%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%22tooltip%20shows%22%3A%20sorted(set(fields))%2C%20%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%20bars%7D%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
db8b14dffd023c8086b951d0edb017d3