Skip to content

Commit 589460c

Browse files
committed
Fix plotly express marginals
Go back to start_cell bottom-left since that makes marginal code easier. Now flip the row index of traces before they are inserted in the grid.
1 parent 6d622c9 commit 589460c

File tree

1 file changed

+19
-10
lines changed
  • packages/python/plotly/plotly/express

1 file changed

+19
-10
lines changed

packages/python/plotly/plotly/express/_core.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,18 @@ def configure_cartesian_marginal_axes(args, fig, orders):
352352

353353
# Add axis titles to non-marginal subplots
354354
y_title = get_decorated_label(args, args["y"], "y")
355-
for row in range(1, nrows):
356-
fig.update_yaxes(title_text=y_title, row=row, col=1)
355+
if args["marginal_x"]:
356+
fig.update_yaxes(title_text=y_title, row=1, col=1)
357+
else:
358+
for row in range(1, nrows + 1):
359+
fig.update_yaxes(title_text=y_title, row=row, col=1)
357360

358361
x_title = get_decorated_label(args, args["x"], "x")
359-
for col in range(1, ncols):
360-
fig.update_xaxes(title_text=x_title, row=1, col=col)
362+
if args["marginal_y"]:
363+
fig.update_xaxes(title_text=x_title, row=1, col=1)
364+
else:
365+
for col in range(1, ncols + 1):
366+
fig.update_xaxes(title_text=x_title, row=1, col=col)
361367

362368
# Configure axis type across all x-axes
363369
if "log_x" in args and args["log_x"]:
@@ -393,8 +399,7 @@ def configure_cartesian_axes(args, fig, orders):
393399

394400
# Set x-axis titles and axis options in the bottom-most row
395401
x_title = get_decorated_label(args, args["x"], "x")
396-
nrows = len(fig._grid_ref)
397-
for xaxis in fig.select_xaxes(row=nrows):
402+
for xaxis in fig.select_xaxes(row=1):
398403
xaxis.update(title_text=x_title)
399404
set_cartesian_axis_opts(args, xaxis, "x", orders)
400405

@@ -959,7 +964,7 @@ def make_figure(args, constructor, trace_patch={}, layout_patch={}):
959964
row = m.val_map[val]
960965
trace._subplot_row_val = val
961966
else:
962-
if trace_spec.marginal == "x":
967+
if has_marginal_x and trace_spec.marginal != "x":
963968
row = 2
964969
else:
965970
row = 1
@@ -1040,7 +1045,11 @@ def make_figure(args, constructor, trace_patch={}, layout_patch={}):
10401045
continue
10411046

10421047
_set_trace_grid_reference(
1043-
trace, fig.layout, fig._grid_ref, trace._subplot_row, trace._subplot_col
1048+
trace,
1049+
fig.layout,
1050+
fig._grid_ref,
1051+
nrows - trace._subplot_row + 1,
1052+
trace._subplot_col,
10441053
)
10451054

10461055
# Add traces, layout and frames to figure
@@ -1123,13 +1132,13 @@ def init_figure(
11231132
specs=specs,
11241133
shared_xaxes="all",
11251134
shared_yaxes="all",
1126-
row_titles=row_titles,
1135+
row_titles=list(reversed(row_titles)),
11271136
column_titles=column_titles,
11281137
horizontal_spacing=horizontal_spacing,
11291138
vertical_spacing=vertical_spacing,
11301139
row_heights=row_heights,
11311140
column_widths=column_widths,
1132-
start_cell="top-left",
1141+
start_cell="bottom-left",
11331142
)
11341143

11351144
# Remove explicit font size of row/col titles so template can take over

0 commit comments

Comments
 (0)