Shika93
Shika93

Reputation: 659

Title over a group of subplots

I want a figure with six plots inside; I split it with subplots. For example

for i = 1:12
    subplot(3,4,i)
    plot(peaks)
    title(['Title plot ',num2str(i)])
end

I would like to add two global titles, let's say a global title for the six plots on the left hand side and another title for the six other plots on the right hand side.

enter image description here

I don't have 2018b version, so I cannot use sgtitle('Subplot Title');. Is it possible use suptitle('my title'); somehow? I can use text() but resizing the window, the two labels move.

Upvotes: 1

Views: 3323

Answers (2)

EBH
EBH

Reputation: 10440

You can use annotation for that, with the location of subplots 1 and 3:

for k = 1:12
    sp(k) = subplot(3,4,k);
    plot(peaks)
    title(['Title plot ',num2str(k)])
end
spPos = cat(1,sp([1 3]).Position);
titleSettings = {'HorizontalAlignment','center','EdgeColor','none','FontSize',18};
annotation('textbox','Position',[spPos(1,1:2) 0.3 0.3],'String','Left title',titleSettings{:})
annotation('textbox','Position',[spPos(2,1:2) 0.3 0.3],'String','Right title',titleSettings{:})

enter image description here

Upvotes: 1

Sven-Eric Krüger
Sven-Eric Krüger

Reputation: 1327

I did not test this, but you can get the handle to a subplot object and then perform the title method on this handle. I would also suggest to then apply the title after the loop.

CODE

for k = 1:12
    h(k) = subplot(3, 4, i)
    plot(peak)
end

title(h(1), 'Left side')
title(h(8), 'Right side')   % find out the right index yourself

Remark:

Do not use i or j as iteration variable for they are already defined in the namespace of MATLAB as imaginary unit.

Upvotes: 1

Related Questions