Skip to content

Commit

Permalink
docs(python): Add a Plotnine example to the visualization docs (pol…
Browse files Browse the repository at this point in the history
  • Loading branch information
jrycw authored and anath2 committed Mar 5, 2025
1 parent f16e8c7 commit 252d1f1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/source/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pyarrow
graphviz
hvplot
matplotlib
plotnine
seaborn
plotly
numba >= 0.54; python_version < '3.13' # numba does not support Python 3.13
Expand Down
30 changes: 30 additions & 0 deletions docs/source/src/python/user-guide/misc/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,36 @@
print(f'<img src="data:image/png;base64, {png}"/>')
# --8<-- [end:matplotlib_make_plot]

"""
# --8<-- [start:plotnine_show_plot]
from plotnine import ggplot, aes, geom_point, labs
(
ggplot(df, mapping=aes(x="sepal_width", y="sepal_length", color="species"))
+ geom_point()
+ labs(title="Irises", x="Sepal Width", y="Sepal Length")
)
# --8<-- [end:plotnine_show_plot]
"""

# --8<-- [start:plotnine_make_plot]
import base64

from plotnine import ggplot, aes, geom_point, labs

fig_path = "docs/assets/images/plotnine.png"

(
ggplot(df, mapping=aes(x="sepal_width", y="sepal_length", color="species"))
+ geom_point()
+ labs(title="Irises", x="Sepal Width", y="Sepal Length")
).save(fig_path, dpi=300, verbose=False)

with open(fig_path, "rb") as f:
png = base64.b64encode(f.read()).decode()
print(f'<img src="data:image/png;base64, {png}"/>')
# --8<-- [end:plotnine_make_plot]

"""
# --8<-- [start:seaborn_show_plot]
import seaborn as sns
Expand Down
12 changes: 12 additions & 0 deletions docs/source/user-guide/misc/visualization.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ values so that it can be passed as an argument to `c`.
--8<-- "python/user-guide/misc/visualization.py:matplotlib_make_plot"
```

## Plotnine

[Plotnine](https://plotnine.org/) is a reimplementation of ggplot2 in Python, bringing the Grammar
of Graphics to Python users with an interface similar to its R counterpart. It supports Polars
`DataFrame` by internally converting it to a pandas `DataFrame`.

{{code_block('user-guide/misc/visualization','plotnine_show_plot',[])}}

```python exec="on" session="user-guide/misc/visualization"
--8<-- "python/user-guide/misc/visualization.py:plotnine_make_plot"
```

## Seaborn and Plotly

[Seaborn](https://seaborn.pydata.org/) and [Plotly](https://plotly.com/) can accept a Polars
Expand Down

0 comments on commit 252d1f1

Please sign in to comment.