绘图#
- property DataFrame.plot: DataFramePlot[source]#
创建一个绘图命名空间。
警告
此功能目前被视为不稳定。它可能会在任何时候更改,而不被视为破坏性更改。
在版本1.6.0中更改:在Polars的早期版本中,HvPlot是绘图后端。如果您想恢复以前的绘图功能,您只需要在脚本的顶部添加
import hvplot.polars,并将df.plot替换为df.hvplot。Polars 本身不实现绘图逻辑,而是依赖于 Altair:
df.plot.line(**kwargs)是alt.Chart(df).mark_line(tooltip=True).encode(**kwargs).interactive()的简写df.plot.point(**kwargs)是alt.Chart(df).mark_point(tooltip=True).encode(**kwargs).interactive()的简写形式(并且plot.scatter作为别名提供)df.plot.bar(**kwargs)是alt.Chart(df).mark_bar(tooltip=True).encode(**kwargs).interactive()的简写对于任何其他属性
attr,df.plot.attr(**kwargs)是alt.Chart(df).mark_attr(tooltip=True).encode(**kwargs).interactive()的简写
对于配置,我们建议阅读 图表配置。 例如,您可以:
使用
.properties(width=500, height=350, title="My amazing plot")更改宽度/高度/标题。使用
.configure_axisX(labelAngle=30)更改 x 轴标签的旋转角度。通过
.configure_point(opacity=.5)更改散点图中点的透明度。
示例
散点图:
>>> df = pl.DataFrame( ... { ... "length": [1, 4, 6], ... "width": [4, 5, 6], ... "species": ["setosa", "setosa", "versicolor"], ... } ... ) >>> df.plot.point(x="length", y="width", color="species")
通过使用
altair.X设置 x 轴标题:>>> import altair as alt >>> df.plot.point( ... x=alt.X("length", title="Length"), y="width", color="species" ... )
折线图:
>>> from datetime import date >>> df = pl.DataFrame( ... { ... "date": [date(2020, 1, 2), date(2020, 1, 3), date(2020, 1, 4)] * 2, ... "price": [1, 4, 6, 1, 5, 2], ... "stock": ["a", "a", "a", "b", "b", "b"], ... } ... ) >>> df.plot.line(x="date", y="price", color="stock")
条形图:
>>> df = pl.DataFrame( ... { ... "day": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] * 2, ... "group": ["a"] * 7 + ["b"] * 7, ... "value": [1, 3, 2, 4, 5, 6, 1, 1, 3, 2, 4, 5, 1, 2], ... } ... ) >>> df.plot.bar( ... x="day", y="value", color="day", column="group" ... )
或者,制作上述图表的堆叠版本:
>>> df.plot.bar(x="day", y="value", color="group")