Link to issue
Closes #5136
Description of change
Change add_hline, add_vline, add_hrect and add_vrect to ignore exclude_empty_subplots when row and col select a single subplot, as suggested here.
Demo
from plotly.subplots import make_subplots
fig = make_subplots(rows=1, cols=1)
fig.add_hline(y=0.5)
print(len(fig.layout.shapes))
fig = make_subplots(rows=2, cols=2)
fig.add_hline(y=0.5, row=2, col=1)
print(len(fig.layout.shapes))
fig = make_subplots(rows=2, cols=2)
fig.add_hline(y=0.5)
print(len(fig.layout.shapes))
On main this prints 0 0 0. With this change it prints 1 1 0.
Testing strategy
New tests in test_axis_span_shapes.py run all four methods against a single subplot, an explicit row/col, col="all" on a one-column grid and list selections, plus a grid where the other cell is None, an empty secondary axis and an empty facet. These 19 fail on main and pass with this change. Two more check that several empty subplots are still skipped, and one that an out-of-range row keeps its IndexError.
tests/test_core plus test_px, test_figure_factory, test_subplots and test_autoshapes under tests/test_optional have the same failures as main. ruff check and ruff format --check are clean on the changed files, and codegen output matches the committed _figure.py and _figurewidget.py.
Additional information (optional)
Before, the shape was dropped without warning if the selected subplot was empty, even when it was the only one or was requested by row and col. The workaround was to pass exclude_empty_subplots=False. Selections covering several subplots still skip empty ones.
An explicit exclude_empty_subplots=True is also ignored for a single subplot, because it can't be told apart from the default. Keeping it would mean changing the default to a sentinel.
An explicit row/col on a blank facet_col_wrap cell now draws there too.
I used Claude Code to help write this change. I've reviewed and tested it myself.
Guidelines