Skip to content

Commit ac4677f

Browse files
author
Sonja Stockhaus
committed
fix behavior for labels (continuous and categorical)
1 parent d7a6d17 commit ac4677f

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/spatialdata_plot/pl/utils.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -797,15 +797,25 @@ def _map_color_seg(
797797

798798
if pd.api.types.is_categorical_dtype(color_vector.dtype):
799799
# Case A: users wants to plot a categorical column
800-
if np.any(color_source_vector.isna()):
801-
cell_id[color_source_vector.isna()] = 0
800+
801+
# TODO: remove
802+
# in seg, the value 0 depicts the background, so this leads to the bg being mapped to the NaN color
803+
# the actual label(s) with na in the color_source_vector don't have their id in cell_id anymore, so they're
804+
# mapped to nothing! => would look like background
805+
# if np.any(color_source_vector.isna()):
806+
# cell_id[color_source_vector.isna()] = 0
802807
val_im: ArrayLike = map_array(seg.copy(), cell_id, color_vector.codes + 1)
803808
cols = colors.to_rgba_array(color_vector.categories)
804809
elif pd.api.types.is_numeric_dtype(color_vector.dtype):
805810
# Case B: user wants to plot a continous column
806811
if isinstance(color_vector, pd.Series):
807812
color_vector = color_vector.to_numpy()
808-
cols = cmap_params.cmap(cmap_params.norm(color_vector))
813+
# normalize only the not nan values, else the whole array would contain only nan values
814+
normed_color_vector = color_vector.copy()
815+
normed_color_vector[~np.isnan(normed_color_vector)] = cmap_params.norm(
816+
normed_color_vector[~np.isnan(normed_color_vector)]
817+
)
818+
cols = cmap_params.cmap(normed_color_vector)
809819
val_im = map_array(seg.copy(), cell_id, cell_id)
810820
else:
811821
# Case C: User didn't specify any colors

0 commit comments

Comments
 (0)