绘图#

property Series.plot: SeriesPlot[source]#

创建一个绘图命名空间。

警告

此功能目前被视为不稳定。它可能会在任何时候更改,而不被视为破坏性更改。

在版本1.6.0中更改:在Polars的早期版本中,HvPlot是绘图后端。如果您想恢复以前的绘图功能,您只需要在脚本的顶部添加import hvplot.polars,并将df.plot替换为df.hvplot

Polars 本身不实现绘图逻辑,而是依赖于 Altair:

  • s.plot.hist(**kwargs)alt.Chart(s.to_frame()).mark_bar(tooltip=True).encode(x=alt.X(f'{s.name}:Q', bin=True), y='count()', **kwargs).interactive() 的简写

  • s.plot.kde(**kwargs)alt.Chart(s.to_frame()).transform_density(s.name, as_=[s.name, 'density']).mark_area(tooltip=True).encode(x=s.name, y='density:Q', **kwargs).interactive()的简写

  • 对于任何其他属性 attr, s.plot.attr(**kwargs)alt.Chart(s.to_frame().with_row_index()).mark_attr(tooltip=True).encode(x='index', y=s.name, **kwargs).interactive() 的简写

对于配置,我们建议阅读 图表配置。 例如,您可以:

  • 使用.properties(width=500, height=350, title="My amazing plot")更改宽度/高度/标题。

  • 使用 .configure_axisX(labelAngle=30) 更改 x 轴标签的旋转角度。

  • 使用 .configure_point(opacity=.5) 更改散点图中点的透明度。

示例

直方图:

>>> s = pl.Series([1, 4, 4, 6, 2, 4, 3, 5, 5, 7, 1])
>>> s.plot.hist()  

KDE 图:

>>> s.plot.kde()  

折线图:

>>> s.plot.line()