Skip to content

Commit 67a8d4f

Browse files
author
Sonja Stockhaus
committed
fix alpha behavior, enable outline alpha setting, use matplotlib logic
1 parent d48de0d commit 67a8d4f

4 files changed

Lines changed: 430 additions & 370 deletions

File tree

src/spatialdata_plot/pl/basic.py

Lines changed: 44 additions & 37 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 | tuple[float | int, float | int] = 1.5,
165-
outline_color: str | list[float] | tuple[str | list[float], 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,17 @@ 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
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, default 1.0
198+
Alpha value for the fill of shapes. If an alpha channel is present in a cmap passed by the user, this
199+
value will overwrite the value present in the cmap.
198200
groups : list[str] | str | None
199201
When using `color` and the key represents discrete labels, `groups` can be used to show only a subset of
200202
them. Other values are set to NA. If elment is None, broadcasting behaviour is attempted (use the same
@@ -204,20 +206,21 @@ def render_shapes(
204206
match the number of groups. If element is None, broadcasting behaviour is attempted (use the same values for
205207
all elements). If groups is provided but not palette, palette is set to default "lightgray".
206208
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
209+
Color to be used for NA values, if present. Can either be a named color ("red"), a hex representation
208210
("#000000ff") or a list of floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). When None, the values
209211
won't be shown.
210212
outline_width : float | int | tuple[float | int, float | int], default 1.5
211213
Width of the border. If 2 values are given (tuple), 2 borders are shown with these widths (outer & inner).
212-
In that case, and if < 2 outline_colors are specified, the default color (`outline_color`, "white") is used.
213-
outline_color : str | list[float], default "#000000"
214+
outline_color : ColorLike | tuple[ColorLike], default "#000000"
214215
Color of the border. Can either be a named color ("red"), a hex representation ("#000000") or a list of
215216
floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). If the hex representation includes alpha, e.g.
216-
"#000000ff", the last two positions are ignored, since the alpha of the outlines is solely controlled by
217-
`outline_alpha`. If 2 values are given (tuple), 2 borders are shown with these colors (outer & inner).
218-
In that case, and if < 2 outline widths are specified, the default width (`outline_width`, 0.5) is used.
219-
outline_alpha : float | int, default 0.0
220-
Alpha value for the outline of shapes. Invisible by default.
217+
"#000000ff", and `outline_alpha` is not given, this value controls the opacity of the outline. If 2 values
218+
are given (tuple), 2 borders are shown with these colors (outer & inner).
219+
outline_alpha : float | int | tuple[float | int, float | int] | None
220+
Alpha value for the outline of shapes. Invisible by default, meaning outline_alpha=0.0 if both outline_color
221+
and outline_width are not specified. Else, outlines are rendered with the alpha implied by outline_color, or
222+
with outline_alpha=1.0 if outline_color does not imply an alpha. For two outlines, alpha values can be
223+
passed in a tuple of length 2.
221224
cmap : Colormap | str | None, optional
222225
Colormap for discrete or continuous annotations using 'color', see :class:`matplotlib.colors.Colormap`.
223226
norm : bool | Normalize, default False
@@ -249,8 +252,6 @@ def render_shapes(
249252
- Empty geometries will be removed at the time of plotting.
250253
- An `outline_width` of 0.0 leads to no border being plotted.
251254
- When passing a color-like to 'color', this has precendence over the potential existence as a column name.
252-
- If a double outline is rendered, the alpha of the inner and outer border cannot be set individually. The value
253-
of `outline_alpha` is always applied to both.
254255
255256
Returns
256257
-------
@@ -287,8 +288,12 @@ def render_shapes(
287288
sdata = self._copy()
288289
sdata = _verify_plotting_tree(sdata)
289290
n_steps = len(sdata.plotting_tree.keys())
290-
outline_params = _set_outline(outline_alpha > 0, outline_width, outline_color)
291291
for element, param_values in params_dict.items():
292+
final_outline_alpha, outline_params = _set_outline(
293+
params_dict[element]["outline_alpha"],
294+
params_dict[element]["outline_width"],
295+
params_dict[element]["outline_color"],
296+
)
292297
cmap_params = _prepare_cmap_norm(
293298
cmap=cmap,
294299
norm=norm,
@@ -303,7 +308,7 @@ def render_shapes(
303308
outline_params=outline_params,
304309
cmap_params=cmap_params,
305310
palette=param_values["palette"],
306-
outline_alpha=param_values["outline_alpha"],
311+
outline_alpha=final_outline_alpha,
307312
fill_alpha=param_values["fill_alpha"],
308313
transfunc=kwargs.get("transfunc"),
309314
table_name=param_values["table_name"],
@@ -320,8 +325,8 @@ def render_shapes(
320325
def render_points(
321326
self,
322327
element: str | None = None,
323-
color: str | None = None,
324-
alpha: float | int = 1.0,
328+
color: ColorLike | None = None,
329+
alpha: float | int | None = None,
325330
groups: list[str] | str | None = None,
326331
palette: list[str] | str | None = None,
327332
na_color: ColorLike | None = "default",
@@ -348,12 +353,14 @@ def render_points(
348353
The name of the points element to render. If `None`, all points elements in the `SpatialData` object will be
349354
used.
350355
color : str | None
351-
Can either be string representing a color-like or key in :attr:`sdata.table.obs`. The latter can be used to
352-
color by categorical or continuous variables. If `element` is `None`, if possible the color will be
353-
broadcasted to all elements. For this, the table in which the color key is found must annotate the
354-
respective element (region must be set to the specific element). If the color column is found in multiple
355-
locations, please provide the table_name to be used for the elements.
356-
alpha : float | int, default 1.0
356+
Can either be color-like (name of a color as string, e.g. "red", hex representation, e.g. "#000000" or
357+
"#000000ff", or an RGB(A) array as a tuple or list containing 3-4 floats within [0, 1]. If an alpha value is
358+
indicated, the value of `fill_alpha` takes precedence if given) or a string representing a key in
359+
:attr:`sdata.table.obs`. The latter can be used to color by categorical or continuous variables. If
360+
`element` is `None`, if possible the color will be broadcasted to all elements. For this, the table in which
361+
the color key is found must annotate the respective element (region must be set to the specific element). If
362+
the color column is found in multiple locations, please provide the table_name to be used for the elements.
363+
alpha : float | int | None, default 1.0
357364
Alpha value for the points.
358365
groups : list[str] | str | None
359366
When using `color` and the key represents discrete labels, `groups` can be used to show only a subset of
@@ -364,7 +371,7 @@ def render_points(
364371
match the number of groups. If `element` is `None`, broadcasting behaviour is attempted (use the same values
365372
for all elements). If groups is provided but not palette, palette is set to default "lightgray".
366373
na_color : ColorLike | None, default "default" (gets set to "lightgray")
367-
Color to be used for NAs values, if present. Can either be a named color ("red"), a hex representation
374+
Color to be used for NA values, if present. Can either be a named color ("red"), a hex representation
368375
("#000000ff") or a list of floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). When None, the values
369376
won't be shown.
370377
cmap : Colormap | str | None, optional
@@ -605,7 +612,7 @@ def render_labels(
605612
element : str | None
606613
The name of the labels element to render. If `None`, all label
607614
elements in the `SpatialData` object will be used and all parameters will be broadcasted if possible.
608-
color : list[str] | str | None
615+
color : str | None
609616
Can either be string representing a color-like or key in :attr:`sdata.table.obs` or in the index of
610617
:attr:`sdata.table.var`. The latter can be used to color by categorical or continuous variables. If the
611618
color column is found in multiple locations, please provide the table_name to be used for the element if you

0 commit comments

Comments
 (0)