.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_plotting/plot_demo_glass_brain_extensive.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_01_plotting_plot_demo_glass_brain_extensive.py: Glass brain plotting in nilearn (all options) ============================================= The first part of this example goes through different options of the :func:`~nilearn.plotting.plot_glass_brain` function (including plotting negative values). The second part goes through same options but selected of the same glass brain function but plotting is seen with contours. See :ref:`plotting` for more plotting functionalities and :ref:`Section 4.3 ` for more details about display objects in Nilearn. Also, see :func:`~nilearn.datasets.load_sample_motor_activation_image` for details about the plotting data and associated meta-data. .. GENERATED FROM PYTHON SOURCE LINES 21-25 Load the data ------------- We will use a motor activation :term:`contrast` map distributed with Nilearn. .. GENERATED FROM PYTHON SOURCE LINES 25-32 .. code-block:: Python from nilearn import datasets stat_img = datasets.load_sample_motor_activation_image() # stat_img is just the name of the image file stat_img .. rst-class:: sphx-glr-script-out .. code-block:: none '/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/nilearn/datasets/data/image_10426.nii.gz' .. GENERATED FROM PYTHON SOURCE LINES 33-42 Demo glass brain plotting ------------------------- By default, :func:`~nilearn.plotting.plot_glass_brain` uses a display mode called 'ortho' which results in three projections. It is equivalent to specify ``display_mode='ortho'`` in :func:`~nilearn.plotting.plot_glass_brain`. Note that depending on the value of ``display_mode``, different display objects are returned. Here, a :class:`~nilearn.plotting.displays.OrthoProjector` is returned. .. GENERATED FROM PYTHON SOURCE LINES 42-50 .. code-block:: Python from nilearn.plotting import plot_glass_brain, show # Whole brain sagittal cuts and map is thresholded at 3 plot_glass_brain(stat_img, threshold=3) .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_001.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 51-53 The same figure, without a colorbar, can be produced by setting ``colorbar=False``. .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: Python plot_glass_brain(stat_img, threshold=3, colorbar=False) .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_002.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 58-61 Here, we show how to set a black background, and we only view sagittal and axial projections by setting ``display_mode='xz'``, which returns a :class:`~nilearn.plotting.displays.XZProjector`. .. GENERATED FROM PYTHON SOURCE LINES 61-71 .. code-block:: Python plot_glass_brain( stat_img, title="plot_glass_brain", black_bg=True, display_mode="xz", threshold=3, ) .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_003.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 72-76 We can also plot the sign of the activation by setting ``plot_abs=False``. Additionally, we only visualize coronal and axial projections by setting ``display_mode='yz'`` which returns a :class:`~nilearn.plotting.displays.YZProjector`. .. GENERATED FROM PYTHON SOURCE LINES 76-80 .. code-block:: Python plot_glass_brain(stat_img, threshold=0, plot_abs=False, display_mode="yz") .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_004.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 81-83 Setting ``plot_abs=True`` and ``display_mode='yx'`` (returns a :class:`~nilearn.plotting.displays.YXProjector`). .. GENERATED FROM PYTHON SOURCE LINES 83-86 .. code-block:: Python plot_glass_brain(stat_img, threshold=3, plot_abs=True, display_mode="yx") .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_005.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 87-91 We can control the limits of the colormap and colorbar by setting ``vmin`` and ``vmax``. Note that we use a non-diverging colormap here since the colorbar will not be centered around zero. .. GENERATED FROM PYTHON SOURCE LINES 91-104 .. code-block:: Python # only plot positive values plot_glass_brain( stat_img, plot_abs=False, display_mode="yz", vmin=0, threshold=2, symmetric_cbar=False, cmap="inferno", title="only plot positive values", ) .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_006.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 105-107 Here we set ``vmin`` to the threshold to use the full color range instead of losing colors due to the thresholding. .. GENERATED FROM PYTHON SOURCE LINES 107-118 .. code-block:: Python plot_glass_brain( stat_img, plot_abs=False, display_mode="yz", vmin=2, threshold=2, symmetric_cbar=False, cmap="inferno", title="vmin == threshold", ) .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_007.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 119-128 Different projections for the left and right hemispheres -------------------------------------------------------- In the previous section we saw a few projection modes, which are controlled by setting the argument ``display_mode`` of :func:`~nilearn.plotting.plot_glass_brain`. In this section, we will show some additional possibilities. For example, setting ``display_mode='lzr'`` enables an hemispheric sagittal view. The display object returned is then a :class:`~nilearn.plotting.displays.LZRProjector`. .. GENERATED FROM PYTHON SOURCE LINES 128-138 .. code-block:: Python plot_glass_brain( stat_img, title='plot_glass_brain with display_mode="lzr"', black_bg=True, display_mode="lzr", threshold=3, ) .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_008.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_008.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 139-141 ``display_mode='lyrz'`` returns a :class:`~nilearn.plotting.displays.LYRZProjector` object. .. GENERATED FROM PYTHON SOURCE LINES 141-151 .. code-block:: Python plot_glass_brain( stat_img, threshold=0, title='plot_glass_brain with display_mode="lyrz"', plot_abs=False, display_mode="lyrz", ) .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_009.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_009.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 152-159 If you are only interested in single projections, you can set ``display_mode`` to 'x' (returns a :class:`~nilearn.plotting.displays.XProjector`), 'y' (returns a :class:`~nilearn.plotting.displays.YProjector`), 'z' (returns a :class:`~nilearn.plotting.displays.ZProjector`), 'l' (returns a :class:`~nilearn.plotting.displays.LProjector`), or 'r' (returns a :class:`~nilearn.plotting.displays.RProjector`). .. GENERATED FROM PYTHON SOURCE LINES 159-169 .. code-block:: Python plot_glass_brain( stat_img, threshold=0, title='display_mode="x"', plot_abs=True, display_mode="x", ) .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_010.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_010.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 170-185 Contours and with fillings -------------------------- The display objects returned by :func:`~nilearn.plotting.plot_glass_brain` all inherit from the :class:`~nilearn.plotting.displays.OrthoProjector` and enable further customization of the figures. In this example, we focus on using methods :meth:`~nilearn.plotting.displays.OrthoProjector.add_contours` and :meth:`~nilearn.plotting.displays.OrthoProjector.title`. First, we save the display object (here a :class:`~nilearn.plotting.displays.LZRYProjector`) into a variable named ``display``. Note that we set the first argument to ``None`` since we want an empty glass brain to plot the statistical maps with :meth:`~nilearn.plotting.displays.OrthoProjector.add_contours`. .. GENERATED FROM PYTHON SOURCE LINES 185-193 .. code-block:: Python display = plot_glass_brain(None, display_mode="lzry") # Here, we project statistical maps display.add_contours(stat_img) # and add a title display.title('"stat_img" on glass brain without threshold') .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_011.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_011.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 194-196 We can fill the contours by setting ``filled=True``. Note that we are not specifying levels here .. GENERATED FROM PYTHON SOURCE LINES 196-204 .. code-block:: Python display = plot_glass_brain(None, display_mode="lzry") # Here, we project statistical maps with filled=True display.add_contours(stat_img, filled=True) # and add a title display.title("Same map but with fillings in the contours") .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_012.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_012.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 205-211 Here, we input a specific level (cut-off) in the statistical map. In other words, we are thresholding our statistical map. We set the threshold using a parameter of method :meth:`~nilearn.plotting.displays.OrthoProjector.add_contours` called ``levels`` which value is given as a list and we choose the color to be red. .. GENERATED FROM PYTHON SOURCE LINES 211-217 .. code-block:: Python display = plot_glass_brain(None, display_mode="lzry") display.add_contours(stat_img, levels=[3.0], colors="r") display.title('"stat_img" on glass brain with threshold') .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_013.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_013.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 218-220 Plotting with same demonstration but fill the contours (by setting ``filled=True``). .. GENERATED FROM PYTHON SOURCE LINES 220-226 .. code-block:: Python display = plot_glass_brain(None, display_mode="lzry") display.add_contours(stat_img, filled=True, levels=[3.0], colors="r") display.title("Same demonstration but using fillings inside contours") .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_014.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_014.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 227-229 Plotting with black background, ``black_bg`` should be set to ``True`` through :func:`~nilearn.plotting.plot_glass_brain`. .. GENERATED FROM PYTHON SOURCE LINES 229-236 .. code-block:: Python # We can set black background using black_bg=True display = plot_glass_brain(None, black_bg=True) display.add_contours(stat_img, levels=[3.0], colors="g") display.title('"stat_img" on glass brain with black background') .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_015.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_015.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 237-238 Black background plotting with filled in contours. .. GENERATED FROM PYTHON SOURCE LINES 238-244 .. code-block:: Python display = plot_glass_brain(None, black_bg=True) display.add_contours(stat_img, filled=True, levels=[3.0], colors="g") display.title("Glass brain with black background and filled in contours") .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_016.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_016.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 245-251 Contour projections in both hemispheres --------------------------------------- The key argument to vary here is ``display_mode`` for hemispheric plotting. Here, we set ``display_mode='lr'`` for both hemispheric plots. Note that a :class:`~nilearn.plotting.displays.LRProjector` is returned. .. GENERATED FROM PYTHON SOURCE LINES 251-257 .. code-block:: Python display = plot_glass_brain(None, display_mode="lr") display.add_contours(stat_img, levels=[3.0], colors="r") display.title('"stat_img" on glass brain only\n"l" "r" hemispheres') .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_017.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_017.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 258-259 Filled contours in both hemispheric plotting, by adding ``filled=True``. .. GENERATED FROM PYTHON SOURCE LINES 259-265 .. code-block:: Python display = plot_glass_brain(None, display_mode="lr") display.add_contours(stat_img, filled=True, levels=[3.0], colors="r") display.title('Filled contours on glass brain only\n"l" "r" hemispheres') .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_018.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_018.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 266-273 With positive and negative signs of activations with ``plot_abs`` in :func:`~nilearn.plotting.plot_glass_brain`. By default parameter ``plot_abs`` is ``True`` and sign of activations can be displayed by changing ``plot_abs`` to ``False``. Note that we also specify ``display_mode='lyr'`` which returns a :class:`~nilearn.plotting.displays.LYRProjector` display object. .. GENERATED FROM PYTHON SOURCE LINES 273-279 .. code-block:: Python display = plot_glass_brain(None, plot_abs=False, display_mode="lyr") display.add_contours(stat_img) display.title("Contours with both sign of activations without threshold") .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_019.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_019.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 280-282 Now, adding ``filled=True`` to get positive and negative sign activations with fillings in the contours. .. GENERATED FROM PYTHON SOURCE LINES 282-290 .. code-block:: Python display = plot_glass_brain(None, plot_abs=False, display_mode="lyr") display.add_contours(stat_img, filled=True) display.title( "Filled contours with both sign of activations without threshold" ) .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_020.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_020.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 291-301 Displaying both signs (positive and negative) of activations with threshold meaning thresholding by adding an argument ``levels`` in method :meth:`~nilearn.plotting.displays.OrthoProjector.add_contours`. We give two values through the argument ``levels`` which corresponds to the thresholds of the contour we want to draw: One is positive and the other one is negative. We give a list of ``colors`` as argument to associate a different color to each contour. Additionally, we also choose to plot contours with thick line widths. For ``linewidths``, one value would be enough so that same value is used for both contours. .. GENERATED FROM PYTHON SOURCE LINES 301-311 .. code-block:: Python import numpy as np display = plot_glass_brain(None, plot_abs=False, display_mode="lzry") display.add_contours( stat_img, levels=[-2.8, 3.0], colors=["b", "r"], linewidths=4.0 ) display.title("Contours with sign of activations with threshold") .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_021.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_021.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 312-322 Same display demonstration as above but adding ``filled=True`` to get fillings inside the contours. Unlike in previous plot, here we specify each sign at a time. We call negative values display first followed by positive values display. First, we fetch our display object with same parameters used as above. Then, we plot negative sign of activation with levels given as negative activation value in a list. Upper bound should be kept to -infinity. Next, using the same display object, we plot positive sign of activation. .. GENERATED FROM PYTHON SOURCE LINES 322-330 .. code-block:: Python display = plot_glass_brain(None, plot_abs=False, display_mode="lzry") display.add_contours(stat_img, filled=True, levels=[-np.inf, -2.8], colors="b") display.add_contours(stat_img, filled=True, levels=[3.0], colors="r") display.title("Now same plotting but with filled contours") # Finally, displaying them show() .. image-sg:: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_022.png :alt: plot demo glass brain extensive :srcset: /auto_examples/01_plotting/images/sphx_glr_plot_demo_glass_brain_extensive_022.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 20.873 seconds) **Estimated memory usage:** 116 MB .. _sphx_glr_download_auto_examples_01_plotting_plot_demo_glass_brain_extensive.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/nilearn/nilearn/0.12.0?urlpath=lab/tree/notebooks/auto_examples/01_plotting/plot_demo_glass_brain_extensive.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_demo_glass_brain_extensive.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_demo_glass_brain_extensive.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_demo_glass_brain_extensive.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_