Skip to content

Commit 545e8ba

Browse files
committed
Support row or col in select_traces (not only supporting both)
1 parent 90f119d commit 545e8ba

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

Diff for: plotly/basedatatypes.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -666,26 +666,36 @@ def select_traces(self, selector=None, row=None, col=None):
666666
if not selector:
667667
selector = {}
668668

669-
if row is not None and col is not None:
669+
if row is not None or col is not None:
670670
_validate_v4_subplots('select_traces')
671671
grid_ref = self._validate_get_grid_ref()
672-
grid_subplot_ref = grid_ref[row-1][col-1]
673672
filter_by_subplot = True
673+
674+
if row is None:
675+
# All rows for column
676+
grid_subplot_refs = [ref_row[col-1] for ref_row in grid_ref]
677+
elif col is None:
678+
# All columns for row
679+
grid_subplot_refs = grid_ref[row-1]
680+
else:
681+
# Single grid cell
682+
grid_subplot_refs = [grid_ref[row-1][col-1]]
683+
674684
else:
675685
filter_by_subplot = False
676-
grid_subplot_ref = None
686+
grid_subplot_refs = None
677687

678688
return self._perform_select_traces(
679-
filter_by_subplot, grid_subplot_ref, selector)
689+
filter_by_subplot, grid_subplot_refs, selector)
680690

681691
def _perform_select_traces(
682-
self, filter_by_subplot, grid_subplot_ref, selector):
692+
self, filter_by_subplot, grid_subplot_refs, selector):
683693

684694
for trace in self.data:
685695
# Filter by subplot
686696
if filter_by_subplot:
687697
trace_subplot_ref = _get_subplot_ref_for_trace(trace)
688-
if grid_subplot_ref != trace_subplot_ref:
698+
if trace_subplot_ref not in grid_subplot_refs:
689699
continue
690700

691701
# Filter by selector

Diff for: plotly/tests/test_core/test_update_traces/test_update_traces.py renamed to plotly/tests/test_core/test_update_objects/test_update_traces.py

+32
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,22 @@ def test_select_by_type(self):
158158
[], selector={'type': 'pie'}, test_no_grid=True)
159159

160160
def test_select_by_grid(self):
161+
# Row and column
161162
self.assert_select_traces([0, 1], row=1, col=1)
162163
self.assert_select_traces([2, 3], row=2, col=1)
163164
self.assert_select_traces([4, 5], row=1, col=2)
164165
self.assert_select_traces([6, 7], row=2, col=2)
165166
self.assert_select_traces([8], row=3, col=1)
166167

168+
# Row only
169+
self.assert_select_traces([0, 1, 4, 5], row=1)
170+
self.assert_select_traces([2, 3, 6, 7], row=2)
171+
self.assert_select_traces([8], row=3)
172+
173+
# Col only
174+
self.assert_select_traces([0, 1, 2, 3, 8], col=1)
175+
self.assert_select_traces([4, 5, 6, 7], col=2)
176+
167177
def test_select_by_property_across_trace_types(self):
168178
self.assert_select_traces(
169179
[0, 4, 6], selector={'mode': 'markers'}, test_no_grid=True)
@@ -322,3 +332,25 @@ def test_update_traces_by_type(self):
322332
{'hoverinfo': 'label+percent'},
323333
[], selector={'type': 'pie'}
324334
)
335+
336+
def test_update_traces_by_grid_and_selector(self):
337+
self.assert_update_traces(
338+
{'marker.size': 5},
339+
[4, 6],
340+
selector={'marker.color': 'green'},
341+
col=2
342+
)
343+
344+
self.assert_update_traces(
345+
{'marker.size': 6},
346+
[0, 4],
347+
selector={'marker.color': 'green'},
348+
row=1
349+
)
350+
351+
self.assert_update_traces(
352+
{'marker.size': 6},
353+
[6],
354+
selector={'marker.color': 'green'},
355+
row=2, col=2
356+
)

0 commit comments

Comments
 (0)