Skip to content

Commit 6d3961f

Browse files
DOC: add Brain example for per-vertex opacity
1 parent 8dd298f commit 6d3961f

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add support for scalar or per-vertex ``alpha`` (shape ``(n_vertices,)``) in distributed overlays, including validation/errors for invalid shapes, tests, and a visualization example in :mod:`examples.visualization.brain` using :meth:`mne.viz.Brain.add_data`, by :newcontrib:`Pragnya Khandelwal`. (:gh:`13706`)

examples/visualization/brain.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# of :class:`mne.viz.Brain` for plotting data on a brain.
2222

2323
import matplotlib.pyplot as plt
24+
import numpy as np
2425
from matplotlib.cm import ScalarMappable
2526
from matplotlib.colors import Normalize
2627

@@ -134,3 +135,31 @@
134135
norm = Normalize(vmin=0, vmax=dip.gof.max())
135136
fig.colorbar(ScalarMappable(norm=norm, cmap=cmap), cax=cax)
136137
fig.suptitle("Dipole Fits Scaled by Amplitude and Colored by GOF")
138+
139+
140+
# %%
141+
# Use per-vertex opacity for distributed data
142+
# --------------------------------------------
143+
#
144+
# You can provide an array for ``alpha`` in :meth:`mne.viz.Brain.add_data`
145+
# to control transparency per vertex. This can be useful to emphasize a
146+
# subset of vertices while still showing surrounding context.
147+
148+
brain = mne.viz.Brain("sample", subjects_dir=subjects_dir, **brain_kwargs)
149+
coords = brain.geo["lh"].coords
150+
n_vertices = len(coords)
151+
152+
# Build synthetic data (a smooth left-to-right gradient) and a matching
153+
# opacity ramp from mostly transparent to fully opaque.
154+
data = coords[:, 0]
155+
data = (data - data.min()) / (data.max() - data.min())
156+
vertex_alpha = np.linspace(0.2, 1.0, n_vertices)
157+
158+
brain.add_data(
159+
data,
160+
hemi="lh",
161+
alpha=vertex_alpha,
162+
colormap="mne",
163+
smoothing_steps=5,
164+
)
165+
brain.show_view(azimuth=190, elevation=70, distance=350, focalpoint=(0, 0, 20))

0 commit comments

Comments
 (0)