4.1. Plotting brain images¶
Nilearn comes with plotting function to display brain maps coming from
Nifti-like images, in the nilearn.plotting
module.
4.1.1. Different plotting functions¶
Nilearn has a set of plotting functions to plot brain volumes that are fined tuned to specific applications. Amongst other things, they use different heuristics to find cutting coordinates.
plot_anat
Plotting an anatomical image |
|
plot_epi
Plotting an EPI, or T2* image |
|
plot_glass_brain
Glass brain visualization. By default plots maximum intensity projection of the absolute values. To plot positive and negative values set plot_abs parameter to False. |
|
plot_stat_map
Plotting a statistical map, like a T-map, a Z-map, or an ICA, with an optional background |
|
plot_roi
Plotting ROIs, or a mask, with an optional background |
|
plot_connectome
Plotting a connectome |
|
plot_img | plot_img
General-purpose function, with no specific presets |
Warning
Opening too many figures without closing
Each call to a plotting function creates a new figure by default. When used in non-interactive settings, such as a script or a program, these are not displayed, but still accumulate and eventually lead to slowing the execution and running out of memory.
To avoid this, you must close the plot as follow:
>>> from nilearn import plotting
>>> display = plotting.plot_stat_map(img)
>>> display.close()
4.1.2. Different display modes¶
4.1.3. Adding overlays, edges and contours¶
To add overlays, contours, or edges, use the return value of the plotting
functions. Indeed, these return a display object, such as the
nilearn.plotting.displays.OrthoSlicer
. This object represents the
plot, and has methods to add overlays, contours or edge maps:
display = plotting.plot_epi(...)
4.1.4. Saving to an image file¶
The simplest way to output an image file from the plotting functions is to specify the output_file argument:
>>> from nilearn import plotting
>>> plotting.plot_stat_map(img, output_file='pretty_brain.png')
In this case, the display is closed automatically and the plotting function returns None.
The display object returned by the plotting function has a savefig method that can be used to save the plot to an image file:
>>> from nilearn import plotting
>>> display = plotting.plot_stat_map(img)
>>> display.savefig('pretty_brain.png')
# Don't forget to close the display
>>> display.close()