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

expanding plotting backends. #20904

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
from polars._utils.wrap import wrap_expr, wrap_ldf, wrap_s
from polars.dataframe._html import NotebookFormatter
from polars.dataframe.group_by import DynamicGroupBy, GroupBy, RollingGroupBy
from polars.dataframe.plotting import DataFramePlot
from polars.dataframe.plotting.plotting import DataFramePlot
from polars.datatypes import (
N_INFER_DEFAULT,
Boolean,
Expand Down Expand Up @@ -611,7 +611,7 @@ def _replace(self, column: str, new_column: Series) -> DataFrame:

@property
@unstable()
def plot(self) -> DataFramePlot:
def plot(self, backend: str | None = None) -> DataFramePlot:
"""
Create a plot namespace.

Expand Down Expand Up @@ -704,7 +704,7 @@ def plot(self) -> DataFramePlot:
if not _ALTAIR_AVAILABLE or parse_version(altair.__version__) < (5, 4, 0):
msg = "altair>=5.4.0 is required for `.plot`"
raise ModuleUpgradeRequiredError(msg)
return DataFramePlot(self)
return DataFramePlot(self, backend)

@property
@unstable()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

import inspect
from typing import TYPE_CHECKING, Callable, Union

from typing import TYPE_CHECKING, Any, Callable, Union
import os
from polars.dependencies import altair as alt

if TYPE_CHECKING:
Expand Down Expand Up @@ -34,6 +34,38 @@
class DataFramePlot:
"""DataFrame.plot namespace."""

def __init__(self, df: DataFrame, backend: str | None = None) -> None:
self._df = df
# TODO: add config for backend
if backend is None and "POLARS_PLOTTING_BACKEND" in os.environ:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I guess it should track if its picking backend from parameter or env. That way we raise fromdf.plot(backend="plotly").alt.line(...) for the inconsistency.

backend = os.environ["POLARS_PLOTTING_BACKEND"]
elif backend is None:
backend = (
"altair" # TODO: change from default to detecting installed library
)

if backend == "altair":
self._backend = AltairPlot(df)

def bar(
self,
x: X | None = None,
y: Y | None = None,
color: Color | None = None,
/,
**kwargs: Any,
) -> Any:
return self._backend.bar(x, y, color, **kwargs)

def alt(self) -> AltairPlot:
# going through the extra class makes it so users can get the right static
# typed outputs or
return AltairPlot(self._df)


class AltairPlot:
"""DataFrame.plot namespace for altair."""

def __init__(self, df: DataFrame) -> None:
self._chart = alt.Chart(df)

Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/series/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from altair.typing import EncodeKwds

from polars.dataframe.plotting import Encodings
from polars.dataframe.plotting.plotting import Encodings

if sys.version_info >= (3, 11):
from typing import Unpack
Expand Down
Loading