Skip to content

Commit eb21739

Browse files
Anything truthy passed to exclude_empty_subplots works
That means that truthy values exclude subplots containing absolutely nothing (but not those containing shapes, but no traces, say) and anything falsey adds to all subplots.
1 parent e4fe7cf commit eb21739

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,9 @@ def _add_annotation_like(
13021302
# if exclude_empty_subplots is True, check to see if subplot is
13031303
# empty and return if it is
13041304
if exclude_empty_subplots and (
1305-
not self._subplot_not_empty(xref, yref, selector=exclude_empty_subplots)
1305+
not self._subplot_not_empty(
1306+
xref, yref, selector=bool(exclude_empty_subplots)
1307+
)
13061308
):
13071309
return self
13081310
# in case the user specified they wanted an axis to refer to the
@@ -1994,7 +1996,7 @@ def add_traces(
19941996
data = list(
19951997
filter(
19961998
lambda trace: self._subplot_not_empty(
1997-
trace["xaxis"], trace["yaxis"], exclude_empty_subplots
1999+
trace["xaxis"], trace["yaxis"], bool(exclude_empty_subplots)
19982000
),
19992001
data,
20002002
)

packages/python/plotly/plotly/tests/test_core/test_figure_messages/test_add_traces.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,43 @@ def test_add_trace_no_exclude_empty_subplots():
120120
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[5, 1, 2]), row=1, col=1)
121121
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 1, -7]), row=2, col=2)
122122
# Add traces with exclude_empty_subplots set to true and make sure this
123-
# doesn't add to traces that don't already have data
123+
# even adds to traces that don't already have data
124+
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[0, 1, -1]), row="all", col="all")
125+
assert len(fig.data) == 6
126+
assert fig.data[2]["xaxis"] == "x" and fig.data[2]["yaxis"] == "y"
127+
assert fig.data[3]["xaxis"] == "x2" and fig.data[3]["yaxis"] == "y2"
128+
assert fig.data[4]["xaxis"] == "x3" and fig.data[4]["yaxis"] == "y3"
129+
assert fig.data[5]["xaxis"] == "x4" and fig.data[5]["yaxis"] == "y4"
130+
131+
132+
def test_add_trace_exclude_totally_empty_subplots():
133+
# Add traces
134+
fig = make_subplots(2, 2)
135+
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[5, 1, 2]), row=1, col=1)
136+
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 1, -7]), row=2, col=2)
137+
fig.add_shape(dict(type="rect", x0=0, x1=1, y0=0, y1=1), row=1, col=2)
138+
# Add traces with exclude_empty_subplots set to true and make sure this
139+
# doesn't add to traces that don't already have data or layout objects
140+
fig.add_trace(
141+
go.Scatter(x=[1, 2, 3], y=[0, 1, -1]),
142+
row="all",
143+
col="all",
144+
exclude_empty_subplots=["anything", "truthy"],
145+
)
146+
assert len(fig.data) == 5
147+
assert fig.data[2]["xaxis"] == "x" and fig.data[2]["yaxis"] == "y"
148+
assert fig.data[3]["xaxis"] == "x2" and fig.data[3]["yaxis"] == "y2"
149+
assert fig.data[4]["xaxis"] == "x4" and fig.data[4]["yaxis"] == "y4"
150+
151+
152+
def test_add_trace_no_exclude_totally_empty_subplots():
153+
# Add traces
154+
fig = make_subplots(2, 2)
155+
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[5, 1, 2]), row=1, col=1)
156+
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 1, -7]), row=2, col=2)
157+
fig.add_shape(dict(type="rect", x0=0, x1=1, y0=0, y1=1), row=1, col=2)
158+
# Add traces with exclude_empty_subplots set to true and make sure this
159+
# even adds to traces that don't already have data or layout objects
124160
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[0, 1, -1]), row="all", col="all")
125161
assert len(fig.data) == 6
126162
assert fig.data[2]["xaxis"] == "x" and fig.data[2]["yaxis"] == "y"

packages/python/plotly/plotly/tests/test_core/test_update_objects/test_update_annotations.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,51 +270,78 @@ def test_image_attributes(self):
270270

271271

