Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Expand plotting docs #19719

Merged
merged 8 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions docs/source/src/python/user-guide/misc/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
y="sepal_length",
by="species",
width=650,
title="Irises",
xlabel='Sepal Width',
ylabel='Sepal Length',
)
# --8<-- [end:hvplot_show_plot]
"""
Expand All @@ -27,6 +30,9 @@
y="sepal_length",
by="species",
width=650,
title="Irises",
xlabel="Sepal Width",
ylabel="Sepal Length",
)
hvplot.save(plot, "docs/assets/images/hvplot_scatter.html", resources="cdn")
with open("docs/assets/images/hvplot_scatter.html", "r") as f:
Expand All @@ -44,6 +50,9 @@
y=df["sepal_length"],
c=df["species"].cast(pl.Categorical).to_physical(),
)
ax.set_title('Irises')
ax.set_xlabel('Sepal Width')
ax.set_ylabel('Sepal Length')
# --8<-- [end:matplotlib_show_plot]
"""

Expand All @@ -58,6 +67,9 @@
y=df["sepal_length"],
c=df["species"].cast(pl.Categorical).to_physical(),
)
ax.set_title("Irises")
ax.set_xlabel("Sepal Width")
ax.set_ylabel("Sepal Length")
fig.savefig("docs/assets/images/matplotlib_scatter.png")
with open("docs/assets/images/matplotlib_scatter.png", "rb") as f:
png = base64.b64encode(f.read()).decode()
Expand All @@ -67,12 +79,19 @@
"""
# --8<-- [start:seaborn_show_plot]
import seaborn as sns
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
sns.scatterplot(
df,
x="sepal_width",
y="sepal_length",
hue="species",
ax=ax,
)
ax.set_title('Irises')
ax.set_xlabel('Sepal Width')
ax.set_ylabel('Sepal Length')
# --8<-- [end:seaborn_show_plot]
"""

Expand All @@ -81,12 +100,16 @@
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax = sns.scatterplot(
sns.scatterplot(
df,
x="sepal_width",
y="sepal_length",
hue="species",
ax=ax,
)
ax.set_title("Irises")
ax.set_xlabel("Sepal Width")
ax.set_ylabel("Sepal Length")
fig.savefig("docs/assets/images/seaborn_scatter.png")
with open("docs/assets/images/seaborn_scatter.png", "rb") as f:
png = base64.b64encode(f.read()).decode()
Expand All @@ -103,6 +126,8 @@
y="sepal_length",
color="species",
width=650,
title="Irises",
labels={'sepal_width': 'Sepal Width', 'sepal_length': 'Sepal Length'}
)
# --8<-- [end:plotly_show_plot]
"""
Expand All @@ -116,6 +141,8 @@
y="sepal_length",
color="species",
width=650,
title="Irises",
labels={"sepal_width": "Sepal Width", "sepal_length": "Sepal Length"},
)
fig.write_html(
"docs/assets/images/plotly_scatter.html", full_html=False, include_plotlyjs="cdn"
Expand All @@ -127,15 +154,19 @@

"""
# --8<-- [start:altair_show_plot]
(
chart = (
df.plot.point(
x="sepal_length",
y="sepal_width",
color="species",
)
.properties(width=500)
.properties(width=500, title="Irises")
.configure_scale(zero=False)
.configure_axisX(tickMinStep=1)
)
chart.encoding.x.title = "Sepal Length"
chart.encoding.y.title = "Sepal Width"
chart
# --8<-- [end:altair_show_plot]
"""

Expand All @@ -146,9 +177,12 @@
y="sepal_width",
color="species",
)
.properties(width=500)
.properties(width=500, title="Irises")
.configure_scale(zero=False)
.configure_axisX(tickMinStep=1)
)
chart.encoding.x.title = "Sepal Length"
chart.encoding.y.title = "Sepal Width"
chart.save("docs/assets/images/altair_scatter.html")
with open("docs/assets/images/altair_scatter.html", "r") as f:
chart_html = f.read()
Expand Down
8 changes: 8 additions & 0 deletions docs/source/user-guide/misc/visualization.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ import altair as alt

and is only provided for convenience, and to signal that Altair is known to work well with Polars.

For configuration, we suggest reading
[Chart Configuration](https://altair-viz.github.io/altair-tutorial/notebooks/08-Configuration.html).
For example, you can:

- Change the width/height/title with `.properties(width=500, height=350, title="My amazing plot")`.
- Change the x-axis label rotation with `.configure_axisX(labelAngle=30)`.
- Change the opacity of the points in your scatter plot with `.configure_point(opacity=.5)`.

## hvPlot

If you import `hvplot.polars`, then it registers a `hvplot` method which you can use to create
Expand Down
16 changes: 16 additions & 0 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,18 @@ def plot(self) -> DataFramePlot:
is shorthand for
`alt.Chart(df).mark_attr(tooltip=True).encode(**kwargs).interactive()`

For configuration, we suggest reading
`Chart Configuration <https://altair-viz.github.io/altair-tutorial/notebooks/08-Configuration.html>`_.
For example, you can (assuming you've done ``import altair as alt``):

- Change the width/height/title with
``.properties(width=500, height=350, title="My amazing plot")``.
- Change the x-axis label rotation with ``.configure_axisX(labelAngle=30)``.
- Change the opacity of the points in your scatter plot with
``.configure_point(opacity=.5)``.
- Set the x-axis title by passing ``x=alt.X('length', title='Length')`` instead
of ``x='length'``.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This last bullet point feels incomplete:
Set the x-axis title by passing x=alt.X(...) instead of x=...... where? Where do I pass it?

(Also, you have the same list in visualization.md and in series.py but this fourth item is only present in this list. It looks like all three lists should look the same but I'm not sure, so I'm just flagging this here in case you missed it.)


Examples
--------
Scatter plot:
Expand Down Expand Up @@ -677,6 +689,10 @@ def plot(self) -> DataFramePlot:
>>> df.plot.bar(
... x="day", y="value", color="day", column="group"
... ) # doctest: +SKIP

Or, to make a stacked version of the plot above:

>>> df.plot.bar(x="day", y="value", color="group") # doctest: +SKIP
"""
if not _ALTAIR_AVAILABLE or parse_version(altair.__version__) < (5, 4, 0):
msg = "altair>=5.4.0 is required for `.plot`"
Expand Down
7 changes: 7 additions & 0 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -7507,6 +7507,13 @@ def plot(self) -> SeriesPlot:
is shorthand for
`alt.Chart(s.to_frame().with_row_index()).mark_attr(tooltip=True).encode(x='index', y=s.name, **kwargs).interactive()`

For configuration, we suggest reading
`Chart Configuration <https://altair-viz.github.io/altair-tutorial/notebooks/08-Configuration.html>`_.

- Change the width/height/title with ``.properties(width=500, height=350, title="My amazing plot")``.
- Change the x-axis label rotation with ``.configure_axisX(labelAngle=30)``.
- Change the opacity of the points in your scatter plot with ``.configure_point(opacity=.5)``.

Examples
--------
Histogram:
Expand Down