Skip to content

render_points(color="x") crashes when coloring by a spatial coordinate column #613

@timtreis

Description

@timtreis

render_points(color="x") crashes when coloring by a spatial coordinate column

Description

Coloring points by their x or y coordinate — a common use case for visualizing spatial gradients or verifying registration — crashes with ValueError: Length mismatch. The root cause is that _render_points appends col_for_color to the coordinate selection list without first checking whether the column is already present. When color="x", the resulting list becomes ["x", "y", "x"], producing a duplicate column. PointsModel.parse then receives a 3-column frame but assigns 2 column names, causing the crash.

Environment

spatialdata-plot: 0.3.4.dev (main, 5cfedc7)
spatialdata: 0.5.0
Python: 3.13

Minimal Reproducible Example

import matplotlib; matplotlib.use("Agg")
import matplotlib.pyplot as plt
import pandas as pd
import dask; dask.config.set({"dataframe.query-planning": False})
import spatialdata as sd
from spatialdata.models import PointsModel
import spatialdata_plot

pts = PointsModel.parse(
    pd.DataFrame({"x": [1., 2., 3., 4., 5.], "y": [1., 2., 3., 4., 5.]}),
    coordinates={"x": "x", "y": "y"},
)
sdata = sd.SpatialData(points={"pts": pts})

fig, ax = plt.subplots()
sdata.pl.render_points("pts", color="x").pl.show(ax=ax)

Expected vs. Actual

Expected: Renders points colored by their x-coordinate. This is a valid use case for spatial gradient visualization.

Actual: ValueError: Length mismatch: Expected axis has 3 elements, new values have 2 elements

Fix Sketch

At render.py:~746, guard the append with a membership check:

if col_for_color not in coords:
    coords.append(col_for_color)

The coordinate column is already present in the DataFrame; no extra selection is needed when color names an existing coordinate.

Labels: bug, points, priority: low


Triage tier: Tier 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions