def _scale()

in python-package/lets_plot/plot/scale.py [0:0]


def _scale(aesthetic, *,
           name=None,
           breaks=None, labels=None,
           lablim=None,
           limits=None,
           expand=None,
           na_value=None,
           trans=None,
           guide=None,
           format=None,
           position=None,
           **kwargs):
    """
    Create a scale (discrete or continuous)

    Parameters
    ----------
    aesthetic : str or list
        The name(s) of the aesthetic(s) that this scale works with.
    name : str
        The name of the scale - used as the axis label or the legend title
    breaks : list or dict
        A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values.
    labels : list of str or dict
        A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels.
    lablim : int, default=None
        The maximum label length (in characters) before trimming is applied.
    limits : list
        A numeric vector of length two providing limits of the scale.
    expand : list
        A numeric vector of length two giving multiplicative and additive expansion constants.
    na_value
        Value to use for missing values
    trans : str
        Name of built-in transformation.
    guide
        Type of legend. Use 'colorbar' for continuous color bar, or 'legend' for discrete values.
    format : str
        A string of the format for labels on the scale. Supported types are number and date/time.
    position : str
        For position scales,
        The position of the axis:
         - 'left', 'right' or 'both' for y-axis;
         - 'top', 'bottom' or 'both' for x-axis.

    Returns
    -------
    ``FeatureSpec`` or ``FeatureSpecArray``
        Scales specification.

    """

    # flatten the 'other' sub-dictionary
    args = locals().copy()
    args.pop('kwargs')

    # 'breaks' - dict of labels as keys and breaks as values
    if isinstance(breaks, dict):
        if labels is None:
            args['labels'] = list(breaks.keys())
        breaks = list(breaks.values())
        args['breaks'] = breaks

    # 'labels' - dict of breaks as keys and labels as values
    if isinstance(labels, dict):
        if breaks is None:
            args['breaks'] = list(labels.keys())
            args['labels'] = list(labels.values())
        else:
            new_labels = []
            new_breaks = []
            for break_value in breaks:
                if break_value in labels:
                    new_labels.append(labels[break_value])
                    new_breaks.append(break_value)

            breaks_without_label = [item for item in breaks if item not in new_breaks]  # keeps order
            args['breaks'] = new_breaks + breaks_without_label
            args['labels'] = new_labels

    # Color aesthetics that support palette generation
    _COLOR_AESTHETICS = {'color', 'fill', 'paint_a', 'paint_b', 'paint_c'}

    specs = []
    if isinstance(aesthetic, list):
        args.pop('aesthetic')
        for aes in aesthetic:
            specs.append(FeatureSpec('scale', aesthetic=aes, **args, **kwargs))
    else:
        # Use ColorScaleFeatureSpec for color aesthetics when creating a single spec
        if aesthetic in _COLOR_AESTHETICS:
            specs.append(ColorScaleFeatureSpec('scale', **args, **kwargs))
        else:
            specs.append(FeatureSpec('scale', **args, **kwargs))

    if len(specs) == 1:
        return specs[0]

    return FeatureSpecArray(*specs)