Skip to content
Merged
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: 6 additions & 0 deletions doc/changelog.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ title: Changelog
layer(geom=geom_point())
```

- `geom.DEFAULT_PARAMS` now implicitly includes `stat="identity"`, `position="identity`
and `na_rm=False`.

- `stat.DEFAULT_PARAMS` now implicitly includes `geom="blank"`, `position="identity`
and `na_rm=False`.

### Bug Fixes

- Fixed [](:class:`~plotnine.geom_smooth`) / [](:class:`~plotnine.stat_smooth`) when using a linear model via "lm" with weights for the model to do a weighted regression. This bug did not affect the formula API of the linear model. ({{< issue 1005 >}})
Expand Down
12 changes: 8 additions & 4 deletions plotnine/doctools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import itertools
import os
import re
import typing
from functools import lru_cache
from textwrap import dedent, indent
from typing import TYPE_CHECKING

if typing.TYPE_CHECKING:
if TYPE_CHECKING:
from typing import Any, Sequence, Type, TypeVar

from plotnine.geoms.geom import geom
Expand Down Expand Up @@ -429,13 +429,15 @@ def document_geom(geom: type[geom]) -> type[geom]:
It replaces `{usage}`, `{common_parameters}` and
`{aesthetics}` with generated documentation.
"""
from plotnine.geoms.geom import _BASE_PARAMS

docstring = dedent(geom.__doc__ or "")
docstring = append_to_section(geom_kwargs, docstring, "Parameters")

# usage
signature = make_signature(
geom.__name__,
geom.DEFAULT_PARAMS,
_BASE_PARAMS | geom.DEFAULT_PARAMS,
common_geom_params,
common_geom_param_values,
)
Expand Down Expand Up @@ -479,6 +481,8 @@ def document_stat(stat: type[stat]) -> type[stat]:
It replaces `{usage}`, `{common_parameters}` and
`{aesthetics}` with generated documentation.
"""
from plotnine.stats.stat import _BASE_PARAMS

# Dedented so that it lineups (in sphinx) with the part
# generated parts when put together
docstring = dedent(stat.__doc__ or "")
Expand All @@ -487,7 +491,7 @@ def document_stat(stat: type[stat]) -> type[stat]:
# usage:
signature = make_signature(
stat.__name__,
stat.DEFAULT_PARAMS,
_BASE_PARAMS | stat.DEFAULT_PARAMS,
common_stat_params,
common_stat_param_values,
)
Expand Down
3 changes: 0 additions & 3 deletions plotnine/geoms/annotation_logticks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ class _geom_logticks(geom_rug):

