@@ -1208,7 +1208,14 @@ def _select_annotations_like(
1208
1208
yield obj
1209
1209
1210
1210
def _add_annotation_like (
1211
- self , prop_singular , prop_plural , new_obj , row = None , col = None , secondary_y = None
1211
+ self ,
1212
+ prop_singular ,
1213
+ prop_plural ,
1214
+ new_obj ,
1215
+ row = None ,
1216
+ col = None ,
1217
+ secondary_y = None ,
1218
+ exclude_empty_subplots = False ,
1212
1219
):
1213
1220
# Make sure we have both row and col or neither
1214
1221
if row is not None and col is None :
@@ -1234,6 +1241,7 @@ def _add_annotation_like(
1234
1241
row = r ,
1235
1242
col = c ,
1236
1243
secondary_y = secondary_y ,
1244
+ exclude_empty_subplots = exclude_empty_subplots ,
1237
1245
)
1238
1246
return self
1239
1247
@@ -1279,6 +1287,12 @@ def _add_annotation_like(
1279
1287
else :
1280
1288
xaxis , yaxis = refs [0 ].layout_keys
1281
1289
xref , yref = xaxis .replace ("axis" , "" ), yaxis .replace ("axis" , "" )
1290
+ # if exclude_empty_subplots is True, check to see if subplot is
1291
+ # empty and return if it is
1292
+ if exclude_empty_subplots and (
1293
+ not self ._subplot_contains_trace (xref , yref )
1294
+ ):
1295
+ return self
1282
1296
# in case the user specified they wanted an axis to refer to the
1283
1297
# domain of that axis and not the data, append ' domain' to the
1284
1298
# computed axis accordingly
@@ -3628,9 +3642,7 @@ def _index_is(iterable, val):
3628
3642
3629
3643
return index_list [0 ]
3630
3644
3631
- def _make_axis_spanning_layout_object (
3632
- self , direction , shape , none_if_no_trace = True
3633
- ):
3645
+ def _make_axis_spanning_layout_object (self , direction , shape ):
3634
3646
"""
3635
3647
Convert a shape drawn on a plot or a subplot into one whose yref or xref
3636
3648
ends with " domain" and has coordinates so that the shape will seem to
@@ -3658,23 +3670,6 @@ def _make_axis_spanning_layout_object(
3658
3670
"Bad direction: %s. Permissible values are 'vertical' and 'horizontal'."
3659
3671
% (direction ,)
3660
3672
)
3661
- if none_if_no_trace :
3662
- # iterate through all the traces and check to see if one with the
3663
- # same xref and yref pair is there, if not, we return None (we don't
3664
- # want to draw a shape if there is no trace)
3665
- if not any (
3666
- t == (shape ["xref" ], shape ["yref" ])
3667
- for t in [
3668
- # if a trace exists but has no xaxis or yaxis keys, then it
3669
- # is plotted with xaxis 'x' and yaxis 'y'
3670
- (
3671
- "x" if d ["xaxis" ] is None else d ["xaxis" ],
3672
- "y" if d ["yaxis" ] is None else d ["yaxis" ],
3673
- )
3674
- for d in self .data
3675
- ]
3676
- ):
3677
- return None
3678
3673
# set the ref to "<axis_id> domain" so that its size is based on the
3679
3674
# axis's size
3680
3675
shape [ref ] += " domain"
@@ -3721,9 +3716,19 @@ def _process_multiple_axis_spanning_shapes(
3721
3716
augmented_annotation = shapeannotation .axis_spanning_shape_annotation (
3722
3717
annotation , shape_type , shape_args , annotation_kwargs
3723
3718
)
3724
- self .add_shape (row = row , col = col , ** _combine_dicts ([shape_args , shape_kwargs ]))
3719
+ self .add_shape (
3720
+ row = row ,
3721
+ col = col ,
3722
+ ** _combine_dicts ([shape_args , shape_kwargs ]),
3723
+ exclude_empty_subplots = exclude_empty_subplots
3724
+ )
3725
3725
if augmented_annotation is not None :
3726
- self .add_annotation (augmented_annotation , row = row , col = col )
3726
+ self .add_annotation (
3727
+ augmented_annotation ,
3728
+ row = row ,
3729
+ col = col ,
3730
+ exclude_empty_subplots = exclude_empty_subplots ,
3731
+ )
3727
3732
# update xref and yref for the new shapes and annotations
3728
3733
for layout_obj , n_layout_objs_before in zip (
3729
3734
["shapes" , "annotations" ], [n_shapes_before , n_annotations_before ]
@@ -3825,6 +3830,20 @@ def _has_subplots(self):
3825
3830
single plot and so this returns False. """
3826
3831
return self ._grid_ref is not None
3827
3832
3833
+ def _subplot_contains_trace (self , xref , yref ):
3834
+ return any (
3835
+ t == (xref , yref )
3836
+ for t in [
3837
+ # if a trace exists but has no xaxis or yaxis keys, then it
3838
+ # is plotted with xaxis 'x' and yaxis 'y'
3839
+ (
3840
+ "x" if d ["xaxis" ] is None else d ["xaxis" ],
3841
+ "y" if d ["yaxis" ] is None else d ["yaxis" ],
3842
+ )
3843
+ for d in self .data
3844
+ ]
3845
+ )
3846
+
3828
3847
3829
3848
class BasePlotlyType (object ):
3830
3849
"""
0 commit comments