Skip to content

Commit 0975891

Browse files
make outline behavior more similar to squidpy (#472)
2 parents fd11c33 + 5942e13 commit 0975891

File tree

42 files changed

+594
-254
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+594
-254
lines changed

src/spatialdata_plot/pl/basic.py

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
# replace with
6262
# from spatialdata._types import ColorLike
6363
# once https://github.com/scverse/spatialdata/pull/689/ is in a release
64-
ColorLike = tuple[float, ...] | str
64+
ColorLike = tuple[float, ...] | list[float] | str
6565

6666

6767
@register_spatial_data_accessor("pl")
@@ -156,14 +156,14 @@ def _copy(
156156
def render_shapes(
157157
self,
158158
element: str | None = None,
159-
color: str | None = None,
160-
fill_alpha: float | int = 1.0,
159+
color: ColorLike | None = None,
160+
fill_alpha: float | int | None = None,
161161
groups: list[str] | str | None = None,
162162
palette: list[str] | str | None = None,
163163
na_color: ColorLike | None = "default",
164-
outline_width: float | int = 1.5,
165-
outline_color: str | list[float] = "#000000",
166-
outline_alpha: float | int = 0.0,
164+
outline_width: float | int | tuple[float | int, float | int] | None = None,
165+
outline_color: ColorLike | tuple[ColorLike] | None = None,
166+
outline_alpha: float | int | tuple[float | int, float | int] | None = None,
167167
cmap: Colormap | str | None = None,
168168
norm: Normalize | None = None,
169169
scale: float | int = 1.0,
@@ -186,15 +186,18 @@ def render_shapes(
186186
element : str | None, optional
187187
The name of the shapes element to render. If `None`, all shapes elements in the `SpatialData` object will be
188188
used.
189-
color : str | None
190-
Can either be string representing a color-like or key in :attr:`sdata.table.obs`. The latter can be used to
191-
color by categorical or continuous variables. If `element` is `None`, if possible the color will be
192-
broadcasted to all elements. For this, the table in which the color key is found must annotate the
193-
respective element (region must be set to the specific element). If the color column is found in multiple
194-
locations, please provide the table_name to be used for the elements.
195-
fill_alpha : float | int, default 1.0
196-
Alpha value for the fill of shapes. If the alpha channel is present in a cmap passed by the user, this value
197-
will multiply the value present in the cmap.
189+
color : ColorLike | None, optional
190+
Can either be color-like (name of a color as string, e.g. "red", hex representation, e.g. "#000000" or
191+
"#000000ff", or an RGB(A) array as a tuple or list containing 3-4 floats within [0, 1]. If an alpha value is
192+
indicated, the value of `fill_alpha` takes precedence if given) or a string representing a key in
193+
:attr:`sdata.table.obs`. The latter can be used to color by categorical or continuous variables. If
194+
`element` is `None`, if possible the color will be broadcasted to all elements. For this, the table in which
195+
the color key is found must annotate the respective element (region must be set to the specific element). If
196+
the color column is found in multiple locations, please provide the table_name to be used for the elements.
197+
fill_alpha : float | int | None, optional
198+
Alpha value for the fill of shapes. By default, it is set to 1.0 or, if a color is given that implies an
199+
alpha, that value is used for `fill_alpha`. If an alpha channel is present in a cmap passed by the user,
200+
`fill_alpha` will overwrite the value present in the cmap.
198201
groups : list[str] | str | None
199202
When using `color` and the key represents discrete labels, `groups` can be used to show only a subset of
200203
them. Other values are set to NA. If elment is None, broadcasting behaviour is attempted (use the same
@@ -204,18 +207,25 @@ def render_shapes(
204207
match the number of groups. If element is None, broadcasting behaviour is attempted (use the same values for
205208
all elements). If groups is provided but not palette, palette is set to default "lightgray".
206209
na_color : ColorLike | None, default "default" (gets set to "lightgray")
207-
Color to be used for NAs values, if present. Can either be a named color ("red"), a hex representation
210+
Color to be used for NA values, if present. Can either be a named color ("red"), a hex representation
208211
("#000000ff") or a list of floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). When None, the values
209212
won't be shown.
210-
outline_width : float | int, default 1.5
211-
Width of the border.
212-
outline_color : str | list[float], default "#000000"
213+
outline_width : float | int | tuple[float | int, float | int], optional
214+
Width of the border. If 2 values are given (tuple), 2 borders are shown with these widths (outer & inner).
215+
If `outline_color` and/or `outline_alpha` are used to indicate that one/two outlines should be drawn, the
216+
default outline widths 1.5 and 0.5 are used for outer/only and inner outline respectively.
217+
outline_color : ColorLike | tuple[ColorLike], optional
213218
Color of the border. Can either be a named color ("red"), a hex representation ("#000000") or a list of
214219
floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). If the hex representation includes alpha, e.g.
215-
"#000000ff", the last two positions are ignored, since the alpha of the outlines is solely controlled by
216-
`outline_alpha`.
217-
outline_alpha : float | int, default 0.0
218-
Alpha value for the outline of shapes. Invisible by default.
220+
"#000000ff", and `outline_alpha` is not given, this value controls the opacity of the outline. If 2 values
221+
are given (tuple), 2 borders are shown with these colors (outer & inner). If `outline_width` and/or
222+
`outline_alpha` are used to indicate that one/two outlines should be drawn, the default outline colors
223+
"#000000" and "#ffffff are used for outer/only and inner outline respectively.
224+
outline_alpha : float | int | tuple[float | int, float | int] | None, optional
225+
Alpha value for the outline of shapes. Invisible by default, meaning outline_alpha=0.0 if both outline_color
226+
and outline_width are not specified. Else, outlines are rendered with the alpha implied by outline_color, or
227+
with outline_alpha=1.0 if outline_color does not imply an alpha. For two outlines, alpha values can be
228+
passed in a tuple of length 2.
219229
cmap : Colormap | str | None, optional
220230
Colormap for discrete or continuous annotations using 'color', see :class:`matplotlib.colors.Colormap`.
221231
norm : bool | Normalize, default False
@@ -283,8 +293,12 @@ def render_shapes(
283293
sdata = self._copy()
284294
sdata = _verify_plotting_tree(sdata)
285295
n_steps = len(sdata.plotting_tree.keys())
286-
outline_params = _set_outline(outline_alpha > 0, outline_width, outline_color)
287296
for element, param_values in params_dict.items():
297+
final_outline_alpha, outline_params = _set_outline(
298+
params_dict[element]["outline_alpha"],
299+
params_dict[element]["outline_width"],
300+
params_dict[element]["outline_color"],
301+
)
288302
cmap_params = _prepare_cmap_norm(
289303
cmap=cmap,
290304
norm=norm,
@@ -299,7 +313,7 @@ def render_shapes(
299313
outline_params=outline_params,
300314
cmap_params=cmap_params,
301315
palette=param_values["palette"],
302-
outline_alpha=param_values["outline_alpha"],
316+
outline_alpha=final_outline_alpha,
303317
fill_alpha=param_values["fill_alpha"],
304318
transfunc=kwargs.get("transfunc"),
305319
table_name=param_values["table_name"],
@@ -316,8 +330,8 @@ def render_shapes(
316330
def render_points(
317331
self,
318332
element: str | None = None,
319-
color: str | None = None,
320-
alpha: float | int = 1.0,
333+
color: ColorLike | None = None,
334+
alpha: float | int | None = None,
321335
groups: list[str] | str | None = None,
322336
palette: list[str] | str | None = None,
323337
na_color: ColorLike | None = "default",
@@ -343,14 +357,17 @@ def render_points(
343357
element : str | None, optional
344358
The name of the points element to render. If `None`, all points elements in the `SpatialData` object will be
345359
used.
346-
color : str | None
347-
Can either be string representing a color-like or key in :attr:`sdata.table.obs`. The latter can be used to
348-
color by categorical or continuous variables. If `element` is `None`, if possible the color will be
349-
broadcasted to all elements. For this, the table in which the color key is found must annotate the
350-
respective element (region must be set to the specific element). If the color column is found in multiple
351-
locations, please provide the table_name to be used for the elements.
352-
alpha : float | int, default 1.0
353-
Alpha value for the points.
360+
color : str | None, optional
361+
Can either be color-like (name of a color as string, e.g. "red", hex representation, e.g. "#000000" or
362+
"#000000ff", or an RGB(A) array as a tuple or list containing 3-4 floats within [0, 1]. If an alpha value is
363+
indicated, the value of `fill_alpha` takes precedence if given) or a string representing a key in
364+
:attr:`sdata.table.obs`. The latter can be used to color by categorical or continuous variables. If
365+
`element` is `None`, if possible the color will be broadcasted to all elements. For this, the table in which
366+
the color key is found must annotate the respective element (region must be set to the specific element). If
367+
the color column is found in multiple locations, please provide the table_name to be used for the elements.
368+
alpha : float | int | None, optional
369+
Alpha value for the points. By default, it is set to 1.0 or, if a color is given that implies an alpha, that
370+
value is used instead.
354371
groups : list[str] | str | None
355372
When using `color` and the key represents discrete labels, `groups` can be used to show only a subset of
356373
them. Other values are set to NA. If `element` is `None`, broadcasting behaviour is attempted (use the same
@@ -360,7 +377,7 @@ def render_points(
360377
match the number of groups. If `element` is `None`, broadcasting behaviour is attempted (use the same values
361378
for all elements). If groups is provided but not palette, palette is set to default "lightgray".
362379
na_color : ColorLike | None, default "default" (gets set to "lightgray")
363-
Color to be used for NAs values, if present. Can either be a named color ("red"), a hex representation
380+
Color to be used for NA values, if present. Can either be a named color ("red"), a hex representation
364381
("#000000ff") or a list of floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). When None, the values
365382
won't be shown.
366383
cmap : Colormap | str | None, optional
@@ -601,7 +618,7 @@ def render_labels(
601618
element : str | None
602619
The name of the labels element to render. If `None`, all label
603620
elements in the `SpatialData` object will be used and all parameters will be broadcasted if possible.
604-
color : list[str] | str | None
621+
color : str | None
605622
Can either be string representing a color-like or key in :attr:`sdata.table.obs` or in the index of
606623
:attr:`sdata.table.var`. The latter can be used to color by categorical or continuous variables. If the
607624
color column is found in multiple locations, please provide the table_name to be used for the element if you
@@ -626,7 +643,7 @@ def render_labels(
626643
won't be shown.
627644
outline_alpha : float | int, default 0.0
628645
Alpha value for the outline of the labels. Invisible by default.
629-
fill_alpha : float | int, default 0.3
646+
fill_alpha : float | int, default 0.4
630647
Alpha value for the fill of the labels.
631648
scale : str | None
632649
Influences the resolution of the rendering. Possibilities for setting this parameter:

0 commit comments

Comments
 (0)