Add title to one row of a subplot

55 views (last 30 days)
Zeynab Mousavikhamene
Zeynab Mousavikhamene on 15 Aug 2019
Answered: Adam Danz on 5 Sep 2024
I am using subplot and there 6*2. I want to add title for every two plots that are in the same row.
Any suggestion?

Answers (2)

Hari
Hari on 3 Sep 2024
Hi Zeynab,
I understand that you want to add a single title to each row of a subplot grid that has 6 rows and 2 columns in MATLAB. Each title should span the two subplots in the same row.
I assume you are using the subplot function to create your grid
You can use the "sgtitle" function to add a title that spans the entire figure. For individual row titles, you can use the "text" function to place a title above each pair of subplots.
Here is the sample code for the same:
% Example: Create a 6x2 subplot grid
for i = 1:12
subplot(6, 2, i);
plot(rand(1, 10)); % Example plot
end
% Add a main title for the whole figure
sgtitle('Main Title for the Subplot Grid');
% Add titles for each row
for i = 1:6
% Calculate position for the text
text(0.5, 0.5, sprintf('Row %d Title', i), 'Units', 'normalized', ...
'HorizontalAlignment', 'center', 'FontSize', 10, ...
'Position', [0.5, 1.1, 0], 'Parent', subplot(6, 2, (i-1)*2+1));
end
Output:
References:
Hope this helps!

Adam Danz
Adam Danz on 5 Sep 2024
Instead of using subplot, I recommend using tiledlayout which supports nested layouts and global titles.
Follow these instructions to create a centered title for each row in a tiled layout or in when using subplot.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!