diff --git a/applications/nilearn/nilearn.ipynb b/applications/nilearn/nilearn.ipynb index 08f1cc8e3d9b0d8ef211ff69debf012253a2e31c..7d966e2b8ee7845a82f8f143464162b38a9cc90b 100644 --- a/applications/nilearn/nilearn.ipynb +++ b/applications/nilearn/nilearn.ipynb @@ -40,6 +40,7 @@ "from nilearn import plotting, datasets, surface\n", "import matplotlib as mpl\n", "import matplotlib.pyplot as plt\n", + "import nibabel as nb\n", "%matplotlib inline\n", "\n", "# get path to FSL installation for the FSLDIR environment variable\n", @@ -181,6 +182,49 @@ "display.savefig('myplot.png')" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`nilearn` plotting is built upon `matplotlib`, so we can use constructs from `matplotlib` to help us create more complex figures. \n", + "\n", + "In this example we:\n", + "1. create a 1x2 grid of subplots using `subplots` from `matplotlib`\n", + "2. plot a single slice of the MNI152 T1w in the first subplot using `plot_anat` from `nilearn`\n", + "3. plot a histogram of the intensities of the MNI152 T12 in the second subplot using `hist` from `matplotlib`\n", + "4. style the histogram by setting the x/y labels\n", + "\n", + "> **NOTE:** Here we use `load` and `get_fdata` from the [`nibabel`](https://nipy.org/nibabel/) package to load the data from the MNI152 T1w nifti for the histogram." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# create matplotlib figure with 1x2 subplots\n", + "fig, ax = plt.subplots(1, 2, figsize=(10, 5))\n", + "\n", + "# plot MNI T1w slice in first subplot\n", + "mni_t1 = f'{FSLDIR}/data/standard/MNI152_T1_1mm.nii.gz'\n", + "\n", + "display = plotting.plot_anat(\n", + " mni_t1, \n", + " dim=-0.5, \n", + " axes=ax[0],\n", + " display_mode='z', \n", + " cut_coords=[15]\n", + ")\n", + "\n", + "# plot histogram of MNI T1w intensity in second subplot\n", + "mni_t1_data = nb.load(mni_t1).get_fdata().ravel()\n", + "\n", + "ax[1].hist(mni_t1_data, bins=25)\n", + "ax[1].set_ylabel('count')\n", + "ax[1].set_xlabel('intensity')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -434,7 +478,7 @@ ">\n", "> **Exercise:**\n", "> \n", - "> Visualise thee **Harvard-Oxford** atlas on a surface in the weeb Browser" + "> Visualise thee **Harvard-Oxford** atlas on a surface in the web browser" ] }, { @@ -470,7 +514,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.9" + "version": "3.7.6" } }, "nbformat": 4,