Skip to content

Commit be05c07

Browse files
Shape is not added to subplot that contains no trace
Eventually we could expose the none_if_no_trace option to make it optional.
1 parent 1d74ee7 commit be05c07

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3479,7 +3479,7 @@ def _index_is(iterable, val):
34793479

34803480
return index_list[0]
34813481

3482-
def _make_paper_spanning_shape(self, direction, shape):
3482+
def _make_paper_spanning_shape(self, direction, shape, none_if_no_trace=True):
34833483
"""
34843484
Convert a shape drawn on a plot or a subplot into one whose yref or
34853485
xref is 'paper' so that the shape will seem to extend infinitely in that
@@ -3513,6 +3513,15 @@ def _make_paper_spanning_shape(self, direction, shape):
35133513
shape[axis + "0"], shape[axis + "1"] = domain
35143514
except KeyError as e:
35153515
raise e("Shape does not support paper spanning.")
3516+
if none_if_no_trace:
3517+
# iterate through all the traces and check to see if one with the
3518+
# same xref and yref pair is there, if not, we return None (we don't
3519+
# want to draw a shape if there is no trace)
3520+
if not any(
3521+
t == (shape["xref"], shape["yref"])
3522+
for t in [(d["xaxis"], d["yaxis"]) for d in self.data]
3523+
):
3524+
return None
35163525
shape[ref] = "paper"
35173526
return shape
35183527

@@ -3530,10 +3539,13 @@ def _process_multiple_paper_spanning_shapes(
35303539
self.add_shape(row=row, col=col, **shape_args, **kwargs)
35313540
n_shapes_after = len(self.layout["shapes"])
35323541
new_shapes = tuple(
3533-
[
3534-
self._make_paper_spanning_shape(direction, self.layout["shapes"][n])
3535-
for n in range(n_shapes_before, n_shapes_after)
3536-
]
3542+
filter(
3543+
lambda x: x is not None,
3544+
[
3545+
self._make_paper_spanning_shape(direction, self.layout["shapes"][n])
3546+
for n in range(n_shapes_before, n_shapes_after)
3547+
],
3548+
)
35373549
)
35383550
self.layout["shapes"] = self.layout["shapes"][:n_shapes_before] + new_shapes
35393551

0 commit comments

Comments
 (0)