Plotting tools in nilearn#

Nilearn comes with a set of plotting functions for easy visualization of Nifti-like images such as statistical maps mapped onto anatomical images or onto glass brain representation, anatomical images, functional/EPI images, region specific mask images.

See Plotting brain images for more details.

Retrieve data from nilearn provided (general-purpose) datasets#

from nilearn import datasets

# haxby dataset to have EPI images and masks
haxby_dataset = datasets.fetch_haxby()

# print basic information on the dataset
print('First subject anatomical nifti image (3D) is at: %s' %
print('First subject functional nifti image (4D) is at: %s' %
      haxby_dataset.func[0])  # 4D data

haxby_anat_filename = haxby_dataset.anat[0]
haxby_mask_filename = haxby_dataset.mask_vt[0]
haxby_func_filename = haxby_dataset.func[0]

# one motor contrast map from NeuroVault
motor_images = datasets.fetch_neurovault_motor_task()
stat_img = motor_images.images[0]
First subject anatomical nifti image (3D) is at: /home/yasmin/nilearn/nilearn_data/haxby2001/subj2/anat.nii.gz
First subject functional nifti image (4D) is at: /home/yasmin/nilearn/nilearn_data/haxby2001/subj2/bold.nii.gz

Plotting statistical maps with function plot_stat_map#

from nilearn import plotting

# Visualizing t-map image on EPI template with manual
# positioning of coordinates using cut_coords given as a list
                       threshold=3, title="plot_stat_map",
                       cut_coords=[36, -27, 66])
plot demo plotting
<nilearn.plotting.displays._slicers.OrthoSlicer object at 0x7fc1e37d1ff0>

Making interactive visualizations with function view_img#

An alternative to nilearn.plotting.plot_stat_map is to use nilearn.plotting.view_img that gives more interactive visualizations in a web browser. See Interactive visualization of statistical map slices for more details.

view = plotting.view_img(stat_img, threshold=3)
# In a Jupyter notebook, if ``view`` is the output of a cell, it will
# be displayed below the cell
/home/yasmin/miniconda3/envs/nilearn-dev/lib/python3.10/site-packages/numpy/core/ UserWarning: Warning: 'partition' will ignore the 'mask' of the MaskedArray.
  a.partition(kth, axis=axis, kind=kind, order=order)