-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(python): Add visualisation page to user guide (#13052)
Co-authored-by: Stijn de Gooijer <stijndegooijer@gmail.com>
- Loading branch information
1 parent
a7a1549
commit 8c8d4fb
Showing
6 changed files
with
199 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# --8<-- [start:dataframe] | ||
import polars as pl | ||
|
||
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] | ||
|
||
""" | ||
# --8<-- [start:hvplot_show_plot] | ||
df.plot.bar( | ||
x="species", | ||
y="petal_length", | ||
width=650, | ||
) | ||
# --8<-- [end:hvplot_show_plot] | ||
""" | ||
|
||
# --8<-- [start:hvplot_make_plot] | ||
import hvplot | ||
|
||
plot = df.plot.bar( | ||
x="species", | ||
y="petal_length", | ||
width=650, | ||
) | ||
hvplot.save(plot, "docs/images/hvplot_bar.html") | ||
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: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=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: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( | ||
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:plotly_show_plot] | ||
import plotly.express as px | ||
px.bar( | ||
df, | ||
x="species", | ||
y="petal_length", | ||
width=400, | ||
) | ||
# --8<-- [end:plotly_show_plot] | ||
""" | ||
|
||
# --8<-- [start:plotly_make_plot] | ||
import plotly.express as px | ||
|
||
fig = px.bar( | ||
df, | ||
x="species", | ||
y="petal_length", | ||
width=650, | ||
) | ||
fig.write_html("docs/images/plotly_bar.html", full_html=False, include_plotlyjs="cdn") | ||
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: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] | ||
import altair as alt | ||
|
||
chart = ( | ||
alt.Chart(df, width=600) | ||
.mark_bar() | ||
.encode( | ||
x="species:N", | ||
y="petal_length:Q", | ||
) | ||
) | ||
chart.save("docs/images/altair_bar.html") | ||
with open("docs/images/altair_bar.html", "r") as f: | ||
chart_html = f.read() | ||
print(f"{chart_html}") | ||
# --8<-- [end:altair_make_plot] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Visualization | ||
|
||
Data in a Polars `DataFrame` can be visualized using common visualization libraries. | ||
|
||
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" 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](https://hvplot.holoviz.org/). | ||
|
||
{{code_block('user-guide/misc/visualization','hvplot_show_plot',[])}} | ||
|
||
```python exec="on" session="user-guide/misc/visualization" | ||
--8<-- "python/user-guide/misc/visualization.py:hvplot_make_plot" | ||
``` | ||
|
||
## Matplotlib | ||
|
||
To create a bar chart we can pass columns of a `DataFrame` directly to Matplotlib as a `Series` for each column. Matplotlib does not have explicit support for Polars objects but Matplotlib can accept a Polars `Series` because it can convert each Series to a numpy array, which is zero-copy for numeric | ||
data without null values. | ||
|
||
{{code_block('user-guide/misc/visualization','matplotlib_show_plot',[])}} | ||
|
||
```python exec="on" session="user-guide/misc/visualization" | ||
--8<-- "python/user-guide/misc/visualization.py:matplotlib_make_plot" | ||
``` | ||
|
||
## Seaborn, Plotly & Altair | ||
|
||
[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 | ||
|
||
{{code_block('user-guide/misc/visualization','seaborn_show_plot',[])}} | ||
|
||
```python exec="on" session="user-guide/misc/visualization" | ||
--8<-- "python/user-guide/misc/visualization.py:seaborn_make_plot" | ||
``` | ||
|
||
### Plotly | ||
|
||
{{code_block('user-guide/misc/visualization','plotly_show_plot',[])}} | ||
|
||
```python exec="on" session="user-guide/misc/visualization" | ||
--8<-- "python/user-guide/misc/visualization.py:plotly_make_plot" | ||
``` | ||
|
||
### Altair | ||
|
||
{{code_block('user-guide/misc/visualization','altair_show_plot',[])}} | ||
|
||
```python exec="on" session="user-guide/misc/visualization" | ||
--8<-- "python/user-guide/misc/visualization.py:altair_make_plot" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters