Commit f55e0b1e authored by Sean Fitzgibbon's avatar Sean Fitzgibbon Committed by Paul McCarthy
Browse files

Added example combining matplotlib and nilearn

parent f1a842ea
......@@ -30,10 +30,11 @@
``` python
import os
from nilearn import plotting, datasets, surface
import matplotlib as mpl
import matplotlib.pyplot as plt
import nibabel as nb
%matplotlib inline
# get path to FSL installation for the FSLDIR environment variable
FSLDIR = os.environ['FSLDIR']
......@@ -144,10 +145,47 @@
%% Cell type:markdown id: tags:
`nilearn` plotting is built upon `matplotlib`, so we can use constructs from `matplotlib` to help us create more complex figures.
In this example we:
1. create a 1x2 grid of subplots using `subplots` from `matplotlib`
2. plot a single slice of the MNI152 T1w in the first subplot using `plot_anat` from `nilearn`
3. plot a histogram of the intensities of the MNI152 T12 in the second subplot using `hist` from `matplotlib`
4. style the histogram by setting the x/y labels
> **NOTE:** Here we use `load` and `get_fdata` from the [`nibabel`]( package to load the data from the MNI152 T1w nifti for the histogram.
%% Cell type:code id: tags:
``` python
# create matplotlib figure with 1x2 subplots
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
# plot MNI T1w slice in first subplot
mni_t1 = f'{FSLDIR}/data/standard/MNI152_T1_1mm.nii.gz'
display = plotting.plot_anat(
# plot histogram of MNI T1w intensity in second subplot
mni_t1_data = nb.load(mni_t1).get_fdata().ravel()
ax[1].hist(mni_t1_data, bins=25)
%% Cell type:markdown id: tags:
> **Exercise:**
> Create a PNG figure that displays the same **Harvard-Oxford** parcellation, using a different colormap, and axial images in a 3x5 grid.
......@@ -334,11 +372,11 @@
%% Cell type:markdown id: tags:
> **Exercise:**
> Visualise thee **Harvard-Oxford** atlas on a surface in the weeb Browser
> Visualise thee **Harvard-Oxford** atlas on a surface in the web browser
%% Cell type:code id: tags:
``` python
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment