1

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, works nicely ! Never used axes classes before, and I had quite a struggle with it... Didn't thought of putting parameters to show even when it was written in front of me... Thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.