DEFAULT_AES = {}
DEFAULT_PARAMS = {
"stat": "identity",
"position": "identity",
"na_rm": False,
"sides": "bl",
"alpha": 1,
"color": "black",
Expand Down
3 changes: 0 additions & 3 deletions plotnine/geoms/annotation_stripes.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ class _geom_stripes(geom):
DEFAULT_AES = {}
REQUIRED_AES = set()
DEFAULT_PARAMS = {
"stat": "identity",
"position": "identity",
"na_rm": False,
"color": None,
"fill": ("#AAAAAA", "#CCCCCC"),
"linetype": "solid",
Expand Down
14 changes: 11 additions & 3 deletions plotnine/geoms/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
from plotnine.typing import DataLike


_BASE_PARAMS: dict[str, Any] = {
"stat": "identity",
"position": "identity",
"na_rm": False,
}


class geom(ABC, metaclass=Register):
"""Base class of all Geoms"""

Expand Down Expand Up @@ -82,11 +89,12 @@ def __init__(
self._raw_kwargs = kwargs # Will be used to create stat & layer

# separate aesthetics and parameters
possible_params = _BASE_PARAMS | self.DEFAULT_PARAMS
self.aes_params = {
ae: kwargs[ae] for ae in self.aesthetics() & set(kwargs)
}
self.params = self.DEFAULT_PARAMS | {
k: v for k, v in kwargs.items() if k in self.DEFAULT_PARAMS
self.params = possible_params | {
k: v for k, v in kwargs.items() if k in possible_params
}
self.mapping = kwargs["mapping"]
self.data = kwargs["data"]
Expand Down Expand Up @@ -459,7 +467,7 @@ def handle_na(self, data: pd.DataFrame) -> pd.DataFrame:
"""
return remove_missing(
data,
self.params["na_rm"],
self.params.get("na_rm", False),
list(self.REQUIRED_AES | self.NON_MISSING_AES),
self.__class__.__name__,
)
Expand Down
7 changes: 1 addition & 6 deletions plotnine/geoms/geom_abline.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ class geom_abline(geom):
"alpha": 1,
"size": 0.5,
}
DEFAULT_PARAMS = {
"stat": "identity",
"position": "identity",
"na_rm": False,
"inherit_aes": False,
}
DEFAULT_PARAMS = {"inherit_aes": False}
REQUIRED_AES = {"slope", "intercept"}
draw_legend = staticmethod(geom_path.draw_legend)

Expand Down
6 changes: 1 addition & 5 deletions plotnine/geoms/geom_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ class geom_area(geom_ribbon):
"""

REQUIRED_AES = {"x", "y"}
DEFAULT_PARAMS = {
**geom_ribbon.DEFAULT_PARAMS,
"position": "stack",
"outline_type": "upper",
}
DEFAULT_PARAMS = {"position": "stack", "outline_type": "upper"}

def setup_data(self, data: pd.DataFrame) -> pd.DataFrame:
data["ymin"] = 0
Expand Down
1 change: 0 additions & 1 deletion plotnine/geoms/geom_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class geom_bar(geom_rect):
DEFAULT_PARAMS = {
"stat": "count",
"position": "stack",
"na_rm": False,
"just": 0.5,
"width": None,
}
Expand Down
2 changes: 1 addition & 1 deletion plotnine/geoms/geom_bin_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class geom_bin_2d(geom_rect):
plotnine.stat_bin_2d : The default stat for this `geom`.
"""

DEFAULT_PARAMS = {"stat": "bin_2d", "position": "identity", "na_rm": False}
DEFAULT_PARAMS = {"stat": "bin_2d"}


geom_bin2d = geom_bin_2d
6 changes: 0 additions & 6 deletions plotnine/geoms/geom_blank.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ class geom_blank(geom):
{common_parameters}
"""

DEFAULT_PARAMS = {
"stat": "identity",
"position": "identity",
"na_rm": False,
}

def draw_panel(
self,
data: pd.DataFrame,
Expand Down
1 change: 0 additions & 1 deletion plotnine/geoms/geom_boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class geom_boxplot(geom):
DEFAULT_PARAMS = {
"stat": "boxplot",
"position": "dodge2",
"na_rm": False,
"width": None,
"outlier_alpha": 1,
"outlier_color": None,
Expand Down
8 changes: 1 addition & 7 deletions plotnine/geoms/geom_col.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,4 @@ class geom_col(geom_bar):

REQUIRED_AES = {"x", "y"}
NON_MISSING_AES = {"xmin", "xmax", "ymin", "ymax"}
DEFAULT_PARAMS = {
"stat": "identity",
"position": "stack",
"na_rm": False,
"just": 0.5,
"width": None,
}
DEFAULT_PARAMS = {"position": "stack", "just": 0.5, "width": None}
2 changes: 1 addition & 1 deletion plotnine/geoms/geom_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ class geom_count(geom_point):
plotnine.stat_sum : The default `stat` for this `geom`.
"""

DEFAULT_PARAMS = {"stat": "sum", "position": "identity", "na_rm": False}
DEFAULT_PARAMS = {"stat": "sum"}
8 changes: 1 addition & 7 deletions plotnine/geoms/geom_crossbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@ class geom_crossbar(geom):
"size": 0.5,
}
REQUIRED_AES = {"x", "y", "ymin", "ymax"}
DEFAULT_PARAMS = {
"stat": "identity",
"position": "identity",
"na_rm": False,
"width": 0.5,
"fatten": 2,
}
DEFAULT_PARAMS = {"width": 0.5, "fatten": 2}

legend_key_size = staticmethod(geom_segment.legend_key_size)

Expand Down
6 changes: 1 addition & 5 deletions plotnine/geoms/geom_density.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,4 @@ class geom_density(geom_area):
"weight": 1,
}

DEFAULT_PARAMS = {
**geom_area.DEFAULT_PARAMS,
"stat": "density",
"position": "identity",
}
DEFAULT_PARAMS = {"stat": "density", "outline_type": "upper"}
6 changes: 1 addition & 5 deletions plotnine/geoms/geom_density_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,4 @@ class geom_density_2d(geom_path):
plotnine.stat_density_2d : The default `stat` for this `geom`.
"""

DEFAULT_PARAMS = {
"stat": "density_2d",
"position": "identity",
"na_rm": False,
}
DEFAULT_PARAMS = {"stat": "density_2d"}
2 changes: 0 additions & 2 deletions plotnine/geoms/geom_dotplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class geom_dotplot(geom):
NON_MISSING_AES = {"size", "shape"}
DEFAULT_PARAMS = {
"stat": "bindot",
"position": "identity",
"na_rm": False,
"stackdir": "up",
"stackratio": 1,
"dotsize": 1,
Expand Down
8 changes: 2 additions & 6 deletions plotnine/geoms/geom_errorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ class geom_errorbar(geom):
"size": 0.5,
}
REQUIRED_AES = {"x", "ymin", "ymax"}
DEFAULT_PARAMS = {
"stat": "identity",
"position": "identity",
"na_rm": False,
"width": 0.5,
}
DEFAULT_PARAMS = {"width": 0.5}

draw_legend = staticmethod(geom_path.draw_legend)

def setup_data(self, data: pd.DataFrame) -> pd.DataFrame:
Expand Down
8 changes: 2 additions & 6 deletions plotnine/geoms/geom_errorbarh.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ class geom_errorbarh(geom):
"size": 0.5,
}
REQUIRED_AES = {"y", "xmin", "xmax"}
DEFAULT_PARAMS = {
"stat": "identity",
"position": "identity",
"na_rm": False,
"height": 0.5,
}
DEFAULT_PARAMS = {"height": 0.5}

draw_legend = staticmethod(geom_path.draw_legend)

def setup_data(self, data: pd.DataFrame) -> pd.DataFrame:
Expand Down
10 changes: 6 additions & 4 deletions plotnine/geoms/geom_freqpoly.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from ..doctools import document
from .geom_path import geom_path

_params = geom_path.DEFAULT_PARAMS.copy()
_params["stat"] = "bin"


@document
class geom_freqpoly(geom_path):
Expand All @@ -16,4 +13,9 @@ class geom_freqpoly(geom_path):
of the parameters.
"""

DEFAULT_PARAMS = _params
DEFAULT_PARAMS = {
"stat": "bin",
"lineend": "butt",
"linejoin": "round",
"arrow": None,
}
2 changes: 1 addition & 1 deletion plotnine/geoms/geom_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ class geom_histogram(geom_bar):
plotnine.geom_bar : The default `stat` for this `geom`.
"""

DEFAULT_PARAMS = {"stat": "bin", "position": "stack", "na_rm": False}
DEFAULT_PARAMS = {"stat": "bin", "position": "stack"}
7 changes: 1 addition & 6 deletions plotnine/geoms/geom_hline.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ class geom_hline(geom):
"alpha": 1,
}
REQUIRED_AES = {"yintercept"}
DEFAULT_PARAMS = {
"stat": "identity",
"position": "identity",
"na_rm": False,
"inherit_aes": False,
}
DEFAULT_PARAMS = {"inherit_aes": False}

draw_legend = staticmethod(geom_path.draw_legend)
legend_key_size = staticmethod(geom_path.legend_key_size)
Expand Down
2 changes: 0 additions & 2 deletions plotnine/geoms/geom_jitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ class geom_jitter(geom_point):
"""

DEFAULT_PARAMS = {
"stat": "identity",
"position": "jitter",
"na_rm": False,
"width": None,
"height": None,
"random_state": None,
Expand Down
38 changes: 15 additions & 23 deletions plotnine/geoms/geom_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,6 @@
from plotnine.layer import layer


_aes = geom_text.DEFAULT_AES.copy()
_aes["fill"] = "white"

_params = geom_text.DEFAULT_PARAMS.copy()
_params.update(
{
# boxstyle is one of
# circle, larrow, rarrow, round, round4,
# roundtooth, sawtooth, square
#
# Documentation at matplotlib.patches.BoxStyle
"boxstyle": "round",
"boxcolor": None,
"label_padding": 0.25,
"label_r": 0.25,
"label_size": 0.7,
"tooth_size": None,
}
)


@document
class geom_label(geom_text):
"""
Expand Down Expand Up @@ -80,8 +59,21 @@ class geom_label(geom_text):
parameters that affect the boxstyle.
"""

DEFAULT_AES = _aes
DEFAULT_PARAMS = _params
DEFAULT_AES = {**geom_text.DEFAULT_AES, "fill": "white"}
DEFAULT_PARAMS = {
**geom_text.DEFAULT_PARAMS,
# boxstyle is one of
# circle, larrow, rarrow, round, round4,
# roundtooth, sawtooth, square
#
# Documentation at matplotlib.patches.BoxStyle
"boxstyle": "round",
"boxcolor": None,
"label_padding": 0.25,
"label_r": 0.25,
"label_size": 0.7,
"tooth_size": None,
}

@staticmethod
def draw_legend(
Expand Down
6 changes: 1 addition & 5 deletions plotnine/geoms/geom_linerange.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ class geom_linerange(geom):
"size": 0.5,
}
REQUIRED_AES = {"x", "ymin", "ymax"}
DEFAULT_PARAMS = {
"stat": "identity",
"position": "identity",
"na_rm": False,
}

draw_legend = staticmethod(geom_path.draw_legend)

@staticmethod
Expand Down
Loading