Skip to content

Commit 00eca47

Browse files
_add_annotation_like now accepts exclude_empty_subplots argument
but this has broken all of the tests.
1 parent 0c5126c commit 00eca47

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

Diff for: packages/python/plotly/codegen/figure.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ def add_{method_prefix}{singular_name}(self"""
541541
buffer,
542542
node.child_datatypes,
543543
prepend_extras=["arg"],
544-
append_extras=["row", "col", "secondary_y"],
544+
append_extras=["row", "col", "secondary_y", "exclude_empty_subplots"],
545545
)
546546

547547
prepend_extras = [
@@ -561,6 +561,10 @@ def add_{method_prefix}{singular_name}(self"""
561561
f"Subplot column for {singular_name}. If 'all', addresses all columns in the specified row(s).",
562562
),
563563
("secondary_y", f"Whether to add {singular_name} to secondary y-axis"),
564+
(
565+
"exclude_empty_subplots",
566+
f"If True, {singular_name} will not be added to subplots without traces.",
567+
),
564568
]
565569
add_docstring(
566570
buffer,
@@ -597,6 +601,7 @@ def add_{method_prefix}{singular_name}(self"""
597601
row=row,
598602
col=col,
599603
secondary_y=secondary_y,
604+
exclude_empty_subplots=exclude_empty_subplots,
600605
)"""
601606
)
602607

Diff for: packages/python/plotly/plotly/basedatatypes.py

+42-23
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,14 @@ def _select_annotations_like(
12081208
yield obj
12091209

12101210
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,
12121219
):
12131220
# Make sure we have both row and col or neither
12141221
if row is not None and col is None:
@@ -1234,6 +1241,7 @@ def _add_annotation_like(
12341241
row=r,
12351242
col=c,
12361243
secondary_y=secondary_y,
1244+
exclude_empty_subplots=exclude_empty_subplots,
12371245
)
12381246
return self
12391247

@@ -1279,6 +1287,12 @@ def _add_annotation_like(
12791287
else:
12801288
xaxis, yaxis = refs[0].layout_keys
12811289
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
12821296
# in case the user specified they wanted an axis to refer to the
12831297
# domain of that axis and not the data, append ' domain' to the
12841298
# computed axis accordingly
@@ -3628,9 +3642,7 @@ def _index_is(iterable, val):
36283642

36293643
return index_list[0]
36303644

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):
36343646
"""
36353647
Convert a shape drawn on a plot or a subplot into one whose yref or xref
36363648
ends with " domain" and has coordinates so that the shape will seem to
@@ -3658,23 +3670,6 @@ def _make_axis_spanning_layout_object(
36583670
"Bad direction: %s. Permissible values are 'vertical' and 'horizontal'."
36593671
% (direction,)
36603672
)
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
36783673
# set the ref to "<axis_id> domain" so that its size is based on the
36793674
# axis's size
36803675
shape[ref] += " domain"
@@ -3721,9 +3716,19 @@ def _process_multiple_axis_spanning_shapes(
37213716
augmented_annotation = shapeannotation.axis_spanning_shape_annotation(
37223717
annotation, shape_type, shape_args, annotation_kwargs
37233718
)
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+
)
37253725
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+
)
37273732
# update xref and yref for the new shapes and annotations
37283733
for layout_obj, n_layout_objs_before in zip(
37293734
["shapes", "annotations"], [n_shapes_before, n_annotations_before]
@@ -3825,6 +3830,20 @@ def _has_subplots(self):
38253830
single plot and so this returns False. """
38263831
return self._grid_ref is not None
38273832

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+
38283847

38293848
class BasePlotlyType(object):
38303849
"""

0 commit comments

Comments
 (0)