import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Grammar%20of%20graphics%20in%20plotnine%0A%0A%20%20%20%20plotnine%20is%20a%20Python%20port%20of%20ggplot2.%20A%20plot%20is%20not%20a%20chart%20type%20chosen%20from%20a%20menu.%20It%20is%20a%20**mapping**%20from%20a%20table%20to%20visual%20properties%2C%20built%20by%20adding%20layers%20(Wilkinson%3B%20Wickham).%0A%0A%20%20%20%20The%20data%20are%20the%20Palmer%20penguins%3A%20344%20birds%2C%20three%20species%2C%20a%20rectangular%20table%20of%20measurements.%20The%20sentence%20we%20will%20draw%20is%20*bill%20length%20against%20bill%20depth*.%0A%0A%20%20%20%20%7C%20Layer%20%7C%20Role%20%7C%20Here%20%7C%0A%20%20%20%20%7C-------%7C------%7C------%7C%0A%20%20%20%20%7C%20data%20%7C%20the%20table%20%7C%20penguins%20%7C%0A%20%20%20%20%7C%20aes%20%7C%20map%20columns%20to%20%24x%24%2C%20%24y%24%2C%20colour%2C%20%E2%80%A6%20%7C%20length%2C%20depth%2C%20species%20%7C%0A%20%20%20%20%7C%20geom%20%7C%20the%20mark%20%7C%20points%20%7C%0A%20%20%20%20%7C%20stat%20%7C%20a%20computed%20layer%20%7C%20linear%20smooth%20%7C%0A%20%20%20%20%7C%20scale%20%7C%20values%20%24%5Cto%24%20ink%20%7C%20species%20colours%20%7C%0A%20%20%20%20%7C%20theme%20%7C%20non-data%20ink%20%7C%20the%20rest%20of%20the%20figure%20%7C%0A%0A%20%20%20%20First%20the%20two%20bill%20columns%20with%20no%20grouping.%20Then%20the%20same%20plot%20with%20%60color%3D%22species%22%60.%20The%20smoother%20inherits%20that%20mapping%2C%20so%20the%20fitted%20lines%20change%20without%20any%20extra%20wrangling.%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%20from%20plotnine%20import%20(%0A%20%20%20%20%20%20%20%20aes%2C%0A%20%20%20%20%20%20%20%20element_blank%2C%0A%20%20%20%20%20%20%20%20element_line%2C%0A%20%20%20%20%20%20%20%20element_rect%2C%0A%20%20%20%20%20%20%20%20element_text%2C%0A%20%20%20%20%20%20%20%20geom_point%2C%0A%20%20%20%20%20%20%20%20geom_smooth%2C%0A%20%20%20%20%20%20%20%20ggplot%2C%0A%20%20%20%20%20%20%20%20labs%2C%0A%20%20%20%20%20%20%20%20scale_color_manual%2C%0A%20%20%20%20%20%20%20%20theme%2C%0A%20%20%20%20%20%20%20%20theme_minimal%2C%0A%20%20%20%20)%0A%20%20%20%20from%20plotnine.data%20import%20penguins%0A%0A%20%20%20%20df%20%3D%20penguins.dropna(subset%3D%5B%22bill_length_mm%22%2C%20%22bill_depth_mm%22%5D).copy()%0A%0A%20%20%20%20species_colors%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22Adelie%22%3A%20%22%23D65A31%22%2C%0A%20%20%20%20%20%20%20%20%22Chinstrap%22%3A%20%22%236B4C9A%22%2C%0A%20%20%20%20%20%20%20%20%22Gentoo%22%3A%20%22%231B7A7A%22%2C%0A%20%20%20%20%7D%0A%0A%20%20%20%20def%20theme_note(legend%3DTrue)%3A%0A%20%20%20%20%20%20%20%20return%20theme_minimal(base_size%3D11)%20%2B%20theme(%0A%20%20%20%20%20%20%20%20%20%20%20%20figure_size%3D(7.2%2C%204.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20dpi%3D120%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20plot_background%3Delement_rect(fill%3D%22white%22%2C%20color%3D%22none%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20panel_background%3Delement_rect(fill%3D%22white%22%2C%20color%3D%22none%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20panel_grid_major%3Delement_line(color%3D%22%23ECECEC%22%2C%20size%3D0.4)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20panel_grid_minor%3Delement_blank()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20axis_ticks%3Delement_blank()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20axis_title%3Delement_text(size%3D11)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20plot_title%3Delement_text(size%3D13%2C%20face%3D%22bold%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20plot_subtitle%3Delement_text(size%3D10%2C%20color%3D%22%23444444%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20plot_caption%3Delement_text(size%3D8%2C%20color%3D%22%23666666%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20legend_title%3Delement_text(size%3D10)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20legend_position%3D%22right%22%20if%20legend%20else%20%22none%22%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20pooled_r%20%3D%20df%5B%22bill_length_mm%22%5D.corr(df%5B%22bill_depth_mm%22%5D)%0A%20%20%20%20within_r%20%3D%20%7B%0A%20%20%20%20%20%20%20%20name%3A%20g%5B%22bill_length_mm%22%5D.corr(g%5B%22bill_depth_mm%22%5D)%0A%20%20%20%20%20%20%20%20for%20name%2C%20g%20in%20df.groupby(%22species%22%2C%20observed%3DTrue)%0A%20%20%20%20%7D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22%7Blen(df)%7D%20penguins%20with%20both%20bill%20measurements%20%22%0A%20%20%20%20%20%20%20%20f%22(dropped%20%7Blen(penguins)%20-%20len(df)%7D%20rows)%22%0A%20%20%20%20)%0A%20%20%20%20print(f%22pooled%20r%20%3D%20%7Bpooled_r%3A.2f%7D%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22within%20species%3A%20%22%0A%20%20%20%20%20%20%20%20%2B%20%22%2C%20%22.join(f%22%7Bname%7D%20%7Br%3A.2f%7D%22%20for%20name%2C%20r%20in%20within_r.items())%0A%20%20%20%20)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20aes%2C%0A%20%20%20%20%20%20%20%20df%2C%0A%20%20%20%20%20%20%20%20geom_point%2C%0A%20%20%20%20%20%20%20%20geom_smooth%2C%0A%20%20%20%20%20%20%20%20ggplot%2C%0A%20%20%20%20%20%20%20%20labs%2C%0A%20%20%20%20%20%20%20%20scale_color_manual%2C%0A%20%20%20%20%20%20%20%20species_colors%2C%0A%20%20%20%20%20%20%20%20theme_note%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(aes%2C%20df%2C%20geom_point%2C%20geom_smooth%2C%20ggplot%2C%20labs%2C%20theme_note)%3A%0A%20%20%20%20(%0A%20%20%20%20%20%20%20%20ggplot(df%2C%20aes(%22bill_length_mm%22%2C%20%22bill_depth_mm%22))%0A%20%20%20%20%20%20%20%20%2B%20geom_point(color%3D%22%234A4A4A%22%2C%20alpha%3D0.55%2C%20size%3D2.2%2C%20stroke%3D0)%0A%20%20%20%20%20%20%20%20%2B%20geom_smooth(%0A%20%20%20%20%20%20%20%20%20%20%20%20method%3D%22lm%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20se%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20color%3D%22%231f4e79%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20fill%3D%22%231f4e79%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20alpha%3D0.15%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20size%3D0.9%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%2B%20labs(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%22Bill%20length%20(mm)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%22Bill%20depth%20(mm)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20title%3D%22Pooled%20across%20species%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20subtitle%3D%22The%20fitted%20line%20slopes%20down%3A%20longer%20bill%2C%20shallower%20depth.%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20caption%3D%22Palmer%20penguins%20%20%C2%B7%20%20Horst%2C%20Hill%2C%20and%20Gorman%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%2B%20theme_note(legend%3DFalse)%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20aes%2C%0A%20%20%20%20df%2C%0A%20%20%20%20geom_point%2C%0A%20%20%20%20geom_smooth%2C%0A%20%20%20%20ggplot%2C%0A%20%20%20%20labs%2C%0A%20%20%20%20scale_color_manual%2C%0A%20%20%20%20species_colors%2C%0A%20%20%20%20theme_note%2C%0A)%3A%0A%20%20%20%20(%0A%20%20%20%20%20%20%20%20ggplot(df%2C%20aes(%22bill_length_mm%22%2C%20%22bill_depth_mm%22%2C%20color%3D%22species%22))%0A%20%20%20%20%20%20%20%20%2B%20geom_point(alpha%3D0.7%2C%20size%3D2.2%2C%20stroke%3D0)%0A%20%20%20%20%20%20%20%20%2B%20geom_smooth(%0A%20%20%20%20%20%20%20%20%20%20%20%20method%3D%22lm%22%2C%20se%3DTrue%2C%20alpha%3D0.12%2C%20size%3D0.9%2C%20show_legend%3DFalse%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%2B%20scale_color_manual(values%3Dspecies_colors%2C%20name%3D%22Species%22)%0A%20%20%20%20%20%20%20%20%2B%20labs(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%22Bill%20length%20(mm)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%22Bill%20depth%20(mm)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20title%3D%22The%20same%20points%2C%20species%20as%20a%20mapping%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20subtitle%3D%22Each%20species%20slopes%20up.%20Gentoo%20sits%20to%20the%20right%20and%20down%2C%20and%20tilts%20the%20pooled%20line.%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20caption%3D%22Palmer%20penguins%20%20%C2%B7%20%20Horst%2C%20Hill%2C%20and%20Gorman%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%2B%20theme_note(legend%3DTrue)%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20pooled%20correlation%20is%20%24r%20%5Capprox%20-0.24%24.%20Within%20each%20species%20it%20is%20positive%20(Adelie%20%240.39%24%2C%20Chinstrap%20%240.65%24%2C%20Gentoo%20%240.64%24).%20Gentoo%20bills%20are%20long%20and%20shallow%2C%20so%20that%20cluster%20sits%20to%20the%20right%20and%20down%20and%20tilts%20the%20pooled%20line.%0A%0A%20%20%20%20Grouping%20is%20therefore%20a%20mapping%2C%20not%20a%20preprocessing%20step%3A%20%60aes(...%2C%20color%3D%22species%22)%60%20changes%20which%20rows%20the%20smoother%20sees%20together.%20%60facet_wrap(%22species%22)%60%20would%20split%20the%20same%20slopes%20into%20three%20panels%3B%20colour%20keeps%20them%20in%20one%20picture%20so%20the%20pooled%20cloud%20and%20the%20within-species%20lines%20are%20both%20visible.%0A%0A%20%20%20%20That%20is%20the%20grammar.%20Matplotlib%20can%20draw%20the%20same%20ink%3B%20plotnine%20is%20the%20vocabulary%20that%20makes%20the%20grouping%20explicit.%0A%20%20%20%20%22%22%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
4d2ee201cc6f96dea76f4f8f29340544