Question

I am trying to plot a figure with five sets of data with subplot is having individual legend but the problem is axis is getting mismatched when i trying to put the legend of plots having varies length of text.

x = [1:10];
y = 2*x;
z = x+1.25*y;
z1 = z+x;
subplot(4,1,1);
plot(x);
legend('x Variable','Location','NorthEastOutside');
subplot(4,1,2);
plot(y);
legend('y var','Location','NorthEastOutside');
subplot(4,1,3);
plot(z);
legend('z','Location','NorthEastOutside');
subplot(4,1,4);
plot(z1);
legend('z1 point','Location','NorthEastOutside');

enter image description here

All legends when location is 'NorthEastOutside' [when outside the plot] getting allocated right alignment of the legend. I want the data with equal axes length or left justified legend.

Is it possible to get the data with Left Justified without comprising on the axes length...?

Was it helpful?

Solution

This is one solution to make to width of all subplots the same

x = [1:10];
y = 2*x;
z = x+1.25*y;
z1 = z+x;
h(1)=subplot(4,1,1);
plot(x);
legend('x Variable','Location','NorthEastOutside');
h(2)=subplot(4,1,2);
plot(y);
legend('y var','Location','NorthEastOutside');
h(3)=subplot(4,1,3);
plot(z);
legend('z','Location','NorthEastOutside');
h(4)=subplot(4,1,4);
plot(z1);
legend('z1 point','Location','NorthEastOutside');

m=zeros(length(h),4);
for k=1:length(h)
    m(k,:) = get(h(k),'Position');
end

m(:,3) = max(m(:,3));
for k=1:length(h)
    set(h(k),'Position',m(k,:));
end

Plot

OTHER TIPS

This solution is very similar to the answer of user3544639, but without loops and more generic as there is no need to give handles to all the subplots.

%// get all subplot axes handles of current figure
s = findobj(gcf,'Type','axes','Tag','');
%// get cell array with positions
p = get(s,'position');

%// masking of positons vector
mask = [0 0 1 0];

%// maximum width
max_width = max( cell2mat(p)*mask' );

%// assinging of new width
arrayfun(@(x) set(s(x),'position',p{x}.*~mask + max_width*mask), 1:numel(s));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top