Position of two subplots with bar charts

조회 수: 38 (최근 30일)
Marc Gebhardt
Marc Gebhardt 2019년 4월 12일
편집: David Wilson 2019년 4월 12일
Hi guys,
to plot two barcharts, I used the subplot command. It is necessary to align both plots exactly under each other.
The problem is the legend. If both legends have not the same number of letters (or space), one of the plots is shorter. I tried to solve this with position of legend or plot, and overwriting the second plots legend or plot position.
clear
clc
data1=[1 2 3 4 5];
data2=[5 4 3 2 1];
figure(1)
subplot(2,1,1)
p1=bar(data1);
legend('Data from measurements and some space so its wide enough','Location','EastOutside')
subplot(2,1,2)
p2=bar(data2);
legend('Just text','Location','EastOutside')

답변 (2개)

David Wilson
David Wilson 2019년 4월 12일
편집: David Wilson 2019년 4월 12일
One option is to obtain the position of the plot with the long legend, and then re-size the other to the same width. But it's pretty ugly though.
Try the following:
clear
clc
data1=[1 2 3 4 5];
data2=[5 4 3 2 1];
figure(1)
subplot(2,1,1)
p1=bar(data1);
ht = legend('Data from measurements and some space so its wide enough','Location','Eastoutside')
hp = gca;
posn1 = hp.Position;
subplot(2,1,2)
p2=bar(data2);
legend('Just text','Location','EastOutside')
hp2 = gca;
posn2 = hp2.Position;
set(hp2,'Position',[posn2(1:2), posn1(3:4)])
You could of course put the legend on the top or bottom with northoutside.
Or have a mult-line legend
ht = legend(['Data from measurements', newline 'and some space so its' newline 'wide enough'],'Location','Eastoutside')

John Doe
John Doe 2019년 4월 12일
I believe the answer is in the above question. The position can be difficult.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by