Skip to content

Commit 34c7f36

Browse files
authored
Merge pull request #5871 from LukeTheoJohnson/fix-add-hline-empty-subplots
Keep axis-spanning shapes on an empty subplot selected by row and col
2 parents 48b626d + e17212c commit 34c7f36

5 files changed

Lines changed: 88 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2626
- Fix Plotly Express mutating lists passed to the `x` or `y` arguments in wide mode [[#5727](https://github.com/plotly/plotly.py/pull/5727)], with thanks to @cpruijsen for the contribution!
2727
- Fix `px.sunburst`, `px.treemap` and `px.icicle` listing sectors in a different order on every run when `path` is used with a Polars DataFrame; sectors now follow their order of first appearance for all dataframe backends [[#5766](https://github.com/plotly/plotly.py/pull/5766)], with thanks to @Irahan2 for the contribution!
2828
- Fix `trendline_scope="overall"` drawing the overall trendline on the `marginal_x`/`marginal_y` subplots as well as the main plot; it is now drawn only on the main subplots [[#5780](https://github.com/plotly/plotly.py/pull/5780)], with thanks to @jayzhou2309 for the contribution!
29+
- Fix `add_hline`, `add_vline`, `add_hrect` and `add_vrect` dropping the shape on an empty subplot when `row` and `col` select a single subplot [[#5871](https://github.com/plotly/plotly.py/pull/5871)], with thanks to @LukeTheoJohnson for the contribution!
2930
- Fix `mpl_to_plotly` conversion of matplotlib contour lines and line collections: close contour rings ending with `Z` codes, support dash styles, convert date x-axes, separate disjoint subpaths with `None` separators, hide line collection traces from the legend by default, and group consecutive same-style lines into single traces [[#5770](https://github.com/plotly/plotly.py/pull/5770)], with thanks to @robertoffmoura for the contribution!
3031
- Fix `plotly.io.write_images` ignoring `layout.width`/`layout.height` (and the template's) and exporting every figure at the default 700x500 size; it now uses the same width/height priority as `write_image` and `to_image` [[#5784](https://github.com/plotly/plotly.py/pull/5784)], with thanks to @breken-ai for the contribution!
3132
- Fix color utilities mutating the list of colors passed to them [[#5836](https://github.com/plotly/plotly.py/pull/5836)], with thanks to @Enesuygurs for the contribution!

‎plotly/basedatatypes.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def _axis_spanning_shapes_docstr(shape_type):
339339
docstr += """
340340
exclude_empty_subplots: Boolean
341341
If True (default) do not place the shape on subplots that have no data
342-
plotted on them.
342+
plotted on them. Ignored when row and col select a single subplot.
343343
row: None, int or 'all'
344344
Subplot row for shape indexed starting at 1. If 'all', addresses all rows in
345345
the specified column(s). If both row and col are None, addresses the
@@ -4003,6 +4003,15 @@ def _process_multiple_axis_spanning_shapes(
40034003
augmented_annotation = shapeannotation.axis_spanning_shape_annotation(
40044004
annotation, shape_type, shape_args, annotation_kwargs
40054005
)
4006+
if (
4007+
row is not None
4008+
and col is not None
4009+
and (
4010+
not _is_select_subplot_coordinates_arg(row, col)
4011+
or len(self._select_subplot_coordinates(row, col)) == 1
4012+
)
4013+
):
4014+
exclude_empty_subplots = False
40064015
self.add_shape(
40074016
row=row,
40084017
col=col,

‎plotly/graph_objs/_figure.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ def add_vline(
441441
A number representing the x coordinate of the vertical line.
442442
exclude_empty_subplots: Boolean
443443
If True (default) do not place the shape on subplots that have no data
444-
plotted on them.
444+
plotted on them. Ignored when row and col select a single subplot.
445445
row: None, int or 'all'
446446
Subplot row for shape indexed starting at 1. If 'all', addresses all rows in
447447
the specified column(s). If both row and col are None, addresses the
@@ -493,7 +493,7 @@ def add_hline(
493493
A number representing the y coordinate of the horizontal line.
494494
exclude_empty_subplots: Boolean
495495
If True (default) do not place the shape on subplots that have no data
496-
plotted on them.
496+
plotted on them. Ignored when row and col select a single subplot.
497497
row: None, int or 'all'
498498
Subplot row for shape indexed starting at 1. If 'all', addresses all rows in
499499
the specified column(s). If both row and col are None, addresses the
@@ -548,7 +548,7 @@ def add_vrect(
548548
A number representing the x coordinate of the other side of the rectangle.
549549
exclude_empty_subplots: Boolean
550550
If True (default) do not place the shape on subplots that have no data
551-
plotted on them.
551+
plotted on them. Ignored when row and col select a single subplot.
552552
row: None, int or 'all'
553553
Subplot row for shape indexed starting at 1. If 'all', addresses all rows in
554554
the specified column(s). If both row and col are None, addresses the
@@ -604,7 +604,7 @@ def add_hrect(
604604
A number representing the y coordinate of the other side of the rectangle.
605605
exclude_empty_subplots: Boolean
606606
If True (default) do not place the shape on subplots that have no data
607-
plotted on them.
607+
plotted on them. Ignored when row and col select a single subplot.
608608
row: None, int or 'all'
609609
Subplot row for shape indexed starting at 1. If 'all', addresses all rows in
610610
the specified column(s). If both row and col are None, addresses the

‎plotly/graph_objs/_figurewidget.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ def add_vline(
441441
A number representing the x coordinate of the vertical line.
442442
exclude_empty_subplots: Boolean
443443
If True (default) do not place the shape on subplots that have no data
444-
plotted on them.
444+
plotted on them. Ignored when row and col select a single subplot.
445445
row: None, int or 'all'
446446
Subplot row for shape indexed starting at 1. If 'all', addresses all rows in
447447
the specified column(s). If both row and col are None, addresses the
@@ -493,7 +493,7 @@ def add_hline(
493493
A number representing the y coordinate of the horizontal line.
494494
exclude_empty_subplots: Boolean
495495
If True (default) do not place the shape on subplots that have no data
496-
plotted on them.
496+
plotted on them. Ignored when row and col select a single subplot.
497497
row: None, int or 'all'
498498
Subplot row for shape indexed starting at 1. If 'all', addresses all rows in
499499
the specified column(s). If both row and col are None, addresses the
@@ -548,7 +548,7 @@ def add_vrect(
548548
A number representing the x coordinate of the other side of the rectangle.
549549
exclude_empty_subplots: Boolean
550550
If True (default) do not place the shape on subplots that have no data
551-
plotted on them.
551+
plotted on them. Ignored when row and col select a single subplot.
552552
row: None, int or 'all'
553553
Subplot row for shape indexed starting at 1. If 'all', addresses all rows in
554554
the specified column(s). If both row and col are None, addresses the
@@ -604,7 +604,7 @@ def add_hrect(
604604
A number representing the y coordinate of the other side of the rectangle.
605605
exclude_empty_subplots: Boolean
606606
If True (default) do not place the shape on subplots that have no data
607-
plotted on them.
607+
plotted on them. Ignored when row and col select a single subplot.
608608
row: None, int or 'all'
609609
Subplot row for shape indexed starting at 1. If 'all', addresses all rows in
610610
the specified column(s). If both row and col are None, addresses the

‎tests/test_optional/test_autoshapes/test_axis_span_shapes.py‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,72 @@ def _check_figure_shapes_custom_sized(test_input, expected, fig):
473473
)
474474
def test_custom_sized_subplots(test_input, expected, custom_sized_subplots):
475475
_check_figure_shapes_custom_sized(test_input, expected, custom_sized_subplots)
476+
477+
478+
@pytest.mark.parametrize(
479+
"add_shape,kwargs",
480+
[
481+
(go.Figure.add_hline, dict(y=1)),
482+
(go.Figure.add_vline, dict(x=1)),
483+
(go.Figure.add_hrect, dict(y0=1, y1=2)),
484+
(go.Figure.add_vrect, dict(x0=1, x1=2)),
485+
],
486+
)
487+
@pytest.mark.parametrize(
488+
"rows,cols,selection,expected_xref",
489+
[
490+
(1, 1, dict(), "x"),
491+
(2, 2, dict(row=2, col=1), "x3"),
492+
(2, 1, dict(row=2, col="all"), "x2"),
493+
(2, 2, dict(row=[1], col=[2]), "x2"),
494+
],
495+
)
496+
def test_single_empty_subplot_gets_shape(
497+
add_shape, kwargs, rows, cols, selection, expected_xref
498+
):
499+
fig = make_subplots(rows=rows, cols=cols)
500+
add_shape(fig, annotation_text="A", **kwargs, **selection)
501+
assert len(fig.layout.shapes) == 1
502+
assert fig.layout.shapes[0].xref.split()[0] == expected_xref
503+
assert len(fig.layout.annotations) == 1
504+
505+
506+
def test_single_subplot_with_none_cell_gets_shape():
507+
fig = make_subplots(rows=1, cols=2, specs=[[{}, None]])
508+
fig.add_hline(y=1)
509+
assert len(fig.layout.shapes) == 1
510+
511+
512+
@pytest.mark.parametrize("selection", [dict(), dict(row=1, col="all")])
513+
def test_multiple_empty_subplots_are_still_excluded(selection):
514+
fig = make_subplots(rows=1, cols=2)
515+
fig.add_hline(y=1, **selection)
516+
assert len(fig.layout.shapes) == 0
517+
518+
519+
def test_out_of_range_row_keeps_its_error():
520+
fig = make_subplots(rows=1, cols=1)
521+
with pytest.raises(IndexError, match="row index 2 out-of-bounds"):
522+
fig.add_hline(y=1, row=2, col=1)
523+
524+
525+
def test_empty_secondary_y_gets_shape():
526+
fig = make_subplots(rows=1, cols=1, specs=[[{"secondary_y": True}]])
527+
fig.add_hline(y=1, row=1, col=1, secondary_y=True)
528+
assert fig.layout.shapes[0].yref == "y2"
529+
530+
531+
def test_explicit_empty_facet_gets_shape(subplot_empty_traces_fig_fixture):
532+
fig = subplot_empty_traces_fig_fixture
533+
empty = next(
534+
(r, c)
535+
for r, c in fig._get_subplot_coordinates()
536+
if not fig._subplot_not_empty(*_xy_refs(fig, r, c))
537+
)
538+
fig.add_hline(y=1, row=empty[0], col=empty[1])
539+
assert len(fig.layout.shapes) == 1
540+
541+
542+
def _xy_refs(fig, row, col):
543+
xaxis, yaxis = fig._grid_ref[row - 1][col - 1][0].layout_keys
544+
return xaxis.replace("axis", ""), yaxis.replace("axis", "")

0 commit comments

Comments
 (0)