Plotting multiple matplotlib axes class object

162 views Asked by At

My problem is the following: I'm trying to plot in a readable way 6 different design matrix. The function creating the display for this design matrix is part of the nipy module and is describe as this:

class nipy.modalities.fmri.design_matrix.DesignMatrix

Function show(): Visualization of a design matrix

Parameters:

 rescale: bool, optional, 
      rescale columns magnitude for visualization or not.
 ax: axis handle, optional
      Handle to axis onto which we will draw design matrix.
 cmap: colormap, optional
      Matplotlib colormap to use, passed to imshow.

Returns:

 ax: axis handle

Basicly, I'm trying to do a subplot with 3 rows and 2 column with 6 different matrix.

n_scans = 84
tr = 7
hrf_models = ['canonical', 'canonical with derivative', 'fir', 'spm', 'spm_time', 'spm_time_dispersion']
drift_model = 'cosine'
frametimes = np.arange(0, n_scans * tr,tr)
hfcut = 128

fig1 = plt.figure()

ax1 = fig1.add_subplot(3, 2, 1)
hrf_model = hrf_models[0]
design_matrix = make_dmtx(frametimes, paradigm, hrf_model=hrf_model, drift_model=drift_model, hfcut=hfcut)
ax1 = design_matrix.show()
ax1.set_position([.05, .25, .9, .65])
ax1.set_title('Design matrix with {} as hrf_model'.format(hrf_model))

ax2 = fig1.add_subplot(3, 2, 2)
hrf_model = hrf_models[1]
design_matrix = make_dmtx(frametimes, paradigm, hrf_model=hrf_model, drift_model=drift_model, hfcut=hfcut)
ax2 = design_matrix.show()
ax2.set_position([.05, .25, .9, .65])
ax2.set_title('Design matrix with {} as hrf_model'.format(hrf_model))

......

ax6 = fig1.add_subplot(3, 2, 6)
hrf_model = hrf_models[5]
design_matrix = make_dmtx(frametimes, paradigm, hrf_model=hrf_model, drift_model=drift_model, hfcut=hfcut)
ax6 = design_matrix.show()
ax6.set_position([.05, .25, .9, .65])
ax6.set_title('Design matrix with {} as hrf_model'.format(hrf_model))

plt.show()

Currently the output is a figure of 3 rows and 2 columns with blank graph on it, and then each design matrix displayed individually bellow.

Moreover, a loop over the list hrf_models would be quite better than repeating 6 times the same block. I did it at some point, but the output was exactly the same sadly.

Current ouput (need to scroll to see all the design matrix):

Current output

Thanks for the help!

1

There are 1 answers

1
ImportanceOfBeingErnest On BEST ANSWER

Essentially the excerpt from the docstring you put in the question already tells you the solution. You need to use the ax argument to DesignMatrix.show()

ax1 = fig1.add_subplot(3, 2, 1)
design_matrix = make_dmtx(...)
design_matrix.show(ax = ax1)

To use a loop, you may produce all axes first and then loop over them.

fig, axes = plt.subplots(nrows=3,ncols=2)
for i, ax in enumerate(axes.flatten()):
    hrf_model = hrf_models[0]
    design_matrix = make_dmtx(frametimes, paradigm, hrf_model=hrf_models[i], 
                              drift_model=drift_model, hfcut=hfcut)
    design_matrix.show(ax = ax)

Note that I haven't tested anything here because I don't have nipy available.