Skip to content

Commit b076a38

Browse files
simplify get_orderings
1 parent 6a1ec9d commit b076a38

File tree

1 file changed

+27
-31
lines changed
  • packages/python/plotly/plotly/express

1 file changed

+27
-31
lines changed

Diff for: packages/python/plotly/plotly/express/_core.py

+27-31
Original file line numberDiff line numberDiff line change
@@ -1816,43 +1816,41 @@ def infer_config(args, constructor, trace_patch, layout_patch):
18161816

18171817
def get_orderings(args, grouper, grouped):
18181818
"""
1819-
`orders` is the user-supplied ordering (with the remaining data-frame-supplied
1820-
ordering appended if the column is used for grouping). It includes anything the user
1821-
gave, for any variable, including values not present in the dataset. It is used
1822-
downstream to set e.g. `categoryarray` for cartesian axes
1823-
1824-
`group_names` is the set of groups, ordered by the order above
1825-
1826-
`group_values` is a subset of `orders` in both keys and values. It contains a key
1827-
for every grouped mapping and its values are the sorted *data* values for these
1828-
mappings.
1819+
`orders` is the user-supplied ordering with the remaining data-frame-supplied
1820+
ordering appended if the column is used for grouping. It includes anything the user
1821+
gave, for any variable, including values not present in the dataset. It's a dict
1822+
where the keys are e.g. "x" or "color"
1823+
1824+
`sorted_group_names` is the set of groups, ordered by the order above. It's a list
1825+
of tuples like [("value1", ""), ("value2", "")] where each tuple contains the name
1826+
of a single dimension-group
18291827
"""
1828+
18301829
orders = {} if "category_orders" not in args else args["category_orders"].copy()
1831-
group_names = []
1832-
group_values = {}
1830+
for col in grouper:
1831+
if col != one_group:
1832+
uniques = args["data_frame"][col].unique()
1833+
if col not in orders:
1834+
orders[col] = list(uniques)
1835+
else:
1836+
orders[col] = list(orders[col])
1837+
for val in uniques:
1838+
if val not in orders[col]:
1839+
orders[col].append(val)
1840+
1841+
sorted_group_names = []
18331842
for group_name in grouped.groups:
18341843
if len(grouper) == 1:
18351844
group_name = (group_name,)
1836-
group_names.append(group_name)
1837-
for col in grouper:
1838-
if col != one_group:
1839-
uniques = args["data_frame"][col].unique()
1840-
if col not in orders:
1841-
orders[col] = list(uniques)
1842-
else:
1843-
orders[col] = list(orders[col])
1844-
for val in uniques:
1845-
if val not in orders[col]:
1846-
orders[col].append(val)
1847-
group_values[col] = sorted(uniques, key=orders[col].index)
1845+
sorted_group_names.append(group_name)
18481846

18491847
for i, col in reversed(list(enumerate(grouper))):
18501848
if col != one_group:
1851-
group_names = sorted(
1852-
group_names,
1849+
sorted_group_names = sorted(
1850+
sorted_group_names,
18531851
key=lambda g: orders[col].index(g[i]) if g[i] in orders[col] else -1,
18541852
)
1855-
return orders, group_names, group_values
1853+
return orders, sorted_group_names
18561854

18571855

18581856
def make_figure(args, constructor, trace_patch=None, layout_patch=None):
@@ -1873,15 +1871,13 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
18731871
grouper = [x.grouper or one_group for x in grouped_mappings] or [one_group]
18741872
grouped = args["data_frame"].groupby(grouper, sort=False)
18751873

1876-
orders, sorted_group_names, sorted_group_values = get_orderings(
1877-
args, grouper, grouped
1878-
)
1874+
orders, sorted_group_names = get_orderings(args, grouper, grouped)
18791875

18801876
col_labels = []
18811877
row_labels = []
18821878
nrows = ncols = 1
18831879
for m in grouped_mappings:
1884-
if m.grouper not in sorted_group_values:
1880+
if m.grouper not in orders:
18851881
m.val_map[""] = m.sequence[0]
18861882
else:
18871883
sorted_values = orders[m.grouper]

0 commit comments

Comments
 (0)