Skip to content

Match plot colors (squidpy)

Color the Landmarks map from Squidpy niche labels and reuse the same hex colors in Matplotlib or Scanpy plots.

Goal

Run Squidpy neighborhood niches on a selection, assign w.color to the niche column Squidpy writes, and read colors back with w.category_colors.

Steps

  1. Install optional analysis packages:
pip install scanpy squidpy leidenalg
  1. Open the ileum slice and draw a region to analyze (or use the whole tissue):
import milume as ml
import squidpy as sq

adata = ml.ds.ileum_merfish_xu2026()
w = ml.peek(adata, color="cell_type")
w  # draw a selection in the browser, then continue
  1. Build a boolean mask aligned to adata.obs_names for the selection you want:
region = w.selections[-1]["id"]
mask = adata.obs_names.isin(w.get_obs_names(region))
  1. Run Squidpy niches on that mask (neighbors + spatial graph first):
sc.pp.neighbors(adata)
sq.gr.spatial_neighbors_knn(adata, n_neighs=6)
sq.gr.calculate_niche(
    adata,
    flavor="neighborhood",
    groups="cell_type",
    n_neighbors=6,
    resolutions=0.5,
    mask=mask,
)
niche_col = "nhood_niche_res=0.5"
  1. Colour the map from the new column:
w.color = niche_col
  1. Reuse the map palette elsewhere:
palette = w.category_colors(niche_col)  # {label: "#rrggbb"}

Result

Landmarks points and downstream plots share the same label-to-color mapping as the map.

For a prepared studio walkthrough (fixed selection and export state), use the Squidpy niche color recipe or the live molab notebook. The recipe export pipeline is unchanged; this page is the task-only steps.

Squidpy and Scanpy parameters stay in their own docs; milume traitlets are in the LandmarksWidget API.