- Refer to the documentation of subplot for creating subplot grids: https://www.mathworks.com/help/matlab/ref/subplot.html
- Refer to the documentation of sgtitle for adding a title to a subplot grid: https://www.mathworks.com/help/matlab/ref/sgtitle.html
- Refer to the documentation of text for adding text annotations: https://www.mathworks.com/help/matlab/ref/text.html
Add title to one row of a subplot
23 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
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!
0 Comments
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.
0 Comments
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!