How to increase the space between the title of each graph and the graph in a tiled layout?

12 次查看(过去 30 天)
Hi all,
How can I increase the space between the title of each graph and the graph itself in a tiled layout?
StackBar = tiledlayout(2,3);
nexttile
ArbeitAnnual = bar(ArbeiterData(:,1:5:30)'.*277.78,'stacked')
title('Arbeiter/in','Position',30)
set(gca,'xtick',1:6,...
'xticklabel',{'Arbeit1','Arbeit2','Arbeit3','Arbeit4','Arbeit5', 'Arbeit6'})
ylabel('Arbeiten (Total)')
ylim([0 370000000])
....
Also, is it possible to place the legend for all graphs in the sixth position in the above code?
Also, I appreciate it if you could teach me to show the percentage of each stack on it and the total value on the top. Thanks

回答(1 个)

Aashray
Aashray 2025-6-26
I really like that you want to know the specifics of plotting multiple graphs within a single tiled layout. I understand that you have three main questions, here's how you can approach them:
1. Adjust space between subplot titles and the graphs. Instead of using the default title, adjust the vertical position manually:
t = title('Title Name');
t.Units = 'normalized';
t.Position(2) = 1.03; % Moving the title slightly above the plot. You can change this to desired value.
2. Show the legend only in the 6th tile. Use an empty tile and attach the legend explicitly:
nexttile(6)
ax = gca;
cla(ax); % Clear any plot
axis off; % Hide axes
legend(ax, barHandle, {'Category 1', 'Category 2', ...}, 'Location', 'northwest');
title('Legend');
3. Add percentage labels and total on top of stacked bars. Loop through each bar and use text to annotate percentages and totals:
for i = 1:size(data,1)
y_base = 0;
for j = 1:size(data,2)
val = data(i,j);
percent = val / sum(data(i,:)) * 100;
if percent > 5 % skip tiny segments
text(i, y_base + val/2, sprintf('%.1f%%', percent), ...
'HorizontalAlignment', 'center', 'FontSize', 8);
end
y_base = y_base + val;
end
% Add total value above the stack
text(i, y_base + 0.03*max(sum(data,2)), ...
num2str(sum(data(i,:)), '%.0f'), ...
'HorizontalAlignment', 'center', 'FontWeight', 'bold');
end
Using these fixes, I have obtained the plot you desired, attaching it for reference.

类别

Help CenterFile Exchange 中查找有关 Title 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by