可視化
Polars の DataFrame 内のデータは、一般的な可視化ライブラリを使用して可視化することができます。
ここでは、Iris データセットを使用してプロット機能を示します。CSV をスキャンし、species カラムでグループ化を行い、petal_length の平均を取得します。
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)
shape: (3, 2)
┌────────────┬──────────────┐
│ species    ┆ petal_length │
│ ---        ┆ ---          │
│ str        ┆ f64          │
╞════════════╪══════════════╡
│ Virginica  ┆ 5.552        │
│ Setosa     ┆ 1.462        │
│ Versicolor ┆ 4.26         │
└────────────┴──────────────┘
hvPlot による組み込みのプロット
Polars には hvPlot を使用してインタラクティブなプロットを作成するための plot メソッドがあります。
df.plot.bar(
    x="species",
    y="petal_length",
    width=650,
)
Matplotlib
棒グラフを作成するには、DataFrame の各カラムを Matplotlib に Series として直接渡すことができます。Matplotlib は Polars オブジェクトを明示的にサポートしていませんが、Polars の Series を受け入れることができます。これは、null 値がないデータは Series をゼロコピーで numpy 配列に変換できるためです。
import matplotlib.pyplot as plt
plt.bar(x=df["species"], height=df["petal_length"])
Seaborn, Plotly & Altair
Seaborn、Plotly 、Altair は Dataframe 変換プロトコルを活用して Polars の DataFrame を受け入れることができます。これにより、可能な場合はゼロコピー変換が提供されます。
Seaborn
import seaborn as sns
sns.barplot(
    df,
    x="species",
    y="petal_length",
)
Plotly
import plotly.express as px
px.bar(
    df,
    x="species",
    y="petal_length",
    width=400,
)
Altair
import altair as alt
alt.Chart(df, width=700).mark_bar().encode(x="species:N", y="petal_length:Q")