Skip to content

Can we manually set an outline color for all labels? #202

Closed

Description

@timtreis

from discussion with @aeisenbarth

Activity

  1. aeisenbarth commented on Dec 14, 2023

    @aeisenbarth
    Contributor

    Use case

    • I want to plot contours only, so that I can overlay multiple labels without them occluding each other.
    • I want to plot all contours of a labels image with a uniform color, so that I can distinguish labels images (colorize by element, not by annotation).

    Example

    from spatialdata import SpatialData
    from spatialdata.models import Labels2DModel
    import numpy as np
    import spatialdata_plot
    
    # Two labels
    array1 = np.zeros((10, 10), dtype=np.uint16)
    array1[1:4, 1:4] = 1
    array1[4:9, 4:9] = 2
    # One label intersecting a label in array1
    array2 = np.zeros((10, 10), dtype=np.uint16)
    array2[2:7, 6:9] = 1
    sdata = SpatialData(
        labels={
            "labels1": Labels2DModel.parse(array1, dims=("y", "x")),
            "labels2": Labels2DModel.parse(array2, dims=("y", "x")),
        }
    )
    sdata.pl.render_labels(
        elements="labels1",
        contour_px=3, # Surprising! contour_px=1 does not render at all
        outline=True,
        outline_alpha=1.0,
        # outline_color="red", # Not yet existing
        fill_alpha=0.0,
    ).pl.render_labels(
        elements="labels2",
        contour_px=3,
        outline=True,
        outline_alpha=1.0,
        # outline_color="green", # Not yet existing
        fill_alpha=0.0,
    ).pl.show(figsize=(2,2))

    Actual:
    spatialdata-plot_202

    Desired:
    spatialdata-plot_202_desired

    • Both cmap and palette affect the fill, not the contour.

    • I don't know whether there are use cases for having the outline color by annotation value.

    • Analogously, one could want pass an actual color name `color="red" (instead of an obs column/ var name) to apply a uniform fill color to all labels.

    Issues

    • Labels rendering currently requires the region to be annotated in the table, or no table at all. This is because _render_labels selects labels to render based on the table. However, a SpatialData with labels only is perfectly valid and there can be interest in visualizing them.

    • Adding fill_alpha=1.0 to above examples should render some default, but raises an error. This is because _map_color_seg is passed a SpatialImage instead of a Numpy array (seg=labels.values).

  2. timtreis commented on Aug 29, 2024

    @timtreis
    MemberAuthor

    Hey @aeisenbarth, does this comment do what you wanted do describe here:

    #252 (comment)

    ?

  3. aeisenbarth commented on Sep 1, 2024

    @aeisenbarth
    Contributor

    My request was to specify an outline color per labels element, that is uniform to all label values of that element. It was already possible enable outline without fill, but the outline is always black.

    From the comment, I couldn't find anything that solves this issue (two render_labels commands, `na_color="green"``).

    I think we would need a separate argument like outline_color that can be set to values other than black.

  4. timtreis commented on Mar 29, 2026

    @timtreis
    MemberAuthor

    Finally fixed with #525

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions