plotnine.mapping._eval_environment.factor

factor(values, categories=None, ordered=None)

Turn x in to a categorical (factor) variable

It is just an alias to pandas.Categorical

Parameters

values : Sequence[Any]

The values of the categorical. If categories are given, values not in categories will be replaced with NaN.

categories : Sequence[Any] | None = None

The unique categories for this categorical. If not given, the categories are assumed to be the unique values of values (sorted, if possible, otherwise in the order in which they appear).

ordered : bool | None = None

Whether or not this categorical is treated as a ordered categorical. If True, the resulting categorical will be ordered. An ordered categorical respects, when sorted, the order of its categories attribute (which in turn is the categories argument, if provided).

Examples

from plotnine import ggplot, aes, geom_bar
from plotnine.data import mtcars

ggplot(mtcars, aes(x="factor(cyl)")) + geom_bar()

Source: factor.ipynb