Skip to content

Commit

Permalink
rename, better bar plot example, simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Aug 18, 2024
1 parent d5167f1 commit efed5c9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
6 changes: 3 additions & 3 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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 Plot
from polars.dataframe.plotting import DataFramePlot
from polars.datatypes import (
N_INFER_DEFAULT,
Boolean,
Expand Down Expand Up @@ -603,7 +603,7 @@ def _replace(self, column: str, new_column: Series) -> DataFrame:

@property
@unstable()
def plot(self) -> Plot:
def plot(self) -> DataFramePlot:
"""
Create a plot namespace.
Expand Down Expand Up @@ -675,7 +675,7 @@ def plot(self) -> Plot:
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 Plot(self)
return DataFramePlot(self)

@property
@unstable()
Expand Down
23 changes: 9 additions & 14 deletions py-polars/polars/dataframe/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
]


class Plot:
class DataFramePlot:
"""DataFrame.plot namespace."""

chart: alt.Chart
Expand Down Expand Up @@ -86,15 +86,16 @@ def bar(
Examples
--------
>>> 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"],
... "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="price", y="count()") # doctest: +SKIP
>>> df.plot.bar(
... x="day", y="value", color="day", column="group"
... ) # doctest: +SKIP
"""
encodings: Encodings = {}
if x is not None:
Expand All @@ -105,9 +106,7 @@ def bar(
encodings["color"] = color
if tooltip is not None:
encodings["tooltip"] = tooltip
return (
self.chart.mark_bar().encode(**{**encodings, **kwargs}).interactive() # type: ignore[arg-type]
)
return self.chart.mark_bar().encode(**encodings, **kwargs).interactive()

def line(
self,
Expand Down Expand Up @@ -173,11 +172,7 @@ def line(
encodings["order"] = order
if tooltip is not None:
encodings["tooltip"] = tooltip
return (
self.chart.mark_line()
.encode(**{**encodings, **kwargs}) # type: ignore[arg-type]
.interactive()
)
return self.chart.mark_line().encode(**encodings, **kwargs).interactive()

def point(
self,
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 @@ -36,7 +36,7 @@
]


class Plot:
class SeriesPlot:
"""Series.plot namespace."""

_accessor = "plot"
Expand Down
6 changes: 3 additions & 3 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
from polars.series.categorical import CatNameSpace
from polars.series.datetime import DateTimeNameSpace
from polars.series.list import ListNameSpace
from polars.series.plotting import Plot
from polars.series.plotting import SeriesPlot
from polars.series.string import StringNameSpace
from polars.series.struct import StructNameSpace
from polars.series.utils import expr_dispatch, get_ffi_func
Expand Down Expand Up @@ -7359,7 +7359,7 @@ def struct(self) -> StructNameSpace:

@property
@unstable()
def plot(self) -> Plot:
def plot(self) -> SeriesPlot:
"""
Create a plot namespace.
Expand Down Expand Up @@ -7404,7 +7404,7 @@ def plot(self) -> Plot:
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 Plot(self)
return SeriesPlot(self)


def _resolve_temporal_dtype(
Expand Down

0 comments on commit efed5c9

Please sign in to comment.