How to use the plotnine.stats.stat.stat function in plotnine

To help you get started, we’ve selected a few plotnine examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github has2k1 / plotnine / plotnine / stats / stat_density_2d.py View on Github external
import numpy as np
import pandas as pd
import matplotlib._contour as _contour
from mizani.breaks import extended_breaks

from ..doctools import document
from .stat import stat
from .density import kde, get_var_type


@document
class stat_density_2d(stat):
    """
    Compute 2D kernel density estimation

    {usage}

    Parameters
    ----------
    {common_parameters}
    contour : bool
        Whether to create contours of the 2d density estimate.
        Default is True.
    n : int, optional(default: 64)
        Number of equally spaced points at which the density is to
        be estimated. For efficient computation, it should be a power
        of two.
    levels : int or array_like
github has2k1 / plotnine / plotnine / stats / stat_count.py View on Github external
import numpy as np
import pandas as pd

from ..utils import resolution
from ..doctools import document
from ..exceptions import PlotnineError
from .stat import stat


@document
class stat_count(stat):
    """
    Counts the number of cases at each x position

    {usage}

    Parameters
    ----------
    {common_parameters}
    width : float, optional (default: None)
        Bar width. By default, set to 90% of the
        resolution of the data

    See Also
    --------
    plotnine.stats.stat_bin
    """
github has2k1 / plotnine / plotnine / stats / stat_bin_2d.py View on Github external
import itertools
import types
from contextlib import suppress

import pandas as pd
import numpy as np

from ..utils import is_scalar_or_string
from ..doctools import document
from .binning import fuzzybreaks
from .stat import stat


@document
class stat_bin_2d(stat):
    """
    2 Dimensional bin counts

    {usage}

    Parameters
    ----------
    {common_parameters}
    bins : int, optional (default: 30)
        Number of bins. Overridden by binwidth.
    breaks : array-like(s), optional (default: None)
        Bin boundaries. This supercedes the ``binwidth``, ``bins``,
        ``center`` and ``boundary``. It can be an array_like or
        a list of two array_likes to provide distinct breaks for
        the ``x`` and ``y`` axes.
    binwidth : float, optional (default: None)
github has2k1 / plotnine / plotnine / stats / stat_summary_bin.py View on Github external
import numpy as np
import pandas as pd

from ..utils import groupby_apply
from ..doctools import document
from ..exceptions import PlotnineError
from ..scales.scale import scale_discrete
from .binning import fuzzybreaks
from .stat_summary import make_summary_fun
from .stat import stat


@document
class stat_summary_bin(stat):
    """
    Summarise y values at x intervals

    {usage}

    Parameters
    ----------
    {common_parameters}
    binwidth : float or tuple, optional (default: None)
        The width of the bins. The default is to use bins bins that
        cover the range of the data. You should always override this
        value, exploring multiple widths to find the best to illustrate
        the stories in your data.
    bins : int or tuple, optional (default: 30)
        Number of bins. Overridden by binwidth.
    breaks : array-like(s), optional (default: None)
github has2k1 / plotnine / plotnine / stats / stat_sina.py View on Github external
import numpy as np
import pandas as pd
from scipy.interpolate import interp1d

from ..aes import has_groups
from ..doctools import document
from ..exceptions import PlotnineError
from ..utils import array_kind, jitter, resolution
from .binning import breaks_from_bins, breaks_from_binwidth
from .stat import stat
from .stat_density import compute_density


@document
class stat_sina(stat):
    """
    Compute Sina plot values

    {usage}

    Parameters
    ----------
    {common_parameters}
    binwidth : float
        The width of the bins. The default is to use bins that
        cover the range of the data. You should always override this
        value, exploring multiple widths to find the best to
        illustrate the stories in your data.
    bins : int (default: 50)
        Number of bins. Overridden by binwidth.
    method : 'density' or 'counts'
