Skip to content

Commit 3a54f45

Browse files
committed
Fix NaN handling and transparency in shape rendering
- Fix KeyError in _convert_shapes by using proper DataFrame indexing - Improve NaN value handling in color processing to use na_color correctly - Preserve transparency from colormaps when fill_alpha is specified - Add support for mixed numeric/color arrays in shape coloring - Refactor _convert_shapes to use positional indexing for better reliability
1 parent 491dae9 commit 3a54f45

File tree

2 files changed

+236
-290
lines changed

2 files changed

+236
-290
lines changed

src/spatialdata_plot/pl/render.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ def _render_shapes(
141141
color_source_vector = color_source_vector[mask]
142142
color_vector = color_vector[mask]
143143

144+
# continuous case: leave NaNs as NaNs; utils maps them to na_color during draw
145+
if color_source_vector is None and not values_are_categorical:
146+
color_vector = np.asarray(color_vector, dtype=float)
147+
if np.isnan(color_vector).any():
148+
nan_count = int(np.isnan(color_vector).sum())
149+
logger.warning(
150+
f"Found {nan_count} NaN values in color data. These observations will be colored with the 'na_color'."
151+
)
152+
144153
# Using dict.fromkeys here since set returns in arbitrary order
145154
# remove the color of NaN values, else it might be assigned to a category
146155
# order of color in the palette should agree to order of occurence
@@ -447,12 +456,13 @@ def _render_shapes(
447456
path.vertices = trans.transform(path.vertices)
448457

449458
if not values_are_categorical:
450-
# If the user passed a Normalize object with vmin/vmax we'll use those,
451-
# if not we'll use the min/max of the color_vector
452-
_cax.set_clim(
453-
vmin=render_params.cmap_params.norm.vmin or min(color_vector),
454-
vmax=render_params.cmap_params.norm.vmax or max(color_vector),
455-
)
459+
vmin = render_params.cmap_params.norm.vmin
460+
vmax = render_params.cmap_params.norm.vmax
461+
if vmin is None:
462+
vmin = float(np.nanmin(color_vector))
463+
if vmax is None:
464+
vmax = float(np.nanmax(color_vector))
465+
_cax.set_clim(vmin=vmin, vmax=vmax)
456466

457467
if (
458468
len(set(color_vector)) != 1

0 commit comments

Comments
 (0)