plotnine.mapping._eval_environment.reorder

reorder(x, y, fun=np.median, ascending=True)

Reorder categorical by sorting along another variable

It is the order of the categories that changes. Values in x are grouped by categories and summarised to determine the new order.

Credit: Copied from plydata

Parameters

x : list - like

Values that will make up the categorical.

y : list - like

Values by which c will be ordered.

fun : callable = np.median

Summarising function to x for each category in c. Default is the median.

ascending : bool = True

If True, the c is ordered in ascending order of x.

Examples

import pandas as pd
from plotnine import ggplot, aes, geom_col

df = pd.DataFrame({
    "x": ["b", "d", "c", "a"],
    "y": [1, 2, 3, 4]
})

ggplot(df, aes("reorder(x, y)", "y")) + geom_col()

Source: reorder.ipynb