Skip to content

Commit 5392f57

Browse files
author
Sonja Stockhaus
committed
nan handling for continuous and categorical coloring
1 parent 478ab27 commit 5392f57

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/spatialdata_plot/pl/utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,15 @@ def _get_collection_shape(
361361
c = cmap(c)
362362
else:
363363
try:
364-
norm = colors.Normalize(vmin=min(c), vmax=max(c)) if norm is None else norm
364+
norm = colors.Normalize(vmin=np.nanmin(c), vmax=np.nanmax(c)) if norm is None else norm
365365
except ValueError as e:
366366
raise ValueError(
367367
"Could not convert values in the `color` column to float, if `color` column represents"
368368
" categories, set the column to categorical dtype."
369369
) from e
370-
c = cmap(norm(c))
370+
# normalize only the not nan values, else the whole array would contain only nan values
371+
c[~c.isnull()] = norm(c[~c.isnull()])
372+
c = cmap(c)
371373

372374
fill_c = ColorConverter().to_rgba_array(c)
373375
fill_c[..., -1] *= render_params.fill_alpha
@@ -769,6 +771,9 @@ def _set_color_source_vec(
769771

770772
# do not rename categories, as colors need not be unique
771773
color_vector = color_source_vector.map(color_mapping)
774+
# nan handling
775+
color_vector = color_vector.add_categories(na_color)
776+
color_vector[pd.isna(color_vector)] = na_color
772777

773778
return color_source_vector, color_vector, True
774779

0 commit comments

Comments
 (0)