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?

回答(2 个)

Hari
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!

Adam Danz
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.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by