8000 Allow `shared_yaxes` to work with secondary axes by gmjw · Pull Request #5180 · plotly/plotly.py · GitHub
[go: up one dir, main page]

Skip to content

Allow shared_yaxes to work with secondary axes #5180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Fix sharing of secondary y-axis
  • Loading branch information
Gareth Williams committed May 12, 2025
commit fd64e0012fb2d6c2531d01504b8ac31fa3e7166d
37 changes: 26 additions & 11 deletions plotly/_subplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,10 @@ def _check_hv_spacing(dimsize, spacing, name, dimvarname, dimname):
)
grid_ref[r][c] = subplot_refs

_configure_shared_axes(layout, grid_ref, specs, "x", shared_xaxes, row_dir)
_configure_shared_axes(layout, grid_ref, specs, "y", shared_yaxes, row_dir)
_configure_shared_axes(layout, grid_ref, specs, "x", shared_xaxes, row_dir, False)
_configure_shared_axes(layout, grid_ref, specs, "y", shared_yaxes, row_dir, False)
if secondary_y:
_configure_shared_axes(layout, grid_ref, specs, "y", shared_yaxes, row_dir, True)

# Build inset reference
# ---------------------
Expand Down Expand Up @@ -887,7 +889,7 @@ def _check_hv_spacing(dimsize, spacing, name, dimvarname, dimname):
return figure


def _configure_shared_axes(layout, grid_ref, specs, x_or_y, shared, row_dir):
def _configure_shared_axes(layout, grid_ref, specs, x_or_y, shared, row_dir, secondary_y):
rows = len(grid_ref)
cols = len(grid_ref[0])

Expand All @@ -898,6 +900,13 @@ def _configure_shared_axes(layout, grid_ref, specs, x_or_y, shared, row_dir):
else:
rows_iter = range(rows)

if secondary_y:
cols_iter = range(cols - 1, -1, -1)
axis_index = 1
else:
cols_iter = range(cols)
axis_index = 0

def update_axis_matches(first_axis_id, subplot_ref, spec, remove_label):
if subplot_ref is None:
return first_axis_id
Expand All @@ -921,13 +930,15 @@ def update_axis_matches(first_axis_id, subplot_ref, spec, remove_label):
return first_axis_id

if shared == "columns" or (x_or_y == "x" and shared is True):
for c in range(cols):
for c in cols_iter:
first_axis_id = None
ok_to_remove_label = x_or_y == "x"
for r in rows_iter:
if not grid_ref[r][c]:
continue
subplot_ref = grid_ref[r][c][0]
if axis_index >= len(grid_ref[r][c]):
continue
subplot_ref = grid_ref[r][c][axis_index]
spec = specs[r][c]
first_axis_id = update_axis_matches(
first_axis_id, subplot_ref, spec, ok_to_remove_label
Expand All @@ -937,26 +948,30 @@ def update_axis_matches(first_axis_id, subplot_ref, spec, remove_label):
for r in rows_iter:
first_axis_id = None
ok_to_remove_label = x_or_y == "y"
for c in range(cols):
for c in cols_iter:
if not grid_ref[r][c]:
continue
subplot_ref = grid_ref[r][c][0]
if axis_index >= len(grid_ref[r][c]):
continue
subplot_ref = grid_ref[r][c][axis_index]
spec = specs[r][c]
first_axis_id = update_axis_matches(
first_axis_id, subplot_ref, spec, ok_to_remove_label
)

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

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

Expand Down
0