Title for a column of subplots

18 次查看(过去 30 天)
Eileen Lukens
Eileen Lukens 2020-10-19
回答: ag 2024-9-26
I want to have a title for each column and row of subplots. I've tried using sgtitle but it is not giving me the effect I want. Is there another way to achieve this? I've included a simplifed example below of what I want to achieve.

回答(1 个)

ag
ag 2024-9-26
Hi Eileen,
To achieve titles for each column and row of subplots, you can manually add text annotations to the figure. This allows you to place titles exactly where you want them.
The below code demonstrates how to achieve this using dummy data:
% Create a figure
figure;
% Create a 2x2 grid of subplots for demonstration
subplot(2, 2, 1);
plot(rand(10, 1));
title('Plot 1');
subplot(2, 2, 2);
plot(rand(10, 1));
title('Plot 2');
subplot(2, 2, 3);
plot(rand(10, 1));
title('Plot 3');
subplot(2, 2, 4);
plot(rand(10, 1));
title('Plot 4');
% Add row titles
annotation('textbox', [0.01, 0.75, 0.1, 0.1], 'String', 'Row 1', 'EdgeColor', 'none', 'FontSize', 12, 'FontWeight', 'bold');
annotation('textbox', [0.01, 0.25, 0.1, 0.1], 'String', 'Row 2', 'EdgeColor', 'none', 'FontSize', 12, 'FontWeight', 'bold');
% Add column titles
annotation('textbox', [0.3, 0.95, 0.1, 0.1], 'String', 'Column 1', 'EdgeColor', 'none', 'FontSize', 12, 'FontWeight', 'bold');
annotation('textbox', [0.7, 0.95, 0.1, 0.1], 'String', 'Column 2', 'EdgeColor', 'none', 'FontSize', 12, 'FontWeight', 'bold');
For more details, please refer to the following MathWorks documentations:
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Labels and Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by