How to plot multiple slices of a 3D brain image with nilearn and matplotlib subplots

897 views Asked by At

I want to plot 25 slices of a brain scan using nilearn. These 25 slices should go along the z axis in steps of value=2. I want to use subplots to present them using subplots.

Here is what I have so far:

cuts = np.arange(-25,25,2)
fig, (axes1,axes2,axes3,axes4,axes5) = plt.subplots(5,1, figsize=(10,10))

for axes in [axes1,axes2,axes3,axes4,axes5]:
    for slc in cuts:
        plotting.plot_stat_map(rsn_four, display_mode='z', axes=axes, cut_coords=[slc], threshold=2)
plt.show()

'rsn_four' is a 3D Nifti file of a BOLD scan.

Output:

I think one of my biggest problem is that I do not know how to implement 5 values of the np.arange() and then bring it to the next axis continuing the counting.

1

There are 1 answers

0
S.Chauhan On BEST ANSWER

The enumerate built-in may be helpful here in getting only the correct indices from cuts that you would want per image

cuts = np.arange(-25,25,2)
fig, (axes1,axes2,axes3,axes4,axes5) = plt.subplots(5,1, figsize=(10,10))

for num, axes in enumerate([axes1,axes2,axes3,axes4,axes5]):
    for slc in cuts[num:num+5]:
        plotting.plot_stat_map(rsn_four, display_mode='z', axes=axes, cut_coords=[slc], threshold=2)
plt.show()