How can I add a legend in the end of sublot ( on the bottom of sublot ) ?\

조회 수: 4 (최근 30일)
Rahim Rahim
Rahim Rahim 2021년 4월 23일
편집: Star Strider 2021년 4월 24일
Hello,
I have a code of subplots, I want to add a legend at the end of sublot like the picture :
This is my code:
x1=[1;2;3]
y1 = [91,25,3];
y2 = [71,22,13];
y3 = [81,22,33];
y4 = [71,12,63];
y5 = [61,42,23];
y6 = [51,21,23];
figure;
subplot(3,2,1);
b1=bar(x1,y1);
ylabel('Cost per Byte (%)');
ylim([0 max(y1)+10]);
title('Aaa');
subplot(3,2,2);
b2=bar(x1,y2);
ylabel('Security (%)');
ylim([0 max(y2)+10]);
title('Second plot');
subplot(3,2,3);
b3=bar(x1,y3);
ylabel('Data rate (kbps)');
ylim([0 max(y3)+10]);
title('Third plot');
subplot(3,2,4);
b4=bar(x1,y4);
ylabel('Delay (ms)');
ylim([0 max(y4)+10]);
title('Fourth plot');
subplot(3,2,5);
b5=bar(x1,y5);
ylabel('Jitter (ms)');
ylim([0 max(y5)+10]);
title('Fifth plot');
subplot(3,2,6);
b6=bar(x1,y6);
ylabel('Loss ratio 10^6 (ms)');
ylim([0 max(y6)+10]);
title('Sixth plot');
legend('peaks');
set(b1,'FaceColor','red');

답변 (2개)

Jonas
Jonas 2021년 4월 23일
the asiest way is to use tiledlayout instead of subplot and then setting the legend position to 'south'. if you want or have to use subplot then just use
lg=legend(...)
and set the position property of the lg object to the place where yiu want to see the legend

Star Strider
Star Strider 2021년 4월 23일
@Rahim Rahim — Responding to your Comment to your earlier Question, I took a look at this.
This is the best I can do —
x1=[1;2;3];
y1 = [91,25,3];
y2 = [71,22,13];
y3 = [81,22,33];
y4 = [71,12,63];
y5 = [61,42,23];
y6 = [51,21,23];
figure;
subplot(4,2,1);
b1=bar(x1,y1);
ylabel('Cost per Byte (%)');
ylim([0 max(y1)+10]);
title('Aaa');
subplot(4,2,2);
b2=bar(x1,y2);
ylabel('Security (%)');
ylim([0 max(y2)+10]);
title('Second plot');
subplot(4,2,3);
b3=bar(x1,y3);
ylabel('Data rate (kbps)');
ylim([0 max(y3)+10]);
title('Third plot');
subplot(4,2,4);
b4=bar(x1,y4);
ylabel('Delay (ms)');
ylim([0 max(y4)+10]);
title('Fourth plot');
subplot(4,2,5);
b5=bar(x1,y5);
ylabel('Jitter (ms)');
ylim([0 max(y5)+10]);
title('Fifth plot');
subplot(4,2,6);
b6=bar(x1,y6);
ylabel('Loss ratio 10^6 (ms)');
ylim([0 max(y6)+10]);
title('Sixth plot');
legend('peaks');
set(b1,'FaceColor','red');
subplot(4,2,[7 8])
cm = turbo(3);
hold on
for k = 1:3
hb78(k) = bar(x1(k), 1);
hb78(k).FaceColor = cm(k,:);
% hb78(k).Visible = 'off';
end
hold off
Ax = gca;
pos = Ax.Position;
Ax.Visible = 'off';
lgd = legend(hb78, '1','2','3');
lgd.Position = pos;
lgd.NumColumns = 3;
Note — The bar colours do not change here, unlike in your earlier Question that specifically requested help with that, so I added that in the bar call creating the legend in case you want to add it to the other subplot bar plots. (It would work the same as the ‘barfcn’ function earlier.) Otherwise, there is no reason for the legend specifically, because there is no way to distinguish the individual bars other than by using the xticklabels function (introduced in R2016b) to describe what they are.
.
  댓글 수: 2
Rahim Rahim
Rahim Rahim 2021년 4월 23일
@Star Strider but How can I change the size of legend box to Pos = pos /2
Star Strider
Star Strider 2021년 4월 23일
편집: Star Strider 2021년 4월 24일
I have no idea what you are asking, so I have no exact idea how to reply.
The ‘pos’ vector is described in the Position property section of the documentation.
To reduce the lateral dimensions while still centring the legend, try this —
Ax = gca;
pos = Ax.Position;
Ax.Visible = 'off';
% Ax.Box = 'off';
lgd = legend(hb78, 'Left','Centre','Right');
lgd.Position = pos+[0.15 0 -0.30 0];
lgd.NumColumns = 3;
Ax.Position = pos+[0.15 0 -0.30 0]; % Must Match New ‘lgd.Position’ Values
Note — The third element must be negative two times the value of the first element in the addition vector, so with ‘x’ here as the desired offset
pos+[x 0 -2*x 0];
Remember to do the same operation for ‘lgd.Position’ and ‘Ax.Position’.
Experiment to get different results.
See Legend Properties for all the available options.
EDIT — (24 Apr at 02:44)
Revised code excerpt.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Labels and Annotations에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by