Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Jan 29, 2024
1 parent 05a251d commit 8ff4f78
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 76 deletions.
117 changes: 50 additions & 67 deletions docs/src/python/user-guide/misc/visualization.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -32,66 +29,71 @@
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'<img src="data:image/png;base64, {png}"/>')

# --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",
)
plt.savefig("docs/images/seaborn_bar.png")
with open("docs/images/seaborn_bar.png", "rb") as f:
png = base64.b64encode(f.read()).decode()
print(f'<img src="data:image/png;base64, {png}"/>')

# --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,
Expand All @@ -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",
Expand All @@ -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]
"""
12 changes: 3 additions & 9 deletions docs/user-guide/misc/visualization.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@

Data in a Polars `DataFrame` can be visualized using common visualization libraries.

<div style="display:none">
```python exec="on" result="text" session="user-guide/misc/visualization"
--8<-- "python/user-guide/misc/visualization.py:setup"
```
</div>

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',[])}}

Expand All @@ -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

Expand Down

0 comments on commit 8ff4f78

Please sign in to comment.