Position of two subplots with bar charts

37 次查看(过去 30 天)
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