Summary
In PlotAccessor.show, elements_to_be_rendered is computed from a leaked loop variable, so for multi-coordinate-system plots it reflects only the last coordinate system instead of all of them.
Details
src/spatialdata_plot/pl/basic.py:1468-1478:
for cs in coordinate_systems:
if cs not in sdata.coordinate_systems:
raise ValueError(...)
# ...
elements_to_be_rendered = _get_elements_to_be_rendered(render_cmds, cs_index, cs)
The for cs in coordinate_systems: loop is validation-only, but it leaves cs bound to the last coordinate system. The subsequent call passes that leftover cs to _get_elements_to_be_rendered, which is genuinely CS-dependent — utils.py does cs_index.loc[cs] and returns the elements present in that one CS.
That element set is then fed to _get_valid_cs(..., elements=elements_to_be_rendered), which prunes which coordinate systems survive. So when a plot spans multiple coordinate systems and elements are distributed across them, the pruning is driven by only the last CS's contents and can drop a coordinate system that should have rendered.
Single-coordinate-system plots (the common path) are accidentally correct, which is why this isn't caught today.
Suggested fix
Compute the union of elements across all coordinate_systems before _get_valid_cs, or move the _get_elements_to_be_rendered call into the actual per-panel loop (which already iterates coordinate systems later).
Regression test
Add a multi-coordinate-system test where elements live in different coordinate systems and assert all expected systems render (would fail before the fix, pass after).
Found during a maintainability/refactor audit of main.
Summary
In
PlotAccessor.show,elements_to_be_renderedis computed from a leaked loop variable, so for multi-coordinate-system plots it reflects only the last coordinate system instead of all of them.Details
src/spatialdata_plot/pl/basic.py:1468-1478:The
for cs in coordinate_systems:loop is validation-only, but it leavescsbound to the last coordinate system. The subsequent call passes that leftovercsto_get_elements_to_be_rendered, which is genuinely CS-dependent —utils.pydoescs_index.loc[cs]and returns the elements present in that one CS.That element set is then fed to
_get_valid_cs(..., elements=elements_to_be_rendered), which prunes which coordinate systems survive. So when a plot spans multiple coordinate systems and elements are distributed across them, the pruning is driven by only the last CS's contents and can drop a coordinate system that should have rendered.Single-coordinate-system plots (the common path) are accidentally correct, which is why this isn't caught today.
Suggested fix
Compute the union of elements across all
coordinate_systemsbefore_get_valid_cs, or move the_get_elements_to_be_renderedcall into the actual per-panel loop (which already iterates coordinate systems later).Regression test
Add a multi-coordinate-system test where elements live in different coordinate systems and assert all expected systems render (would fail before the fix, pass after).
Found during a maintainability/refactor audit of
main.