Hi. I recently worked that out. This is the function of nilearn to plot connetcome: nilearn.plotting.plot_connectome(), in which it needs adjacency_matrix and node_coords, the two most importance parameters. Adjacency matrix is the āconnectivity matrixā we get from mne. But the problem is mneās built-in āaparcā .annot file donāt have MNI coordinates to input as node_coords. Therefore, we use mne.vertex_to_mni() to convert the labels into MNI coordinates. I now share the codes:
import numpy as np
import mne
from nilearn.plotting import plot_connectome
import matplotlib.pyplot as plt
Define subject and directory
subject = āfsaverageā
subjects_dir = āC:/Users/23859/mne_data/MNE-fsaverage-dataā
Load labels from the aparc parcellation for both hemispheres
labels_lh = mne.read_labels_from_annot(subject, parc=āYeo2011_17Networks_N1000ā, hemi=ālhā, subjects_dir=subjects_dir)
labels_rh = mne.read_labels_from_annot(subject, parc=āYeo2011_17Networks_N1000ā, hemi=ārhā, subjects_dir=subjects_dir)
Combine left and right hemisphere labels
labels = labels_lh + labels_rh
Store MNI coordinates and label names
mni_coords =
label_names =
Convert each labelās vertex coordinates to MNI space and compute centroid
for label in labels:
vertices = label.vertices # Get vertex indices
hemisphere = 0 if label.hemi == ālhā else 1 # 0 for left, 1 for right
if len(vertices) > 0: # Ensure the label has vertices
mni_vertex_coords = mne.vertex_to_mni(vertices, hemis=hemisphere, subject=subject, subjects_dir=subjects_dir)
centroid = np.mean(mni_vertex_coords, axis=0) # Compute centroid
mni_coords.append(centroid)
label_names.append(label.name)
mni_coords = np.array(mni_coords) # Convert list to array
Create a random connectivity matrix for visualization (replace with real data)
n_labels = len(mni_coords)
Plot the connectome
fig, ax = plt.subplots(figsize=(8, 8))
plot_connectome(adjacency_matrix = con_res[āpliā],node_coords= mni_coords, edge_cmap=āviridisā, title=āConnectivity Visualizationā, axes=ax,edge_threshold=ā95%ā)
plt.show()