@@ -3479,7 +3479,7 @@ def _index_is(iterable, val):
3479
3479
3480
3480
return index_list [0 ]
3481
3481
3482
- def _make_paper_spanning_shape (self , direction , shape ):
3482
+ def _make_paper_spanning_shape (self , direction , shape , none_if_no_trace = True ):
3483
3483
"""
3484
3484
Convert a shape drawn on a plot or a subplot into one whose yref or
3485
3485
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):
3513
3513
shape [axis + "0" ], shape [axis + "1" ] = domain
3514
3514
except KeyError as e :
3515
3515
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
3516
3525
shape [ref ] = "paper"
3517
3526
return shape
3518
3527
@@ -3530,10 +3539,13 @@ def _process_multiple_paper_spanning_shapes(
3530
3539
self .add_shape (row = row , col = col , ** shape_args , ** kwargs )
3531
3540
n_shapes_after = len (self .layout ["shapes" ])
3532
3541
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
+ )
3537
3549
)
3538
3550
self .layout ["shapes" ] = self .layout ["shapes" ][:n_shapes_before ] + new_shapes
3539
3551
0 commit comments