# Position of two subplots with bar charts

37 visualizzazioni (ultimi 30 giorni)
Marc Gebhardt il 12 Apr 2019
Modificato: David Wilson il 12 Apr 2019
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 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (2)

David Wilson il 12 Apr 2019
Modificato: David Wilson il 12 Apr 2019
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')
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

John Doe il 12 Apr 2019
I believe the answer is in the above question. The position can be difficult.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Legend in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by