display a shared legend for bar graph
19 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
dpb
le 22 Oct 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?
Réponse acceptée
Dave B
le 22 Oct 2021
Modifié(e) : Dave B
le 22 Oct 2021
There's an extraneous call to nexttile at the bottom, but after removing that I see what I'd call a shared legend....can you help me understand what's missing? (or is this what you were expecting)
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 commentaires
Dave B
le 22 Oct 2021
Glad it helps!
Actually legend does need to be in a tile! (it's the 'east' tile)
I think what happened was you called nexttile, and legend defaults to grabbing its data from the current axes, so you were making a legend that went with an empty axes.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Legend dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!