@@ -652,9 +652,6 @@ def set_cartesian_axis_opts(args, axis, letter, orders):
652
652
653
653
654
654
def configure_cartesian_marginal_axes (args , fig , orders ):
655
- if "histogram" in [args ["marginal_x" ], args ["marginal_y" ]]:
656
- fig .layout ["barmode" ] = "overlay"
657
-
658
655
nrows = len (fig ._grid_ref )
659
656
ncols = len (fig ._grid_ref [0 ])
660
657
@@ -1497,17 +1494,14 @@ def build_dataframe(args, constructor):
1497
1494
# If data_frame is provided, we parse it into a narwhals DataFrame, while accounting
1498
1495
# for compatibility with pandas specific paths (e.g. Index/MultiIndex case).
1499
1496
if df_provided :
1500
-
1501
1497
# data_frame is pandas-like DataFrame (pandas, modin.pandas, cudf)
1502
1498
if nw .dependencies .is_pandas_like_dataframe (args ["data_frame" ]):
1503
-
1504
1499
columns = args ["data_frame" ].columns # This can be multi index
1505
1500
args ["data_frame" ] = nw .from_native (args ["data_frame" ], eager_only = True )
1506
1501
is_pd_like = True
1507
1502
1508
1503
# data_frame is pandas-like Series (pandas, modin.pandas, cudf)
1509
1504
elif nw .dependencies .is_pandas_like_series (args ["data_frame" ]):
1510
-
1511
1505
args ["data_frame" ] = nw .from_native (
1512
1506
args ["data_frame" ], series_only = True
1513
1507
).to_frame ()
@@ -1861,7 +1855,6 @@ def _check_dataframe_all_leaves(df: nw.DataFrame) -> None:
1861
1855
for row_idx , row in zip (
1862
1856
null_indices_mask , null_mask .filter (null_indices_mask ).iter_rows ()
1863
1857
):
1864
-
1865
1858
i = row .index (True )
1866
1859
1867
1860
if not all (row [i :]):
@@ -1990,7 +1983,6 @@ def process_dataframe_hierarchy(args):
1990
1983
1991
1984
if args ["color" ]:
1992
1985
if discrete_color :
1993
-
1994
1986
discrete_aggs .append (args ["color" ])
1995
1987
agg_f [args ["color" ]] = nw .col (args ["color" ]).max ()
1996
1988
agg_f [f'{ args ["color" ]} { n_unique_token } ' ] = (
@@ -2045,7 +2037,6 @@ def post_agg(dframe: nw.LazyFrame, continuous_aggs, discrete_aggs) -> nw.LazyFra
2045
2037
).drop ([f"{ col } { n_unique_token } " for col in discrete_aggs ])
2046
2038
2047
2039
for i , level in enumerate (path ):
2048
-
2049
2040
dfg = (
2050
2041
df .group_by (path [i :], drop_null_keys = True )
2051
2042
.agg (** agg_f )
@@ -2422,7 +2413,6 @@ def get_groups_and_orders(args, grouper):
2422
2413
# figure out orders and what the single group name would be if there were one
2423
2414
single_group_name = []
2424
2415
unique_cache = dict ()
2425
- grp_to_idx = dict ()
2426
2416
2427
2417
for i , col in enumerate (grouper ):
2428
2418
if col == one_group :
@@ -2440,27 +2430,28 @@ def get_groups_and_orders(args, grouper):
2440
2430
else :
2441
2431
orders [col ] = list (OrderedDict .fromkeys (list (orders [col ]) + uniques ))
2442
2432
2443
- grp_to_idx = {k : i for i , k in enumerate (orders )}
2444
-
2445
2433
if len (single_group_name ) == len (grouper ):
2446
2434
# we have a single group, so we can skip all group-by operations!
2447
2435
groups = {tuple (single_group_name ): df }
2448
2436
else :
2449
- required_grouper = list ( orders . keys ())
2437
+ required_grouper = [ group for group in orders if group in grouper ]
2450
2438
grouped = dict (df .group_by (required_grouper , drop_null_keys = True ).__iter__ ())
2451
- sorted_group_names = list (grouped .keys ())
2452
2439
2453
- for i , col in reversed (list (enumerate (required_grouper ))):
2454
- sorted_group_names = sorted (
2455
- sorted_group_names ,
2456
- key = lambda g : orders [col ].index (g [i ]) if g [i ] in orders [col ] else - 1 ,
2457
- )
2440
+ sorted_group_names = sorted (
2441
+ grouped .keys (),
2442
+ key = lambda values : [
2443
+ orders [group ].index (value ) if value in orders [group ] else - 1
2444
+ for group , value in zip (required_grouper , values )
2445
+ ],
2446
+ )
2458
2447
2459
2448
# calculate the full group_names by inserting "" in the tuple index for one_group groups
2460
2449
full_sorted_group_names = [
2461
2450
tuple (
2462
2451
[
2463
- "" if col == one_group else sub_group_names [grp_to_idx [col ]]
2452
+ ""
2453
+ if col == one_group
2454
+ else sub_group_names [required_grouper .index (col )]
2464
2455
for col in grouper
2465
2456
]
2466
2457
)
@@ -2487,6 +2478,10 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
2487
2478
constructor = go .Bar
2488
2479
args = process_dataframe_timeline (args )
2489
2480
2481
+ # If we have marginal histograms, set barmode to "overlay"
2482
+ if "histogram" in [args .get ("marginal_x" ), args .get ("marginal_y" )]:
2483
+ layout_patch ["barmode" ] = "overlay"
2484
+
2490
2485
trace_specs , grouped_mappings , sizeref , show_colorbar = infer_config (
2491
2486
args , constructor , trace_patch , layout_patch
2492
2487
)
@@ -2558,7 +2553,12 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
2558
2553
legendgroup = trace_name ,
2559
2554
showlegend = (trace_name != "" and trace_name not in trace_names ),
2560
2555
)
2561
- if trace_spec .constructor in [go .Bar , go .Violin , go .Box , go .Histogram ]:
2556
+
2557
+ # Set 'offsetgroup' only in group barmode (or if no barmode is set)
2558
+ barmode = layout_patch .get ("barmode" )
2559
+ if trace_spec .constructor in [go .Bar , go .Box , go .Violin , go .Histogram ] and (
2560
+ barmode == "group" or barmode is None
2561
+ ):
2562
2562
trace .update (alignmentgroup = True , offsetgroup = trace_name )
2563
2563
trace_names .add (trace_name )
2564
2564
0 commit comments