272272
def test_exclude_empty_subplots():
273-
for k, fun, d in [
273+
for k, fun, d, fun2, d2 in [
274274
(
275275
"shapes",
276276
go.Figure.add_shape,
277277
dict(type="rect", x0=1.5, x1=2.5, y0=3.5, y1=4.5),
278+
# add a different type to make the check easier (otherwise we might
279+
# mix up the objects added before and after fun was run)
280+
go.Figure.add_annotation,
281+
dict(x=1, y=2, text="A"),
282+
),
283+
(
284+
"annotations",
285+
go.Figure.add_annotation,
286+
dict(x=1, y=2, text="A"),
287+
go.Figure.add_layout_image,
288+
dict(x=3, y=4, sizex=2, sizey=3, source="test"),
278289
),
279-
("annotations", go.Figure.add_annotation, dict(x=1, y=2, text="A")),
280290
(
281291
"images",
282292
go.Figure.add_layout_image,
283293
dict(x=3, y=4, sizex=2, sizey=3, source="test"),
294+
go.Figure.add_shape,
295+
dict(type="rect", x0=1.5, x1=2.5, y0=3.5, y1=4.5),
284296
),
285297
]:
286298
# make a figure where not all the subplots are populated
287299
fig = make_subplots(2, 2)
288300
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[5, 1, 2]), row=1, col=1)
289301
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 1, -7]), row=2, col=2)
302+
fun2(fig, d2, row=1, col=2)
290303
# add a thing to all subplots but make sure it only goes on the
291-
# plots without data
292-
fun(fig, d, row="all", col="all", exclude_empty_subplots=True)
293-
assert len(fig.layout[k]) == 2
304+
# plots without data or layout objects
305+
fun(fig, d, row="all", col="all", exclude_empty_subplots="anything_truthy")
306+
assert len(fig.layout[k]) == 3
294307
assert fig.layout[k][0]["xref"] == "x" and fig.layout[k][0]["yref"] == "y"
295-
assert fig.layout[k][1]["xref"] == "x4" and fig.layout[k][1]["yref"] == "y4"
308+
assert fig.layout[k][1]["xref"] == "x2" and fig.layout[k][1]["yref"] == "y2"
309+
assert fig.layout[k][2]["xref"] == "x4" and fig.layout[k][2]["yref"] == "y4"
296310

297311

298312
def test_no_exclude_empty_subplots():
299-
for k, fun, d in [
313+
for k, fun, d, fun2, d2 in [
300314
(
301315
"shapes",
302316
go.Figure.add_shape,
303317
dict(type="rect", x0=1.5, x1=2.5, y0=3.5, y1=4.5),
318+
# add a different type to make the check easier (otherwise we might
319+
# mix up the objects added before and after fun was run)
320+
go.Figure.add_annotation,
321+
dict(x=1, y=2, text="A"),
322+
),
323+
(
324+
"annotations",
325+
go.Figure.add_annotation,
326+
dict(x=1, y=2, text="A"),
327+
go.Figure.add_layout_image,
328+
dict(x=3, y=4, sizex=2, sizey=3, source="test"),
304329
),
305-
("annotations", go.Figure.add_annotation, dict(x=1, y=2, text="A")),
306330
(
307331
"images",
308332
go.Figure.add_layout_image,
309333
dict(x=3, y=4, sizex=2, sizey=3, source="test"),
334+
go.Figure.add_shape,
335+
dict(type="rect", x0=1.5, x1=2.5, y0=3.5, y1=4.5),
310336
),
311337
]:
312338
# make a figure where not all the subplots are populated
313339
fig = make_subplots(2, 2)
314340
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[5, 1, 2]), row=1, col=1)
315341
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 1, -7]), row=2, col=2)
316-
# add a thing to all subplots and make sure it even goes on the
317-
# plots without data
342+
fun2(fig, d2, row=1, col=2)
343+
# add a thing to all subplots but make sure it only goes on the
344+
# plots without data or layout objects
318345
fun(fig, d, row="all", col="all", exclude_empty_subplots=False)
319346
assert len(fig.layout[k]) == 4
320347
assert fig.layout[k][0]["xref"] == "x" and fig.layout[k][0]["yref"] == "y"

0 commit comments

Comments
 (0)