-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(python!): Use Altair in DataFrame.plot
- Loading branch information
1 parent
5134051
commit 872d784
Showing
5 changed files
with
155 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from typing import Any | ||
from altair import Undefined | ||
|
||
|
||
if TYPE_CHECKING: | ||
from polars import DataFrame | ||
import altair as alt | ||
|
||
|
||
class Plot: | ||
chart: alt.Chart | ||
|
||
def __init__(self, df: DataFrame) -> None: | ||
import altair as alt | ||
self.chart = alt.Chart(df) | ||
|
||
def line( | ||
self, | ||
x: str | Any, | ||
y: str | Any, | ||
color: str | Any, | ||
order: str | Any, | ||
tooltip: str | Any, | ||
) -> alt.Chart: | ||
""" | ||
Draw line plot. | ||
Polars does not implement plottinng logic itself but instead defers to Altair. | ||
`df.plot.line(*args, **kwargs)` is shorthand for `alt.Chart(df).mark_line().encode(*args, **kwargs)`, | ||
as is intended for convenience - for full customisatibility, use Altair directly. | ||
.. versionchanged:: 1.4.0 | ||
In prior versions of Polars, HvPlot was the plotting backend. If you would like to restore | ||
the previous plotting functionality, all you need to do add `import hvplot.polars` at the | ||
top of your script and replace `df.plot` with `df.hvplot`. | ||
Parameters | ||
---------- | ||
x | ||
Column with x-coordinates of lines. | ||
y | ||
Column with y-coordinates of lines. | ||
color | ||
Column to color lines by. | ||
order | ||
Column to use for order of data points in lines. | ||
tooltip | ||
Columns to show values of when hovering over points with pointer. | ||
*args, **kwargs | ||
Additional arguments and keyword arguments passed to Altair. | ||
""" | ||
encodings = {} | ||
if x is not Undefined: | ||
encodings['x'] = x | ||
if y is not Undefined: | ||
encodings['y'] = y | ||
if color is not Undefined: | ||
encodings['color'] = color | ||
if order is not Undefined: | ||
encodings['order'] = order | ||
if tooltip is not Undefined: | ||
encodings['tooltip'] = tooltip | ||
return self.chart.mark_point().encode(*args, **{**encodings, **kwargs}) | ||
|
||
def point( | ||
self, | ||
x: str | Any=Undefined, | ||
y: str | Any=Undefined, | ||
color: str | Any=Undefined, | ||
size: str | Any=Undefined, | ||
tooltip: str | Any=Undefined, | ||
*args, | ||
**kwargs | ||
) -> alt.Chart: | ||
""" | ||
Draw scatter plot. | ||
Polars does not implement plottinng logic itself but instead defers to Altair. | ||
`df.plot.point(*args, **kwargs)` is shorthand for `alt.Chart(df).mark_point().encode(*args, **kwargs)`, | ||
as is intended for convenience - for full customisatibility, use Altair directly. | ||
.. versionchanged:: 1.4.0 | ||
In prior versions of Polars, HvPlot was the plotting backend. If you would like to restore | ||
the previous plotting functionality, all you need to do add `import hvplot.polars` at the | ||
top of your script and replace `df.plot` with `df.hvplot`. | ||
Parameters | ||
---------- | ||
x | ||
Column with x-coordinates of points. | ||
y | ||
Column with y-coordinates of points. | ||
color | ||
Column to color points by. | ||
color | ||
Column which determines points' sizes. | ||
tooltip | ||
Columns to show values of when hovering over points with pointer. | ||
*args, **kwargs | ||
Additional arguments and keyword arguments passed to Altair. | ||
""" | ||
encodings = {} | ||
if x is not Undefined: | ||
encodings['x'] = x | ||
if y is not Undefined: | ||
encodings['y'] = y | ||
if color is not Undefined: | ||
encodings['color'] = color | ||
if size is not Undefined: | ||
encodings['size'] = size | ||
if tooltip is not Undefined: | ||
encodings['tooltip'] = tooltip | ||
return self.chart.mark_line().encode(*args, **{**encodings, **kwargs}) | ||
|
||
def __getattr__(self, attr: str, *args, **kwargs) -> alt.Chart: | ||
method = self.chart.getattr(f'mark_{attr}', None) | ||
if method is None: | ||
msg = "Altair has no method 'mark_{attr}'" | ||
raise AttributeError(msg) | ||
return method().encode(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters