spatialdata-plot icon indicating copy to clipboard operation
spatialdata-plot copied to clipboard

Can we manually set an outline color for all labels?

Open timtreis opened this issue 2 years ago • 3 comments

from discussion with @aeisenbarth

timtreis avatar Dec 14 '23 10:12 timtreis

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).

aeisenbarth avatar Dec 14 '23 19:12 aeisenbarth

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

https://github.com/scverse/spatialdata-plot/issues/252#issuecomment-2316559464

?

timtreis avatar Aug 29 '24 17:08 timtreis

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.

aeisenbarth avatar Sep 01 '24 09:09 aeisenbarth