Skip to content

Commit 7ac1e27

Browse files
Adding it back for two x-axis columns and y-axis rows
- Changed the code to allow for the axis sharing for the x-axis in columns mode and y-axis in rows mode - Disabled the sharing for all mode (known bug) - Disabled for secondary_y (due to wanting to match the testing
1 parent 5a2740a commit 7ac1e27

2 files changed

Lines changed: 224 additions & 201 deletions

File tree

plotly/_subplots.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,9 @@ def _configure_shared_axes(
920920
row_count : int = len(grid_ref)
921921
column_count : int = len(grid_ref[0])
922922

923+
BASE_TRACE_LAYER = 0
924+
SECOND_Y_LAYER = 1
925+
923926
axis_index : int = 0 if x_or_y == 'x' else 1
924927

925928
def find_label_and_index(row_order : int | Tuple[int], column_order : int | Tuple[int], trace_layer : int) -> Optional[Tuple[str, Tuple[int, int]]]:
@@ -942,6 +945,7 @@ def find_label_and_index(row_order : int | Tuple[int], column_order : int | Tupl
942945
column_order : Tuple[int] = [column_order] if isinstance(column_order, int) else column_order
943946

944947

948+
945949
# Iterate through the rows and columns
946950
for row in row_order:
947951
for column in column_order:
@@ -965,7 +969,7 @@ def find_label_and_index(row_order : int | Tuple[int], column_order : int | Tupl
965969
return None
966970

967971

968-
def update_trace_axis(axis_label : str, row : int, column : int, trace_layer : int, can_hide_ticks : bool, can_match_axis : bool) -> None:
972+
def update_trace_axis(axis_label : str, row : int, column : int, trace_layer : int, can_reassign_axis : bool, can_hide_ticks : bool, can_match_axis : bool) -> None:
969973
'''
970974
Updates the specific subplot trace at the given row and column with the given label, and removes the label visibility if necessary; ONLY WORKS WITH 2D CARTESIAN SUBPLOTS AKA 'xy' TYPE SUBPLOTS
971975
@@ -996,13 +1000,17 @@ def update_trace_axis(axis_label : str, row : int, column : int, trace_layer : i
9961000
return
9971001

9981002
axis_name : str = trace.layout_keys[axis_index]
1003+
axis_dimension : str = 'xaxis' if x_or_y == 'x' else 'yaxis'
9991004
axis : XAxis = layout[axis_name]
10001005

10011006
if can_match_axis:
10021007
axis.matches = axis_label
10031008

10041009
if can_hide_ticks:
10051010
axis.showticklabels = False
1011+
1012+
if can_reassign_axis:
1013+
trace.trace_kwargs[axis_dimension] = axis_label
10061014

10071015
def columns_mode(rows : Tuple[int], columns : Tuple[int], trace_layer : int):
10081016
for column in columns:
@@ -1014,10 +1022,17 @@ def columns_mode(rows : Tuple[int], columns : Tuple[int], trace_layer : int):
10141022

10151023
# Set all of the values in the column
10161024
for row in rows:
1017-
can_match_axis : bool = (row != label_row)
1018-
can_hide_ticks : bool = can_match_axis and x_or_y == 'x' # Sharing column wise can only hide x-axis; still need all of the different y-axis across plots in the same columns
10191025

1020-
update_trace_axis(axis_label, row, column, trace_layer, can_hide_ticks, can_match_axis)
1026+
subplot_spec : Optional[SubplotSpec] = specs[row][column]
1027+
if subplot_spec is None:
1028+
continue
1029+
1030+
# NOTE: Axes sharing is turned off for having secondary_y, so instead of shared axes getting the same axis, they get unique ones; this is to prevent a bug since the left and right side axes are different axes (and shouldn't be the same)
1031+
can_reassign_axis : bool = (x_or_y == 'x' and not subplot_spec['secondary_y']) # Every subplot in the same column should share the same axis if in columns mode
1032+
can_match_axis : bool = (row != label_row)
1033+
can_hide_ticks : bool = can_match_axis and x_or_y == 'x' # Sharing column wise can only hide x-axis; still need all of the different y-axis across plots in the same columns
1034+
1035+
update_trace_axis(axis_label, row, column, trace_layer, can_reassign_axis, can_hide_ticks, can_match_axis)
10211036

10221037

10231038
def rows_mode(rows : Tuple[int], columns : Tuple[int], trace_layer : int):
@@ -1028,10 +1043,17 @@ def rows_mode(rows : Tuple[int], columns : Tuple[int], trace_layer : int):
10281043
axis_label, (_, label_column) = label_data
10291044

10301045
for column in columns:
1031-
can_match_axis : bool = (column != label_column)
1032-
can_hide_ticks : bool = can_match_axis and x_or_y == 'y' # Sharing row wise can only hide y-axis; still need all of the different x-axis across plots in the same row
10331046

1034-
update_trace_axis(axis_label, row, column, trace_layer, can_hide_ticks, can_match_axis)
1047+
subplot_spec : Optional[SubplotSpec] = specs[row][column]
1048+
if subplot_spec is None:
1049+
continue
1050+
1051+
# NOTE: Axes sharing is turned off for having secondary_y, so instead of shared axes getting the same axis, they get unique ones; this is to prevent a bug since the left and right side axes are different axes (and shouldn't be the same)
1052+
can_reassign_axis : bool = (x_or_y == 'y' and not subplot_spec['secondary_y'])
1053+
can_match_axis : bool = (column != label_column)
1054+
can_hide_ticks : bool = can_match_axis and x_or_y == 'y' # Sharing row wise can only hide y-axis; still need all of the different x-axis across plots in the same row
1055+
1056+
update_trace_axis(axis_label, row, column, trace_layer, can_reassign_axis, can_hide_ticks, can_match_axis)
10351057

10361058
def all_mode(rows : Tuple[int], columns : Tuple[int], trace_layer : int):
10371059
label_data = find_label_and_index(rows, columns, trace_layer)
@@ -1041,15 +1063,16 @@ def all_mode(rows : Tuple[int], columns : Tuple[int], trace_layer : int):
10411063

10421064
for row in rows:
10431065
for column in columns:
1044-
can_match_axis : bool = (row != label_row or column != label_column)
1045-
can_hide_ticks : bool = not ((row == label_row and x_or_y == 'x') or (column == label_column and x_or_y == 'y')) # The x-axis is across the first row, and the y-axis is along the first column
1046-
update_trace_axis(axis_label, row, column, trace_layer, can_hide_ticks, can_match_axis)
1066+
# spec : SubplotSpec = specs[row][column]
1067+
can_reassign_axis : bool = False # TODO:: Fix the all mode to allow for the hover to go across all of them as found in Issue #5427 [https://github.com/plotly/plotly.py/issues/5427]
1068+
can_match_axis : bool = (row != label_row or column != label_column)
1069+
can_hide_ticks : bool = not ((row == label_row and x_or_y == 'x') or (column == label_column and x_or_y == 'y')) # The x-axis is across the first row, and the y-axis is along the first column
1070+
update_trace_axis(axis_label, row, column, trace_layer, can_reassign_axis, can_hide_ticks, can_match_axis)
10471071

10481072

10491073
rows : Tuple[int] = tuple(range(row_count - 1, -1, -1)) if row_direction < 0 else tuple(range(row_count))
10501074
columns : Tuple[int] = tuple(range(column_count))
1051-
BASE_TRACE_LAYER = 0
1052-
SECOND_Y_LAYER = 1
1075+
10531076
match(shared, x_or_y):
10541077
case ('columns', _) | (True, 'x'): # If columns mode, or shared and x
10551078
columns_mode(rows, columns, BASE_TRACE_LAYER)

0 commit comments

Comments
 (0)