Position of two subplots with bar charts
39 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
David Wilson
el 12 de Abr. de 2019
Editada: David Wilson
el 12 de Abr. de 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 comentarios
John Doe
el 12 de Abr. de 2019
I believe the answer is in the above question. The position can be difficult.
0 comentarios
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!