display a shared legend for bar graph
Ältere Kommentare anzeigen
Hi all,
I want to display a shared legend for a bar graph. I have tried a few things but they didn't work out. Could anyone please help? I really appreciate your support. Please find the code below.
Thanks
P_g1(1,:) = [7.8269 5.8145 0.5642+0.0030 0.1352];
P_g1(2,:) = [7.8612 5.7935 0.5371+0.0024 0.2280];
P_g1(3,:) = [7.8815 5.7321 0.5095+0.0020 0.3433];
P_g1(4,:) = [7.8750 5.6529 0.4718+0.0017 0.4496];
P_g2(1,:) = [7.8034 5.7474 0.3709+0.0019 0.3708];
P_g2(2,:) = [7.8572 5.9635 0.4167+0.0015 0.1771];
P_g2(3,:) = [7.8758 6.0315 0.4243+0.0013 0.1223];
P_g2(4,:) = [7.7865 6.0026 0.4204+0.0012 0.1422];
sl = [240:20:300];
%%
figure
t = tiledlayout('flow','TileSpacing','compact');
nexttile
b = bar(sl,P_g1,0.9);
set(b, {'DisplayName'}, {'Accepted power','Absorbed power','Losses','Radiating power'}')
ax=gca;
ax.TickLabelInterpreter = 'latex';
axis tight
ylim([0 8])
grid on
ylabel('Power (W)','Interpreter','Latex')
xlabel('Shield length (mm)','Interpreter','Latex')
title('$\gamma_1$','Interpreter','Latex')
%h = legend('Location','eastoutside');
%set(h,'Interpreter','Latex')
set(gcf,'Color','w')
set(findall(gcf,'-property','FontName'),'fontsize',18)
nexttile
b = bar(sl,P_g2,0.9);
set(b, {'DisplayName'}, {'Accepted power','Absorbed power','Losses','Radiating power'}')
ax=gca;
ax.TickLabelInterpreter = 'latex';
axis tight
ylim([0 8])
grid on
ylabel('Power (W)','Interpreter','Latex')
xlabel('Shield length (mm)','Interpreter','Latex')
title('$\gamma_2$','Interpreter','Latex')
%h = legend('Location','eastoutside');
%set(h,'Interpreter','Latex')
set(gcf,'Color','w')
set(findall(gcf,'-property','FontName'),'fontsize',18)
nexttile
set(b, {'DisplayName'}, {'Accepted power','Absorbed power','Losses','Radiating power'}')
h = legend;
h.Layout.Tile = 'east';
2 Kommentare
dpb
am 22 Okt. 2021
You write over your various figure handles so there's no access to any but the latest -- just as a side point.
What, effect, specifically, are you trying to achieve, in other words, what does "a shared legend" mean to you?
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Legend finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

