How can I add a legend in the end of sublot ( on the bottom of sublot ) ?\
3 次查看(过去 30 天)
显示 更早的评论
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');
0 个评论
回答(2 个)
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
0 个评论
Star Strider
2021-4-23
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 个评论
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.
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.
EDIT — (24 Apr at 02:44)
Revised code excerpt.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Labels and Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!