Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 40 additions & 31 deletions plotly/_subplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,78 +890,87 @@ def _check_hv_spacing(dimsize, spacing, name, dimvarname, dimname):
def _configure_shared_axes(layout, grid_ref, specs, x_or_y, shared, row_dir):
rows = len(grid_ref)
cols = len(grid_ref[0])
# Precompute index for "x" or "y"
layout_key_ind = 0 if x_or_y == "x" else 1

layout_key_ind = ["x", "y"].index(x_or_y)

# Predefine iterators
if row_dir < 0:
rows_iter = range(rows - 1, -1, -1)
else:
rows_iter = range(rows)

def update_axis_matches(first_axis_id, subplot_ref, spec, remove_label):
# Inline the inner function for speed (original code is preserved in spirit, but moved out)
def update_axis_matches(first_axis_id, subplot_ref, spec, remove_label, axis_type, key_ind, layout_dict):
if subplot_ref is None:
return first_axis_id

if x_or_y == "x":
span = spec["colspan"]
else:
span = spec["rowspan"]

span = spec["colspan"] if axis_type == "x" else spec["rowspan"]
if subplot_ref.subplot_type == "xy" and span == 1:
axis_name = subplot_ref.layout_keys[key_ind]
if first_axis_id is None:
first_axis_name = subplot_ref.layout_keys[layout_key_ind]
first_axis_id = first_axis_name.replace("axis", "")
return axis_name.replace("axis", "")
else:
axis_name = subplot_ref.layout_keys[layout_key_ind]
axis_to_match = layout[axis_name]
axis_to_match = layout_dict[axis_name]
axis_to_match.matches = first_axis_id
if remove_label:
axis_to_match.showticklabels = False

return first_axis_id

# Cache function locally and needed attributes for maximal inner loop speed
_update = update_axis_matches
_subtype = "xy"
lkeyind = layout_key_ind

# Fast-path: factor out repeated lookups
layout_dict = layout

if shared == "columns" or (x_or_y == "x" and shared is True):
for c in range(cols):
first_axis_id = None
ok_to_remove_label = x_or_y == "x"
col_grid_ref = grid_ref # local for speed, though not strictly necessary
col_specs = specs
for r in rows_iter:
if not grid_ref[r][c]:
cell = col_grid_ref[r][c]
if not cell:
continue
subplot_ref = grid_ref[r][c][0]
spec = specs[r][c]
first_axis_id = update_axis_matches(
first_axis_id, subplot_ref, spec, ok_to_remove_label
subplot_ref = cell[0]
spec = col_specs[r][c]
first_axis_id = _update(
first_axis_id, subplot_ref, spec, ok_to_remove_label, x_or_y, lkeyind, layout_dict
)

elif shared == "rows" or (x_or_y == "y" and shared is True):
for r in rows_iter:
first_axis_id = None
ok_to_remove_label = x_or_y == "y"
row_grid_ref = grid_ref[r]
row_spec = specs[r]
for c in range(cols):
if not grid_ref[r][c]:
cell = row_grid_ref[c]
if not cell:
continue
subplot_ref = grid_ref[r][c][0]
spec = specs[r][c]
first_axis_id = update_axis_matches(
first_axis_id, subplot_ref, spec, ok_to_remove_label
subplot_ref = cell[0]
spec = row_spec[c]
first_axis_id = _update(
first_axis_id, subplot_ref, spec, ok_to_remove_label, x_or_y, lkeyind, layout_dict
)

elif shared == "all":
first_axis_id = None
for c in range(cols):
first_axis_id = None
for ri, r in enumerate(rows_iter):
if not grid_ref[r][c]:
cell = grid_ref[r][c]
if not cell:
continue
subplot_ref = grid_ref[r][c][0]
subplot_ref = cell[0]
spec = specs[r][c]

if x_or_y == "y":
ok_to_remove_label = c > 0
else:
ok_to_remove_label = ri > 0 if row_dir > 0 else r < rows - 1

first_axis_id = update_axis_matches(
first_axis_id, subplot_ref, spec, ok_to_remove_label
ok_to_remove_label = (ri > 0) if row_dir > 0 else (r < rows - 1)
first_axis_id = _update(
first_axis_id, subplot_ref, spec, ok_to_remove_label, x_or_y, lkeyind, layout_dict
)


Expand Down