From 8ff4f78fa81042e644bd77644ddb005e5eda649c Mon Sep 17 00:00:00 2001 From: Stijn de Gooijer Date: Mon, 29 Jan 2024 22:36:54 +0100 Subject: [PATCH] Some cleanup --- .../python/user-guide/misc/visualization.py | 117 ++++++++---------- docs/user-guide/misc/visualization.md | 12 +- 2 files changed, 53 insertions(+), 76 deletions(-) diff --git a/docs/src/python/user-guide/misc/visualization.py b/docs/src/python/user-guide/misc/visualization.py index f05e74c331178..f04288cb78129 100644 --- a/docs/src/python/user-guide/misc/visualization.py +++ b/docs/src/python/user-guide/misc/visualization.py @@ -1,29 +1,26 @@ -# --8<-- [start:setup] -import base64 - -# --8<-- [end:setup] # --8<-- [start:dataframe] - import polars as pl -url = "docs/data/iris.csv" +path = "docs/data/iris.csv" +df = pl.scan_csv(path).group_by("species").agg(pl.col("petal_length").mean()).collect() +print(df) +# --8<-- [end:dataframe] -iris_agg_df = ( - pl.scan_csv(url) - .group_by("species") - .agg( - pl.col("petal_length").mean(), - ) - .collect() +""" +# --8<-- [start:hvplot_show_plot] +df.plot.bar( + x="species", + y="petal_length", + width=650, ) - -# --8<-- [end:dataframe] +# --8<-- [end:hvplot_show_plot] +""" # --8<-- [start:hvplot_make_plot] import hvplot -plot = iris_agg_df.plot.bar( +plot = df.plot.bar( x="species", y="petal_length", width=650, @@ -32,41 +29,44 @@ with open("docs/images/hvplot_bar.html", "r") as f: chart_html = f.read() print(f"{chart_html}") - # --8<-- [end:hvplot_make_plot] + """ -# --8<-- [start:hvplot_show_plot] -iris_agg_df.plot.bar( - x="species", - y="petal_length", - width=650, -) -# --8<-- [end:hvplot_show_plot] -""" +# --8<-- [start:matplotlib_show_plot] +import matplotlib.pyplot as plt +plt.bar(x=df["species"], height=df["petal_length"]) +# --8<-- [end:matplotlib_show_plot] +""" # --8<-- [start:matplotlib_make_plot] +import base64 + import matplotlib.pyplot as plt -plt.bar(x=iris_agg_df["species"], height=iris_agg_df["petal_length"]) +plt.bar(x=df["species"], height=df["petal_length"]) plt.savefig("docs/images/matplotlib_bar.png") with open("docs/images/matplotlib_bar.png", "rb") as f: png = base64.b64encode(f.read()).decode() print(f'') - # --8<-- [end:matplotlib_make_plot] -""" -# --8<-- [start:matplotlib_show_plot] -import matplotlib.pyplot as plt -plt.bar(x=iris_agg_df["species"], height=iris_agg_df["petal_length"]) -# --8<-- [end:matplotlib_show_plot] """ +# --8<-- [start:seaborn_show_plot] +import seaborn as sns +sns.barplot( + df, + x="species", + y="petal_length", +) +# --8<-- [end:seaborn_show_plot] +""" + # --8<-- [start:seaborn_make_plot] import seaborn as sns sns.barplot( - iris_agg_df, + df, x="species", y="petal_length", ) @@ -74,24 +74,26 @@ with open("docs/images/seaborn_bar.png", "rb") as f: png = base64.b64encode(f.read()).decode() print(f'') - # --8<-- [end:seaborn_make_plot] + """ -# --8<-- [start:seaborn_show_plot] -import seaborn as sns -sns.barplot( - iris_agg_df, +# --8<-- [start:plotly_show_plot] +import plotly.express as px + +px.bar( + df, x="species", y="petal_length", + width=400, ) -# --8<-- [end:seaborn_show_plot] +# --8<-- [end:plotly_show_plot] """ # --8<-- [start:plotly_make_plot] import plotly.express as px fig = px.bar( - iris_agg_df, + df, x="species", y="petal_length", width=650, @@ -100,25 +102,21 @@ with open("docs/images/plotly_bar.html", "r") as f: chart_html = f.read() print(f"{chart_html}") - # --8<-- [end:plotly_make_plot] + """ -# --8<-- [start:plotly_show_plot] -import plotly.express as px -px.bar( - iris_agg_df, - x="species", - y="petal_length", - width=400, -) -# --8<-- [end:plotly_show_plot] +# --8<-- [start:altair_show_plot] +import altair as alt + +alt.Chart(df, width=700).mark_bar().encode(x="species:N", y="petal_length:Q") +# --8<-- [end:altair_show_plot] """ -# --8<-- [start:altair_make_plot] +# --8<-- [start:altair_make_plot] import altair as alt chart = ( - alt.Chart(iris_agg_df, width=600) + alt.Chart(df, width=600) .mark_bar() .encode( x="species:N", @@ -129,19 +127,4 @@ with open("docs/images/altair_bar.html", "r") as f: chart_html = f.read() print(f"{chart_html}") - # --8<-- [end:altair_make_plot] - -""" -# --8<-- [start:altair_show_plot] -import altair as alt - -alt.Chart(iris_agg_df, width=700) -.mark_bar() -.encode( - x="species:N", - y="petal_length:Q", -) - -# --8<-- [end:altair_show_plot] -""" diff --git a/docs/user-guide/misc/visualization.md b/docs/user-guide/misc/visualization.md index 4aab1acfea039..88dcd83a18a6f 100644 --- a/docs/user-guide/misc/visualization.md +++ b/docs/user-guide/misc/visualization.md @@ -2,23 +2,17 @@ Data in a Polars `DataFrame` can be visualized using common visualization libraries. -
-```python exec="on" result="text" session="user-guide/misc/visualization" ---8<-- "python/user-guide/misc/visualization.py:setup" -``` -
- We illustrate plotting capabilities using the Iris dataset. We scan a CSV and then do a group-by on the `species` column and get the mean of the `petal_length`. {{code_block('user-guide/misc/visualization','dataframe',[])}} -```python exec="on" session="user-guide/misc/visualization" +```python exec="on" result="text" session="user-guide/misc/visualization" --8<-- "python/user-guide/misc/visualization.py:dataframe" ``` ## Built-in plotting with hvPlot -Polars has a `plot` method to create interactive plots using hvPlot. +Polars has a `plot` method to create interactive plots using [hvPlot](https://hvplot.holoviz.org/). {{code_block('user-guide/misc/visualization','hvplot_show_plot',[])}} @@ -39,7 +33,7 @@ data without null values. ## Seaborn, Plotly & Altair -Seaborn, Plotly & Altair can accept a Polars `DataFrame` by leveraging the [dataframe interchange protocol](https://data-apis.org/dataframe-api/), which offers zero-copy-conversion where possible. +[Seaborn](https://seaborn.pydata.org/), [Plotly](https://plotly.com/) & [Altair](https://altair-viz.github.io/) can accept a Polars `DataFrame` by leveraging the [dataframe interchange protocol](https://data-apis.org/dataframe-api/), which offers zero-copy conversion where possible. ### Seaborn