Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/scanpy/plotting/_tools/scatterplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,9 @@ def embedding( # noqa: PLR0912, PLR0913, PLR0915
# Null points go on bottom
order = np.argsort(~pd.isnull(color_source_vector), kind="stable")
# Set orders
if isinstance(size, np.ndarray):
size = np.array(size)[order]
# Copy size before reordering so the original array is preserved
# across loop iterations (fixes #4024)
_size = size[order] if isinstance(size, np.ndarray) else size
color_source_vector = color_source_vector[order]
color_vector = color_vector[order]
coords = basis_values[:, dims][order, :]
Expand Down Expand Up @@ -348,10 +349,10 @@ def embedding( # noqa: PLR0912, PLR0913, PLR0915
)
else:
scatter = (
partial(ax.scatter, s=size, plotnonfinite=True)
partial(ax.scatter, s=_size, plotnonfinite=True)
if scale_factor is None
else partial(
circles, s=size, ax=ax, scale_factor=scale_factor
circles, s=_size, ax=ax, scale_factor=scale_factor
) # size in circles is radius
)

Expand All @@ -366,7 +367,7 @@ def embedding( # noqa: PLR0912, PLR0913, PLR0915
# with some transparency.

bg_width, gap_width = outline_width
point = np.sqrt(size)
point = np.sqrt(_size)
gap_size = (point + (point * gap_width) * 2) ** 2
bg_size = (np.sqrt(gap_size) + (point * bg_width) * 2) ** 2
# the default black and white colors can be changes using
Expand Down
17 changes: 17 additions & 0 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1875,6 +1875,23 @@ def test_umap_mask_no_modification():
pd.testing.assert_series_equal(pbmc.obs["louvain"], data_copy)


def test_scatter_size_not_mutated_across_panels():
"""Per-point size array must not be cumulatively reordered across subplots.

Regression test for https://github.com/scverse/scanpy/issues/4024
"""
pbmc = pbmc3k_processed()
rng = np.random.default_rng(0)
sizes = rng.uniform(10, 200, size=pbmc.n_obs)
sizes_original = sizes.copy()

axes = sc.pl.umap(pbmc, color=["louvain", "n_genes"], size=sizes, show=False)
Comment thread
flying-sheep marked this conversation as resolved.
Outdated
plt.close()

# The input array must not be modified
np.testing.assert_array_equal(sizes, sizes_original)


def test_string_mask(tmp_path, check_same_image):
"""Check that the same mask given as string or bool array provides the same result."""
pbmc = pbmc3k_processed()
Expand Down
Loading