display a shared legend for bar graph

1 view (last 30 days)
Shan Chu
Shan Chu on 22 Oct 2021
Commented: Dave B on 22 Oct 2021
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 Comments
Shan Chu
Shan Chu on 22 Oct 2021
Hi, Thanks for your comment. Basically, the two bar graphs have the same legend like in this picture. So I think it would be nicer to have a shared legend. I tried to follow one example in the section: "Display Shared Legend in Tiled Chart Layout" (https://uk.mathworks.com/help/matlab/ref/legend.html)
But it doesn't work for bar graphs.
Thanks

Sign in to comment.

Accepted Answer

Dave B
Dave B on 22 Oct 2021
Edited: Dave B on 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 Comments
Dave B
Dave B on 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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by