9.2.19. More plotting tools from nilearn¶
In this example, we demonstrate how to use plotting options from nilearn essential in visualizing brain image analysis results.
We emphasize the use of parameters such as display_mode and cut_coords
with plotting function
we show how to use various features such as add_edges, add_contours,
add_markers essential in visualizing regions of interest images or
mask images overlaying on subject specific anatomical/EPI image.
The display features shown here are inherited from the
The parameter display_mode is used to draw brain slices along given specific directions, where directions can be one of ‘ortho’, ‘tiled’,’x’, ‘y’, ‘z’, ‘yx’, ‘xz’, ‘yz’. whereas parameter cut_coords is used to specify a limited number of slices to visualize along given specific slice direction. The parameter cut_coords can also be used to draw the specific cuts in the slices by giving its particular coordinates in MNI space accordingly with particular slice direction. This helps us point to the activation specific location of the brain slices.
See Plotting brain images for more details.
22.214.171.124. First, we retrieve data from nilearn provided (general-purpose) datasets¶
from nilearn import datasets # haxby dataset to have anatomical image, EPI images and masks haxby_dataset = datasets.fetch_haxby() haxby_anat_filename = haxby_dataset.anat haxby_mask_filename = haxby_dataset.mask_vt haxby_func_filename = haxby_dataset.func # localizer dataset to have contrast maps motor_images = datasets.fetch_neurovault_motor_task() stat_img = motor_images.images
Now, we show from here how to visualize the retrieved datasets using plotting tools from nilearn.
from nilearn import plotting
126.96.36.199. Visualizing in - ‘sagittal’, ‘coronal’ and ‘axial’ with given coordinates¶
The first argument is a path to the filename of a contrast map, optional argument display_mode is given as string ‘ortho’ to visualize the map in three specific directions xyz and the optional cut_coords argument, is here a list of integers denotes coordinates of each slice in the order [x, y, z]. By default the colorbar argument is set to True in plot_stat_map.
<nilearn.plotting.displays.OrthoSlicer object at 0x7ff03efc5850>
188.8.131.52. Visualizing in - single view ‘axial’ with number of cuts=5¶
In this type of visualization, the display_mode argument is given as string ‘z’ for axial direction and cut_coords as integer 5 without a list implies that number of cuts in the slices should be maximum of 5. The coordinates to cut the slices are selected automatically
<nilearn.plotting.displays.ZSlicer object at 0x7ff034d12220>
184.108.40.206. Visualizing in - single view ‘sagittal’ with only two slices¶
In this type, display_mode should be given as string ‘x’ for sagittal view and coordinates should be given as integers in a list
<nilearn.plotting.displays.XSlicer object at 0x7ff0308b8a00>
220.127.116.11. Visualizing in - ‘coronal’ view with single cut¶
For coronal view, display_mode is given as string ‘y’ and cut_coords as integer 1 not as a list for single cut. The coordinates are selected automatically
<nilearn.plotting.displays.YSlicer object at 0x7ff036fdcd00>
18.104.22.168. Visualizing without a colorbar on the right side¶
The argument colorbar should be given as False to show plots without a colorbar on the right side.
<nilearn.plotting.displays.ZSlicer object at 0x7ff03e48f790>
22.214.171.124. Visualize in - two views ‘sagittal’ and ‘axial’ with given coordinates¶
argument display_mode=’xz’ where ‘x’ for sagittal and ‘z’ for axial view. argument cut_coords should match with input number of views therefore two integers should be given in a list to select the slices to be displayed
<nilearn.plotting.displays.XZSlicer object at 0x7ff037fae820>
126.96.36.199. Changing the views to ‘coronal’, ‘sagittal’ views with coordinates¶
display_mode=’yx’ for coronal and sagittal view and coordinates will be assigned in the order of direction as [x, y, z]
<nilearn.plotting.displays.YXSlicer object at 0x7ff03f5236d0>
188.8.131.52. Now, views are changed to ‘coronal’ and ‘axial’ views with coordinates¶
<nilearn.plotting.displays.YZSlicer object at 0x7ff03ce96610>
184.108.40.206. Visualizing three views in 2x2 fashion¶
display_mode=’tiled’ for sagittal, coronal and axial view
<nilearn.plotting.displays.TiledSlicer object at 0x7ff035652f10>
220.127.116.11. Visualizing three views along multiple rows and columns¶
display_mode=’mosaic’ for sagittal, coronal and axial view with default option i.e. cut_coords=None
<nilearn.plotting.displays.MosaicSlicer object at 0x7ff0386d96d0>
18.104.22.168. Now, changing the number of slices along columns¶
display_mode=’mosaic’ for sagittal, coronal and axial view with number of slices specified as integer i.e. cut_coords=3
<nilearn.plotting.displays.MosaicSlicer object at 0x7ff03da62a00>
22.214.171.124. Now, another way of limiting the number of slices along rows and columns¶
display_mode=’mosaic’ for sagittal, coronal and axial view with number of slices specified as tuple of length 3
<nilearn.plotting.displays.MosaicSlicer object at 0x7ff03edf3730>
126.96.36.199. Demonstrating various display features¶
In second part, we switch to demonstrating various features add_* from nilearn where each specific feature will be helpful in projecting brain imaging results for further interpretation.
188.8.131.52. Showing how to use add_edges¶
Now let us see how to use add_edges, method useful for checking coregistration by overlaying anatomical image as edges (red) on top of mean functional image (background), both being of same subject.
# First, we call the `plot_anat` plotting function, with a background image # as first argument, in this case the mean fMRI image. display = plotting.plot_anat(mean_haxby_img, title="add_edges") # We are now able to use add_edges method inherited in plotting object named as # display. First argument - anatomical image and by default edges will be # displayed as red 'r', to choose different colors green 'g' and blue 'b'. display.add_edges(haxby_anat_filename)
184.108.40.206. How to use add_contours¶
Plotting outline of the mask (red) on top of the mean EPI image with add_contours. This method is useful for region specific interpretation of brain images
# As seen before, we call the `plot_anat` function with a background image # as first argument, in this case again the mean fMRI image and argument # `cut_coords` as list for manual cut with coordinates pointing at masked # brain regions display = plotting.plot_anat(mean_haxby_img, title="add_contours", cut_coords=[-34, -39, -9]) # Now use `add_contours` in display object with the path to a mask image from # the Haxby dataset as first argument and argument `levels` given as list # of values to select particular level in the contour to display and argument # `colors` specified as red 'r' to see edges as red in color. # See help on matplotlib.pyplot.contour to use more options with this method display.add_contours(haxby_mask_filename, levels=[0.5], colors='r')
Plotting outline of the mask (blue) with color fillings using same method add_contours.
display = plotting.plot_anat(mean_haxby_img, title="add_contours with filled=True", cut_coords=[-34, -39, -9]) # By default, no color fillings will be shown using `add_contours`. To see # contours with color fillings use argument filled=True. contour colors are # changed to blue 'b' with alpha=0.7 sets the transparency of color fillings. # See help on matplotlib.pyplot.contourf to use more options given that filled # should be True display.add_contours(haxby_mask_filename, filled=True, alpha=0.7, levels=[0.5], colors='b')
/home/nicolas/GitRepos/nilearn-fork/nilearn/plotting/displays.py:101: UserWarning: No contour levels were found within the data range. im = getattr(ax, type)(data_2d.copy(),
220.127.116.11. Plotting seeds using add_markers¶
Plotting seed regions of interest as spheres using new feature add_markers with MNI coordinates of interest.
display = plotting.plot_anat(mean_haxby_img, title="add_markers", cut_coords=[-34, -39, -9]) # Coordinates of seed regions should be specified in first argument and second # argument `marker_color` denotes color of the sphere in this case yellow 'y' # and third argument `marker_size` denotes size of the sphere coords = [(-34, -39, -9)] display.add_markers(coords, marker_color='y', marker_size=100)
18.104.22.168. Annotating plots¶
It is possible to alter the default annotations of plots, using the
annotate member function of display objects.
For example, we can add a scale bar at the bottom right of each view:
Further configuration can be achieved by setting
scale_* keyword args.
For instance, changing
units to mm or a different scale bar size.
22.214.171.124. Saving plots to file¶
Finally, saving the plots to file with two different ways
Another way of saving plots is using ‘savefig’ option from display object
Total running time of the script: ( 0 minutes 30.192 seconds)