github has2k1 / plotnine / plotnine / stats / stat_summary.py View on Github external
d['ymin'] = [fun_ymin(df['y'], **kwargs)]
            if fun_ymax:
                kwargs = get_valid_kwargs(fun_ymax, fun_args)
                d['ymax'] = [fun_ymax(df['y'], **kwargs)]
            return pd.DataFrame(d)
    elif fun_data:
        kwargs = get_valid_kwargs(fun_data, fun_args)

        def func(df):
            return fun_data(df['y'], **kwargs)

    return func


@document
class stat_summary(stat):
    """
    Calculate summary statistics depending on x

    {usage}

    Parameters
    ----------
    {common_parameters}
    fun_data : str or function, optional
        One of
        :py:`['mean_cl_boot', 'mean_cl_normal', 'mean_sdl', 'median_hilow']`
        or any function that takes a array and returns a dataframe
        with three rows indexed as ``y``, ``ymin`` and ``ymax``.
        Defaults to :py:`'mean_cl_boot'`.
    fun_y : function, optional (default: None)
        Any function that takes a array-like and returns a value
github has2k1 / plotnine / plotnine / stats / stat_ellipse.py View on Github external
from warnings import warn

import numpy as np
import pandas as pd
import scipy.stats as stats
from scipy import linalg

from ..doctools import document
from ..exceptions import PlotnineWarning
from .stat import stat


@document
class stat_ellipse(stat):
    """
    Calculate normal confidence interval ellipse

    {usage}

    Parameters
    ----------
    {common_parameters}
    type : str in ['t', 'norm', 'euclid'] (default: 't')
        The type of ellipse.

        - ``'t'`` - assumes a multivariate t-distribution, and
        - ``'norm'`` - assumes a multivariate normal distribution.
        - ``'euclid'`` - draws a circle with the radius equal to
          `level`, representing the euclidean distance from the
          center.
github has2k1 / plotnine / plotnine / stats / stat_density.py View on Github external
from warnings import warn

import numpy as np
import pandas as pd
import statsmodels.api as sm
from scipy.stats import iqr

from ..doctools import document
from ..exceptions import PlotnineError, PlotnineWarning
from .stat import stat


# NOTE: Parameter discriptions are in
# statsmodels/nonparametric/kde.py
@document
class stat_density(stat):
    """
    Compute density estimate

    {usage}

    Parameters
    ----------
    {common_parameters}
    kernel : str, optional (default: 'gaussian')
        Kernel used for density estimation. One of::

            'biweight'
            'cosine'
            'cosine2'
            'epanechnikov'
            'gaussian'
github has2k1 / plotnine / plotnine / stats / stat_sum.py View on Github external
from ..aes import all_aesthetics
from ..utils import groupby_apply
from ..doctools import document
from .stat import stat


@document
class stat_sum(stat):
    """
    Sum unique values

    Useful for overplotting on scatterplots.

    {usage}

    Parameters
    ----------
    {common_parameters}
    """

    _aesthetics_doc = """
    {aesthetics_table}

    .. rubric:: Options for computed aesthetics
github has2k1 / plotnine / plotnine / stats / stat.py View on Github external
Raises
        ------
        :class:`PlotnineError` if unable to create a `stat`.
        """
        name = geom.params['stat']
        kwargs = geom._kwargs
        # More stable when reloading modules than
        # using issubclass
        if (not isinstance(name, type) and
                hasattr(name, 'compute_layer')):
            return name

        if isinstance(name, stat):
            return name
        elif isinstance(name, type) and issubclass(name, stat):
            klass = name
        elif is_string(name):
            if not name.startswith('stat_'):
                name = 'stat_{}'.format(name)
            klass = Registry[name]
        else:
            raise PlotnineError(
                'Unknown stat of type {}'.format(type(name)))

        valid_kwargs = (
             (klass.aesthetics() |
              klass.DEFAULT_PARAMS.keys()) &
             kwargs.keys())

        params = {k: kwargs[k] for k in valid_kwargs}
        return klass(geom=geom, **params)