- 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
61 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Hari
2024-9-3
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 个评论
Adam Danz
2024-9-5
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!