![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/213506/image.png)
Position of two subplots with bar charts
45 ビュー (過去 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')
0 件のコメント
回答 (2 件)
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)])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/213506/image.png)
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')
0 件のコメント
John Doe
2019 年 4 月 12 日
I believe the answer is in the above question. The position can be difficult.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!